pytest 测试框架
介绍:
- 简单灵活,容易上手
- 支持参数化
- 测试用例的skip 和 xfail,自动失败重试等处理
- pytest具有很多的第三方插件,并且可以自定义扩展。如 pytest-allure,pytest-xdist(多CPU分发)等
- 可以很好的和jenkins集成
文档资料:
官方文档:https://docs.pytest.org/en/latest/
第三方库:https://pypi.org/search/?q=pytest
测试用例的识别与命名规则:
- 文件名: test_*.py 或 *_test.py
- 类名:Test* (测试类不能带有__init__方法)
- 方法名:test_* (不在class中的所有test_* 方法也可识别)
安装与升级:
pip install pytest
pip install -U pytest
pytest --version 查看版本
运行:
- pytest -k case_name -v # -k 指定case、-v 输出详细日志
- 解释器运行
if __name__=="__main__": # -v 输出详细日志 pytest.main(["test_pytest.py::test_answer","-v"]) # 文件下特定用例执行,可为类名或方法名 pytest.main(["test_pytest.py", "-v"]) # 文件下所有符合命名规则用例执行
装饰器-数据参数化:
@pytest.mark.parametrize(argnames,argvalues)
argnames:要参数化的变量,string(逗号分隔),list,tuple
argvalues:参数化的值,list,list[tuple] ,与参数化变量一一对应
# 使用字符串
@pytest.mark.parametrize('a,b,c',[(1,1,2),(2,3,4),(4,5,9)])
def test_a(self,a,b,c):
assert a+b==c
# 使用list
@pytest.mark.parametrize(["a","b"],[(1,1),(2,3),(4,5)])
def test_aa(a,b):
assert a==b
# 使用tuple
@pytest.mark.parametrize(("a","b"),[(1,1),(2,3)])
def test_aaa(a,b):
assert a==b
装饰器-fixture
- 相当于setup 功能,需要用到的用例方法括号中添加fixture函数
- 定义fixture跟定义普通函数差不多,唯一区别就是在函数上加个装饰器@pytest.fixture(),fixture命名不要以test开头,跟用例区分开
@pytest.fixture() def login(): print("login success") username='jerry' return username class TestDemo: def test_b(self,login): print(f"a username='{login}")
yaml实现参数化
- 实现list -
- 实现dict key:value
- 进行嵌套
- 如:
companies:[{id:1,name:company1,price:200W},{id;2,name:company2,price:500W}]
companies: - id:1 name:company1 price:200W - id:2 name:company2 price:500W
- 加载yaml文件 yaml.safe_load(open("./data.yaml"))
- 安装包 pyYAML
allure 安装
- windows/mac 通用安装方法:
- 下载zip包安装 https://repo1.maven.org/maven2/io/qameta/allure/allure-commandline/
- 解压
- 配置环境变量:把bin目录加入path环境变量
- cmd 中输入 where allure,查看是否能找到
- MAC 可以通过一条命令安装:brew install allure
- 官网:http://allure.qatools.ru/
- 安装 allure-pytest 插件 : pip install allure-pytest
运行过滤规则
|
功能
|
--allure-severities 级别1,级别2,..
|
根据级别过滤
|
--allure-features=FEATURES_SET
|
根据features 过滤
|
--allure-stories=STORIES_SET
|
根据 story 过滤
|
--alluredir=DIR
|
指定临时报告的生成路径
|
--clean-alluredir
|
清除之前生成的结果
|
--allure-no-capture
|
不添加pytest捕捉的logging/stdout/stderr到测试报告中
|
Allure 常用的特性
- 场景:在报告中看到测试功能,子功能或场景、测试步骤等
- 解决办法: @Feater @story @step @attach
- 步骤:
- import allure
- @allure.feature ("功能名称")
- @allure.story ('子功能名称')
- @allure.step ('步骤细节')
- @allure.attach ('具体文本信息'),需要附加的信息,可以是数据、文本、图片、视频、网页
- 如果只测试登录功能运行的时候可以加限制过滤
@allure.feature("搜索模块") class TestSearch(): @allure.story("搜索成功") def test_search_success(self): with allure.step("步骤1:打开应用"): print("这是搜索:测试用例,搜索成功") @allure.feature("登录模块") class TestLogin(): @allure.story("登录成功") def test_login_success(self): print("这是登录:测试用例,登录成功") pass
allure 特性-feature/story
注解 @allure.feature 与 @allure.store 的关系
feature 相当于一个功能,一个大的模块,将case分类到某个feature 中,报告中behaviore中显示,相当于testsuite
story 相当于对应这个功能或者模块下的不同场景,分支功能,属于 feature之下的结构,报告在features中显示,相当于testcase
feature 与 story 类似于父子关系
allure 特性-step
测试过程中每个步骤,一般放在具体逻辑方法中
可以放在关键步骤中,在报告中显示
在app,web自动测试中,建议每切换到一个新的页面当做一个step
用法:
@allure.step() 只能以装饰器的形式放在类或者方法上面
with allure.step() 可以放在测试用例方法里面,但测试步骤的代码需要被该语句包含
allure 特性-testcase
关联测试用例(关联测试用例的地址链接)
@allure.testcase(TEST_CASE_LINK,"Test case title")
import allure TEST_CASE_LINK = 'https://i.cnblogs.com/posts/edit;postId=15013680' @allure.testcase(TEST_CASE_LINK,"Test case title") def test_with_testcase_link(): pass
按重要性级别进行一定范围测试 @allure.severtity()
场景:通常测试有P0、冒烟测试、验证上线测试。可按照重要级别分别执行
解决方法:
通过附加 pytest.mark 标记
通过allure.feature.allure.story
推荐通过 allure.severity 来附加标记:级别有 Trivial 不重要 Minor 不太重要;Normal 正常问题;Critical 严重;Blocker 阻塞
实现步骤:
@allure.severtity(allure.severity_level.TRIVIAL) 可用在方法级别 也可用在类级别
执行:pytest -s -v 文件名 --allure-severities normal,critical
import allure import pytest def test_with_no_serverity_label(): pass @allure.severity(allure.severity_level.TRIVIAL) # 可用在方法级别 也可用在类级别 def test_with_trivial_severity(): pass @allure.severity(allure.severity_level.NORMAL) # 可用在方法级别 也可用在类级别 def test_with_normal_severity(): pass @allure.severity(allure.severity_level.NORMAL) # 可用在方法级别 也可用在类级别 class TestClassWithNormalSeverity(object): def test_inside_the_normal_severity_test_class(self): pass @allure.severity(allure.severity_level.CRITICAL) def test_inside_the_normal_severity_test_class_with_override(self): pass if __name__=="__main__": pytest.main()
allure.title("title 名") 代替方法名显示在报告中
添加截图、图片、视频
场景:前端自动化测试经常需要附加图片或html,在适当的地方,适当的时机截图
解决:@allure.attach 显示许多不同类型的提供的附图,可以补充测试、步骤、或测试结果
步骤:
def test_attach_text(): allure.attach("这是一个纯文本",attachment_type=allure.attachment_type.TEXT) def test_attach_html(): allure.attach("<body>这是一段htmlbody块</body>","html测试块",attachment_type=allure.attachment_type.HTML) def test_attach_photo(): allure.attach.file("图片地址","图片",attachment_type=allure.attachment_type.PNG) def test_attach_video(): allure.attach.file("视频地址","视频",attachment_type=allure.attachment_type.MP4) if __name__=="__main__": pytest.main()