运行pytest会导致六个不同的退出代码:
退出代码0: 所有测试均已收集并成功通过
退出代码1: 测试已收集并运行,但有些测试失败
退出代码2: 测试执行被用户中断
退出代码3: 执行测试时发生内部错误
退出代码4: pytest命令行用法错误
退出代码5: 没有收集测试
pytest --version # 显示从哪里导入pytest
pytest --fixtures # 显示可用的内置函数参数
pytest -h | --help # 在命令行和配置文件选项上显示帮助
pytest # 测试当前目录下的test_*.py全部文件
pytest demo/ # 测试选中的目录
pytest -q test_sysexit.py # 指定测试test_sysexit.py
pytest -x # 随机测试一次失败后停止
pytest --maxfail=2 # 随机测试两次失败后停止
pytest test_sample.py::test_answer # 按函数节点ID运行测试
pytest test_class.py::TestClass::test_two # 按类里的方法节点ID运行测试
pytest -m slow # 在全部文件写下,将运行用@pytest.mark.slow装饰器装饰的所有测试。
pytest test_example.py -m errorcase # 在指定文件下,将运行用@pytest.mark.errorcase装饰器装饰的所有测试。
pytest -m 'run_these_test' test_example.py # 指定文件,根据标签运行测试对象
pytest -k "MyClass and not method"#通过关键字表达式运行测试
pytest --showlocals # show local variables in tracebacks
pytest -l # show local variables (shortcut)
pytest --tb=auto # (default) 'long' tracebacks for the first and last
# entry, but 'short' style for the other entries
pytest --tb=long # exhaustive, informative traceback formatting
pytest --tb=short # shorter traceback format
pytest --tb=line # only one line per failure
pytest --tb=native # Python standard library formatting
pytest --tb=no # no traceback at all
pytest --pyargs pkg.testing
pytest -ra # 测试全部详细的总结报告(-r选项后面接受多个字符)
pytest -q test_example.py -rfs # 仅查看失败和跳过的测试
f -失败
E -错误
s -跳过
x -xfailed
X -xpass
p -通过
P -通过输出
a -除通行证外的所有字符
A -全部
pytest --trace # 允许您在每次测试开始时通过命令行选项立即进入PDB提示符
pytest -s --collect-only # 打印要执行的测试对象
pytest -k "user" # 测试对象名中包含”user“的对象
pytest -q # 打印出测试的简略情况
pytest -q test_example.py -l/--showlocals # 如果测试出错就打印局部变量
pytest -l/--showlocals -q test_example.py # 如果测试出错就打印局部变量
pytest -v # 打印出测试的详细情况
pytest -v test_example.py --html=./dd.html # 生成html测试报告
pytest test_example.py --junitxml=junitxml.xml # 生成xml测试报告
# 测试文件函数,每个函数运行3次,每次花费时间4秒
pytest test_example.py --reruns 3 --reruns-delay 4
基础测试语句
最新推荐文章于 2022-06-01 21:37:23 发布