一、pytest 测试用例的运行方式
假设目录结构如下,run为测试执行入口
pytest 默认命名规则:
- 文件名以 test_*.py 文件和*_test.py
- 以 test_ 开头的函数
- 以 Test 开头的类,且不能包含 __init__ 方法
- 以 test_ 开头的类里面的方法
- 所有的包 package 必须要有__init__.py 文件
执行模式:
- 主函数模式
- pytest.main() ----运行所有模块
- pytest.main(['-vs','./test_1.py']) ----运行指定模块
- pytest.main(['-vs', './other_test']) ----运行指定目录
- pytest.main(['-vs','./test_1.py::test_2']) ----运行指定的测试方法
- pytest.main(['-vs', './test_Class.py::TestMycase1']) ----运行指定的测试类
- pytest.main(['-vs', './test_Class.py::TestMycase1::test_method3']) ----运行类中指定的测试方法
- 命令行模式
- pytest ----运行所有模块
- pytest -vs test_1.py ----运行指定模块
- pytest -vs other_test ----运行指定目录
- pytest -vs test_1.py::test_2 ----运行指定的测试方法
- pytest -vs test_Class.py::TestMycase1 ----运行指定的测试类
- pytest -vs test_Class.py::TestMycase1::test_method3 ----运行类中指定的测试方法
执行参数
-v 输出更详细的执行结果
pytest.main(['-v','./test_1.py'])
-s 输出更详细的打印信息(比如print 打印)
pytest.main(['-s', './test_1.py'])
还可以组合使用
pytest.main(['-vs', './test_1.py'])
-k 匹配用例名词(当前路径下的 文件名、类名、方法名,备注可以结合or,not 等关键字 ,不区分大小写)
pytest.main(['-vs', '-k', 'up', './test_2.py'])
-x 遇到错误时停止
pytest.main(['-vs','-x', './test_stop.py'])
--maxfail=num 失败数量达到num个时停止
pytest.main(['-vs', '--maxfail=2', './test_stop.py'])
-n=num 开启num个线程去执行用例 (需要安装 pytest-xdist)
pytest.main(['-vs','./test_thread.py','-n=2'])
--reruns=num 设置失败重跑次数 如果存在用例失败,用例失败后重跑的次数num
pytest.main(['-vs', './test_fail.py', '--reruns=2'])
按执行的范围去设置重跑次数(比如某个类中或者某个方法失败了重跑几次):
pytest.main(['-vs', './test_fail2.py::TestMycase2', '--reruns=2']
执行顺序
pytest 默认允许是从上到下的顺序去执行
但是凡事总有例外
pytest 支持使用装饰器 去设置测试用例的执行顺序
@pytest.mark.run(order=num)
import pytest ''' pytest时按从上到下的顺序去执行的 可以通过mark标记 改变 模块中的测试方法的执行顺序 ''' @pytest.mark.run(order=3) def test_3(): print('test_3') @pytest.mark.run(order=2) def test_1(): print('test_1') @pytest.mark.run(order=1) def test_2(): print('test_2') if __name__ == '__main__': pytest.main(['./test_顺序.py'])
运行结果
============================= 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 plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.4.0 collecting ... collected 3 items test_顺序.py::test_2 test_2 PASSED test_顺序.py::test_1 test_1 PASSED test_顺序.py::test_3 test_3 PASSED ============================== warnings summary =============================== ..\..\Python\lib\site-packages\_pytest\config\__init__.py:1233 D:\Python\lib\site-packages\_pytest\config\__init__.py:1233: PytestConfigWarning: Unknown config option: marks self._warn_or_fail_if_strict(f"Unknown config option: {key}\n") -- Docs: https://docs.pytest.org/en/stable/warnings.html ======================== 3 passed, 1 warning in 0.02s =========================
跳过用例
skip
skipif
通过skip关键字,我们可以跳过指定用例
import pytest class Test_1: def test_1(self): print('test_1') def test_2(self): print('test_2') def test_3(self): print('test_3') class Test_2: num = 10 @pytest.mark.skipif(num>=10,reason='大于10,所以跳过') def test_21(self): print('test_21') @pytest.mark.skip(reason='无条件跳过') def test_22(self): assert 1==2 def test_23(self): print('test_23') if __name__ == '__main__': pytest.main(['-vs','./test_跳过.py'])
执行结果:
============================= 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\pytest_demo66\pytest_跳过 plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.4.0 collecting ... collected 6 items test_跳过.py::Test_1::test_1 PASSED [ 16%]test_1 test_跳过.py::Test_1::test_2 PASSED [ 33%]test_2 test_跳过.py::Test_1::test_3 PASSED [ 50%]test_3 test_跳过.py::Test_2::test_21 SKIPPED (大于10,所以跳过) [ 66%] Skipped: 大于10,所以跳过 test_跳过.py::Test_2::test_22 SKIPPED (无条件跳过) [ 83%] Skipped: 无条件跳过 test_跳过.py::Test_2::test_23 PASSED [100%]test_23 ======================== 4 passed, 2 skipped in 0.03s ========================= Process finished with exit code 0