# 仅收集测试,不执行pytest --collect-only# 最小化输出pytest -q# 增加冗余度,显示更多信息pytest -v# 修改结果显示方式pytest -r CHAR# 修改traceback打印方式pytest --tb=long# 禁用traceback局部变量pytest --no-showlocals# 显示附加的汇总报告pytest --report# 报告所有测试状态更改pytest --report-frailties# 显示测试汇总pytest --report-summary# 显示每个测试调用的详细信息pytest --report-calls# 显示测试持续时间pytest --report-time# 限制报告的项目(passed, skipped, failed, errors)pytest --report-only=passed# 不显示多个测试文件的状态行和汇总pytest --no-header# 禁用警告摘要报告pytest -W ignore# 在第一个失败后停止测试pytest -x# 在多个失败后停止测试pytest --maxfail=2# 未知标记时报错pytest --strict-markers# 未知配置值时报错pytest --strict-config# 强制测试即使存在收集错误pytest --continue-on-collection-errors# 定义测试根目录pytest --rootdir=/path/to/root/dir# 控制assertion调试工具pytest --assert=plain# 在错误或KeyboardInterrupt时启动交互式Python调试器pytest --pdb# 跟踪conftest.py文件的考虑事项pytest --trace-config# 将断言消息写入日志而不是标准输出pytest --assert-passed-to-logs# 仅安装fixtures,不执行测试pytest --setup-only# 在执行测试时显示fixtures的安装pytest --setup-show# 显示将执行的fixtures和测试,但不执行任何操作pytest --setup-plan# 安装fixtures的超时时间pytest --setup-timeout=3# 拆卸fixtures时抛弃已执行的fixturespytest --setup-discard=2# 设置fixtures的默认作用域pytest --setup-scope=function# 递归fixture设置的最大深度pytest --setup-depth=2# 设置安装fixtures的顺序,默认是"先到先得"pytest --setup-order=scoped# 设置fixture的最长启动时间pytest --setup-start-timeout=3# 在fixture设置过程中显示更详细的输出pytest --setup-debug# 拆卸fixture的超时时间pytest --teardown-timeout=3# 拆卸fixtures时抛弃已执行的fixturespytest --teardown-discard=2# 设置fixture的作用域pytest --teardown-scope=function
| 参数名 | 缩写 | 解释 | 示例 |
|---|---|---|---|
| —help | -h | 查看pytest命令的帮助信息 | pytest -h |
| —version | -V | 查看pytest的版本信息 | pytest -V |
| —verbose | -v | 输出详细的测试结果 | pytest -v |
| —quiet | -q | 输出简洁的测试结果 | pytest -q |
| —exitfirst | -x | 遇到测试用例失败即停止执行 | pytest -x |
| —maxfail=num | 在测试用例失败次数达到指定次数后停止执行 | pytest —maxfail=2 | |
| —last-failed | —lf | 仅重新运行上次失败的测试用例 | pytest —lf |
| —failed-first | —ff | 仅重新运行最后一个失败的测试用例 | pytest —ff |
| —pdb | 在测试用例失败时进入Python调试器 | pytest —pdb | |
| —trace | 启用跟踪模式,输出用于调试的堆栈跟踪信息 | pytest —trace | |
| —disable-warnings | 禁用pytest的警告信息 | pytest —disable-warnings | |
| —strict | 遇到未捕获的异常即停止执行 | pytest —strict | |
| —assert | 控制断言的方式,可选项有 plain、rewrite 和 raisin | pytest —assert=plain | |
| —pdbcls | 设置调试器的类名,用于自定义调试器 | pytest —pdbcls=my_pdb.Pdb | |
| —capture | 控制标准输出的捕获方式,可选项有 no、sys 和 fd | pytest —capture=no | |
| —color | 控制控制台输出的颜色,可选项有 yes、no 和 auto | pytest —color=yes | |
| —full-trace | 输出详细的堆栈跟踪信息 | pytest —full-trace | |
| —rootdir=path | 指定pytest的根目录 | pytest —rootdir=/path/to/root | |
| —ignore=path | 忽略指定路径下的测试文件 | pytest —ignore=/path/to/test_file.py | |
| —junitxml=path | 生成JUnitXML格式的测试结果报告 | pytest —junitxml=/path/to/report.xml | |
| —cov=module | 生成代码覆盖率报告 | pytest —cov=my_module | |
| —cov-report=format | 设置代码覆盖率报告的格式,可选项有 term、html、xml | pytest —cov-report=format | |
| —tb=style | 指定输出失败测试用例时使用的traceback的样式 | pytest —tb=short | |
| —capture=method | 指定标准输出的捕获方式 | pytest —capture=sys | |
| —color=mode | 指定控制台输出的颜色样式 | pytest —color=yes | |
| —noconftest | 禁用所有conftest.py文件 | pytest —noconftest | |
| —strict | 在导入的Python模块中启用严格模式 | pytest —strict | |
| —disable-pytest-warnings | 禁用pytest自带的警告信息 | pytest —disable-pytest-warnings | |
| —collect-only | 仅收集测试,不执行 | pytest —collect-only |
运行示例:
# 运行所有测试文件,增加冗余度,显示更多信息pytest -v# 运行指定的测试文件并显示每个测试调用的详细信息pytest test_sample.py --report-calls# 运行指定目录下所有测试文件,停止在第一个失败后pytest /path/to/tests/directory -x# 运行指定的测试文件,启动交互式Python调试器pytest test_sample.py --pdb# 运行指定的测试文件,并显示fixtures的安装pytest test_sample.py --setup-show# 运行指定的测试文件,并安装fixtures的超时时间pytest test_sample.py --setup-timeout=3# 运行指定的测试文件,并设置fixture的最长启动时间pytest test_sample.py --setup-start-timeout=3
运行结果
# 运行所有测试文件,增加冗余度,显示更多信息pytest -v================================== test session starts ==================================platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3cachedir: .pytest_cacherootdir: /path/to/projectcollected X itemstest_sample.py::test_function PASSED [ 33%]test_sample.py::test_another_function PASSED [ 66%]test_sample.py::test_yet_another_function PASSED [100%]================================== X passed in Xs ===================================# 运行指定的测试文件并显示每个测试调用的详细信息pytest test_sample.py --report-calls================================== test session starts ==================================platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3cachedir: .pytest_cacherootdir: /path/to/projectcollected X itemstest_sample.pytest_functioncalltest_another_functioncalltest_yet_another_functioncall================================== X passed in Xs ===================================# 运行指定目录下所有测试文件,停止在第一个失败后pytest /path/to/tests/directory -x================================== test session starts ==================================platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3cachedir: .pytest_cacherootdir: /path/to/tests/directorycollected X itemstest_sample.py::test_function PASSED [ 50%]test_sample.py::test_another_function FAILED [100%]====================================== FAILURES =======================================_________________________________ test_another_function ______________________________def test_another_function():> assert foo() == 3E assert 2 == 3E + where 2 = foo()test_sample.py:8: AssertionError================================== X failed, X passed in Xs ==================================# 运行指定的测试文件,并显示fixtures的安装pytest test_sample.py --setup-show================================== test session starts ==================================platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3cachedir: .pytest_cacherootdir: /path/to/projectcollected X itemstest_sample.pytest_functionSETUP FCALL Ftest_another_functionSETUP SCALL Stest_yet_another_functionSETUP SCALL S================================== FAILURES =======================================_______________________________ test_function _______________________________def test_function():> assert foo() == 3E assert 2 == 3E + where 2 = foo()test_sample.py:4: AssertionError------------------------------- Captured stdout setup --------------------------------Fixture "some_fixture" called.--------------------------------- Captured log setup ---------------------------------INFO root:Running setup of test_function--------------------------------- Captured log call ----------------------------------INFO root:Running call of test_function================================== X failed, X passed in Xs ==================================# 运行指定的测试文件,并安装fixtures的超时时间pytest test_sample.py --setup-timeout=3================================== test session starts ==================================platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3cachedir: .pytest_cacherootdir: /path/to/projectcollected X itemstest_sample.py::test_function PASSED [ 33%]test_sample.py::test_another_function PASSED [ 66%]test_sample.py::test_yet_another_function PASSED [100%]================================== X passed in Xs ===================================# 运行指定的测试文件,并设置fixture的最长启动时间pytest test_sample.py --setup-start-timeout=3================================== test session starts ==================================platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3cachedir: .pytest_cacherootdir: /path/to/projectcollected X itemstest_sample.py::test_function PASSED [ 33%]test_sample.py::test_another_function PASSED [ 66%]test_sample.py::test_yet_another_function PASSED [100%]================================== X passed in Xs ===================================
