Pytest
pip install pytest
pip install pytest-html
pip install pytest-ordering
pip install pytest-rerunfailures
1.安装 pip3 install pytest
2.初始化和结束函数:
1.setup teardown : 在一个类内部每个测试方法的开始和结束运行一次
2.setup_class,teardown_class:在一个类内部只运行一次,不关心有多少测试方法
3.pytest插件
1.pytest-html: 生成测试报告
安装:pip3 install pytest-html
使用:跟在命令行里,pytest -s 测试文件 —html=路径/xx.html
2.pytest-ordering: 控制测试函数运行顺序
安装:pip install pytest-ordering
使用:@pytest.mark.run(order=x)
x: 1.全为正数 或者 全为负数 值越小 优先级越高,意味着最先执行
2.正数和负数同时存在,正数优先级高
3.值为负数时,优先级低于没被标记的测试方法
4.值为正数时,优先级高于没被标记的测试方法
5.order 为正数时,值越小,优先级越高, 比如:order=1 要比order=3 优先执行
6.order为负数,值越小,优先级越高, 比如:order-3 要比order=-1优先执行, 因为-3<-1
7.没有标识order,程序按照先后顺序执行
3.pytest-rerunfailures : 失败测试函数重试机制
安装:pip install pytest-rerunfailures
使用:在命令行参数中配置:—reruns n
n:重试次数
4.配置文件:
1.命令行参数 addopts = -s —reruns n —html=路径/xx.html # 命令之间空格分隔
2.搜索路径:testpaths = 脚本存放的路径
3.搜索文件名:python_files = test_*.py
4.搜索测试类名:python_classes = Test_*
5.搜索方法名:python_functions = test_*
[pytest] #固定 配置文件名pytest.ini 也是固定的,要执行这个文件,就必须要cd的这个文件所在文件夹
#所有参数的Key都是固定的
#命令行参数 执行命令 相对路径是相对于 pytest.ini这个文件
addopts = -s --html=./report/report.html
#测试路径
#遇到fail时会重新执行failed的case,n代表重新执行的次数
addopts = -s -reruns n --html=./report/report.html
testpaths =./myscript
# 搜索文件名
python_files = test_p*.py
#搜索测试类名
python_classed =Test_*
#搜索测试方法名
python_funcation = test_*
命令行形式执行:
pytest -s ./study/study_mypy/myscript/test_p.py 会执行test_p.py 中的方法
pytest -s ./t.py --html=report.html 命令行形式生成报告
直接点击run执行
if __name__ == '__main__':
pytest.main(['-v','-s','t.py::test_a'])#执行一个test_a的方法
pytest.main(['-v','-s','t.py']) #执行这个t.py中的所有方法的方法
pytest -v -s test_cass.py -v表示打印详细的信息 -s表示输出print打印信息
pytest -v -s test_cass.py::test_one ::表示只执行某个测试函数
Pytest组件的使用
于 2021-09-14 14:21:51 首次发布