一、pytest简洁和好处
自动发现测试用例 testloader
断言方便 ,自定义错误提示 assert 正则匹配
灵活运行指定的测试用例,指定模块,制定测试类,测试用例 -k 标签化,回归 正向 冒烟 登陆
环境管理灵活。
丰富的插件,测试报告,插件介绍 allure(比较成熟的测试报告体系,unittest不支持这个插件)
和unittest / nose兼容,pytest中标签、生成allure、分析最慢的测试用例和unittest兼容
unittest的好处:
1.标准库,兼容性好
2.更加容易理解
pytest和unittest兼容是有条件的,pytest使用以下功能就不能和unittes兼容:
parametrize
fixture(autouse=True的可以) 不能和setUp同时使用
Custom hooks
二、自动发现测试用例原理,命名方式
文件名必须以test_*.py 和 *_test.py开头或结尾,
如果有类名,必须以Test开头,没有__init__函数,测试方法名以test_开头的函数
没有类,那么以函数为单位的函数名必须以test_开头
三、断言: 自定义提示文案
assert 1==1, "提示文案"
四、运行方式
1.在控制台指定执行范围
在指定文件中输入pytest,此时会收集该文件中所有测试用例
指定某个模块,例如 pytest test_demo1.py
指定某个目录及其子目录的所有测试文件,pytest 文件名,例pytest testcase
指定某个模块下某个类的某个用例,pytest 模块名::类名::方法名,例pytest test_demo2.py::TestDemo2::test_success
指定某个模块下的某个方法,pytest 模块名::方法名,例pytest test_demo1.py::test_fail
python -m pytest/unittest
2.编辑器
Run -->Run...-->Edit Configurations-->+ -->python test -->pytest -->Run
3.通过python代码执行pytest
直接执行pytest.main(),自动查找当前目录下,以test_开头的文件或者以_test结尾的py文件
五、自定义查找规则(在根目录下pytest.ini)
[pytest]
python_files=test_*.py
check_*.py
example_*.py
python_functions= *_test
python_classes= *Suite
四、mark随机测试(冒烟测试,给测试用例打标签)
第一步:mark注册,新建ini文件
#pytest.ini
[pytest]
markers=login
demo
第二步:
1.把标签贴到测试用例(方法)上,一个测试用例上可以贴多个标签,标签不仅可以贴到测试用例上,还可以贴到测试类上
@pytest.mark.标签名
2.贴标签第二种方法:标记在测试类中,固定写法
pytestmark = [pytest.mark.标签名, pytest.mark.标签名]
importpytestclassTestDemo4:
pytestmark=[pytest.mark.login]#@pytest.mark.login
deftest_demo_4(self):assert 1 == 1
第三步:运行的时候指定标签,在Terminal输入pytest -m "mark1 and not mark2"
注意:一定要使用双引号,单引号会报错
五、跳过函数
@pytest.mark.skip(reason='out-of-data api')
# skipif 当满足某个条件时就跳过
@pytest.mark.skipif(sys.plafform == "win32", reason="does not run on windows")
# 跳过预见错误
@pytest.mark.xfail(gen.__version__ < '0.2.0', reason='not supported until v0.2.0')
def test_api():
id_1 = gen.unique_id()
id_2 = gen.unique_id()
assert id_1 != id_2
六、pytest的用例执行顺序
pytest有自己内置的执行顺序规则,一般从上到下,可以通过安装插件pytest.ordering:run your tests in order
七、参数化
ddt数据驱动只是unittest支持数据驱动的一种库 ddt != 数据驱动
ddt模块不能和fixture共用,所以要换用pytest的参数化实现ddt
参数化会和unittest冲突,因为setUp这些东西都不会运行了!要改成pytest,不要继承unittest.TestCase
方法一:使用pytest.mark.parametrize()方式进行参数化
importpytest
Data= [1, 2, 3]
@pytest.mark.parametrize("data", Data)deftest_mock(data):print('测试开始')assert data == 2, "备注信息"
方法二:使用pytest.fixture()方式进行参数化,fixture装饰的函数可以作为参数传入其他函数
1.在固件中添加返回值
2.然后在测试方法中传入固件
3.在测试方法中,用固件名来表示其返回值
test_demo.py
importpytestclassTestDemo1:#定义参数列表
list_a = [1, 2, 3]#定义固件名is_success,并设置返回值
@pytest.fixture(params=list_a)defis_success(self, request):returnrequest.param#在测试方法中传入固件名
@pytest.mark.successdeftest_success(self, is_success):assert is_success == 1
方法三:测试数据和用例分离
可以采用conftest.py文件存储参数化数据和函数,模块下的用例执行时,会自动读取conftest.py文件中的数据
1.先定义一个固件is_success
conftest.py
importpytest#准备测试数据
list_a = [1, 2, 3]#params中需要传入list
@pytest.fixture(params=list_a)defis_success(request):return request.param
2.在测试类中传入固件名称
test_demo4.py
importpytestclassTestDemo4:
@pytest.mark.testdeftest_demo_4(self, is_success):#print('测试一下')
assert is_success == 1
fixture相对于setUp和tearDown来说的优势:
命名方式灵活,不局限于setUp和tearDown这几个命名
conftest.py配置里可以实现数据共享,不需要import就能自动找到一些配置
scope = "module"可以实现多个.py跨文件共享前置
scope = "session"可以实现多个.py跨文件使用一个session来完成多个用例
通过yield生成器实现了前置条件和后置条件的组合
1.先在根目录conftest.py文件中定义一个名为init_web的固件
importpytest
@pytest.fixture()definit_web():print("启动浏览器")yielddriverprint("关闭浏览器")
2.作为参数传到测试用例中
def test_demo(init_web):
3.接收这个东西
driver = init_web
importpytestclassTestDemo3:
@pytest.mark.logindeftest_demo3(self, init_web):
driver=init_webassert driver == 2
注意:
1.pytest 运行默认不会打印固件中print(),如果想打印,那么使用python -m login -s 或者 pytest --capture=no
九、重运行
安装插件:pip install pytest -rerunfailures
pytest --reruns 3 --reruns-delay 5
十、测试报告
各种插件地址:https://plugincompat.herokuapp.com/
html:--html=\report\demo.html
需要安装:
pip install pytest-html
# log
--resultlog=report/demo.txt 相对路径
# xml,jenkins使用
--junitxml=report/demo.xml
# html
--html=report/demo.html
main.py
importpytest
import timeif __name__ == '__main__':
ts = time.time()
pytest.main(['-m test', '-s','--resultlog=report/{}.txt'.format(ts),'--junitxml=report/demo.xml','--html=report/demo.html'])
在Terminal输入:python main.py
查看结果:在根目录下会生成一个含有各种报告的report文件夹
本文详细介绍了Python的pytest框架,包括其简洁性、自动发现测试用例的原理、断言方法、运行方式及自定义查找规则。重点讨论了pytest与unittest的兼容性,parametrize和fixture的使用,以及如何进行标记测试、跳过函数、参数化、固件(fixture)的优势。此外,还涵盖了重运行测试、生成测试报告的方法,如html、xml格式,以及如何通过main.py脚本控制测试流程。
2177

被折叠的 条评论
为什么被折叠?



