通过之前的学习 我们已经知道了 pytest 运行测试用例 主要 以下两种方式:
1、主函数 传参运行 pytest.main(['-vs ./xxxx.py'])
2、命令行输入 运行pytest -vs './xxx.py'
在我们实际项目应用中 通过以上两种方式 去运行测试用例 维护成本较高
故pytest 还提供了第三种执行测试用例 的方式:通过配置文件pytest.ini的方式运行测试用例
pytest.ini配置文件的格式要求:
- 编码格式必须是ANSI
- 配置文件的存放目录必须是项目的根目录
这里我们通过nodepad++编辑器修改配置文件的编码格式
具体配置参数内容如下:
[pytest] # 命令参数 ,多个参数用空格分开 addopts = -vs #执行测试的路径 testpaths = ./pytest_demo66/pyini #匹配路径下的测试模块 python_files = test_*.py #匹配具体测试类,比如以Test开头 python_classes = Test* #匹配要运行的测试函数 python_functions = test #对测试用例进行分组分类,比如区分冒烟用例,回归用例等 marks = smoke:冒烟用例 master:回归 pre:预发 online:线上
配置pytest.ini后,我们后续运行测试的时候,pytest会优先读取这个配置文件里的配置,并按照配置参数去执行测试
我们前面讲了 pytest编写测试用例时默认的的命名规则:
- 文件名以 test_*.py 文件和*_test.py
- 以 test_ 开头的函数
- 以 Test 开头的类,且不能包含 __init__ 方法
- 以 test_ 开头的类里面的方法
- 所有的包 package 必须要有__init__.py 文件
但此时我们是通过配置文件的方式去运行测试,
故我们的测试模块名,方法名,函数名,类名 的命名规则就可以不按这个默认的规则去书写了
比如我们定义一个 模块hello.py,Login的测试类,abc_1的测试方法
hello.py
class Login: def abc_1(self): print('abc-1') def test_2(self): print('test_2') def test_3(self): print('test_3')
我们通过修改pytest.ini配置文件去运行这个测试用例
[pytest] addopts = -vs testpaths = ./pytest_demo66 python_files = hello_*.py python_classes = Login* python_functions = abc_*
命令行输入pytest 执行测试
结果如下:
========================================================================== test session starts =========================================================================== platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-0.13.1 -- d:\python\python.exe cachedir: .pytest_cache metadata: {'Python': '3.8.5', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.2', 'xdist': '2.4.0'}} rootdir: D:\pytest_demo, configfile: pytest.ini, testpaths: ./pytest_demo66/pyini plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.4.0 collected 1 item pytest_demo66/pyini/hello_world.py::Login::abc_1 test_1 PASSED =========================================================================== 1 passed in 0.03s ============================================================================
我们看到只有abc_1 这条用例被执行了 其他 测试方法并没有被执行,所以通过配置文件的方式 去执行用例,我们就可以灵活的命名自己想要的测试方法,类,以及模块名。
除此之外,我们在实际的项目种还有可能需要给用例做标记或分类
比如a用例是冒烟用例,b用例是回归用例等,现在通过配置文件的方式,我们就可以实现
test_2.py
import pytest @pytest.mark.smoke def test_3(): print('冒烟用例3') def test_1(): print('冒烟用例1') @pytest.mark.master def test_2(): print('回归用例2')
配置文件我们更改为:
[pytest] addopts = -vs testpaths = ./pytest_demo66/pyini python_files = test_*.py python_classes = Login* python_functions = test_* marks = smoke:冒烟用例 masster:回归 pre:预发 online:线上
命令行运行
pytest -m "smoke"
运行结果
D:\pytest_demo>pytest -m "smoke" ========================================================================== test session starts =========================================================================== platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-0.13.1 -- d:\python\python.exe cachedir: .pytest_cache metadata: {'Python': '3.8.5', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.2', 'xdist': '2.4.0'}} rootdir: D:\pytest_demo, configfile: pytest.ini, testpaths: ./pytest_demo66/pyini plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.4.0 collected 3 items / 2 deselected / 1 selected pytest_demo66/pyini/test_1.py::test_3 冒烟用例3 PASSED
我们看到 只有 标记了smoke的这个测试方法被执行了
修改命令行参数继续执行测试:
pytest -m "smoke or master"
运行结果:
D:\pytest_demo>pytest -m "smoke or master" ========================================================================== test session starts =========================================================================== platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-0.13.1 -- d:\python\python.exe cachedir: .pytest_cache metadata: {'Python': '3.8.5', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.2', 'xdist': '2.4.0'}} rootdir: D:\pytest_demo, configfile: pytest.ini, testpaths: ./pytest_demo66/pyini plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.4.0 collected 3 items / 1 deselected / 2 selected pytest_demo66/pyini/test_1.py::test_2 回归用例2 PASSED pytest_demo66/pyini/test_1.py::test_3 冒烟用例3 PASSED
============================================================== 2 passed, 1 deselected, 5 warnings in 0.03s =========================================================
我们看到标记了smoke 和master 的两条测试用例都被执行了
也就说针对不同标记 的测试用例,pytest可以组合一起运行测试
这极大的提高了运行测试的自由度