一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第2天,点击查看活动详情。
感谢点赞和一起学习。关注我,每天学习一点点。
- [使用python -m pytest调用pytest]
- [可能出现的执行退出code]
- [获取版本路径、命令行选项及环境变量相关帮助]
- [第1(N)次失败后停止测试]
- [指定及选择测试用例]
- [创建JUnit XML格式的测试报告]
- [在Python代码调用pytest]
使用python -m pytest调用pytest
上篇文章我们说过,python -m pytest [...]
来运行测试用例。它的几乎等同于命令行直接调用pytest [...]
。
可能出现的执行退出code
我们在执行测试用例的时候,每个case会返回不同的状态码,这个状态码跟咱们的http的状态码大同小异。主要是看是否执行成功。总共有6个状态码。如下:
- 退出code 0: 收集并成功通过所有测试用例
- 退出code 1: 收集并运行了测试,部分测试用例执行失败
- 退出code 2: 测试执行被用户中断
- 退出code 3: 执行测试中发生内部错误
- 退出code 4: pytest命令行使用错误
- 退出code 5: 没有收集到测试用例
获取版本路径、命令行选项及环境变量相关帮助
第1(N)次失败后停止测试
pytest -x # 第1次失败后停止
pytest --maxfail=2 # 2次失败后停止,可以是2,也可以是n
复制代码
指定及选择测试用例
pytest test_model.py
运行模块内所有的case
pytest testfile
运行目录下符合要去的case
pytest -k "key"
运行符合表达式的case
pytest test_model.py::test_func
运行模块中指定的case
pytest test_model.py::TestClass::test_func
运行模块中指定的case
pytest -m slow
运行case带@pytest.mark.slow装饰器的case
pytest --pyargs pkg.test
运行包中的case
创建JUnit XML格式的测试报告
要创建可由Jenkins或其他持续集成软件读取的XML测试报告,可以使用:pytest --junutxml=path
path是路径文件.
在Python代码调用pytest
这个就比较简单了。 pytest.main(['-x','mytestdir'])