pytest的命名规律与运行方式_byseyOrd
命名规则
#文件名以test_开头或以_test结尾 test_* *_test
类名以Test开头,不能带init方法
方法和函数以test_开头
test_
pytest主要使用命令行来运行,也可使用配置好pytest解析环境的pycharm来运行
命令行运行pytest的命令
切换到源码目录后,输入
py.test #或者 python -m pytest
pytest会自动检索当前目录下的test开头或以test结尾的py文件,运行其中符合命名规则的测试用例
要运行部分文件的时候
py.test -q test_one.py pytest -q test_one.py python -m pytest -q test_one.py
要运行部分用例的时候
pytest test_mod.py::TestClass::test_method
要运行指定用例的时候
#运行指定用例,在某用例上使用@pytest.mark.slow标记 #命令行中写入 pytest - m slow
命令行运行pytest的参数
控制参数
-q简单结果
-v详细结果
-x遇到错误停止过程
-k希望执行test_aaa()和test_bbb()这两个测试用例,那么可以用表达式"aaa or bbb"来筛选。结合-v查看是否符合预期
–maxfail=num 错误用例达到次数停止
功能参数
--html = report.html --self-contained-html #html带样式的报告的输出
--html = report.html #html与样式分离的报告的输出
--pyargs pkg_test #导入pkg_test包,使用系统位置查找运行测试文件并运行
--collect-only #打印被运行的用例名而不是运行的py文件
浙公网安备 33010602011771号