1. # 仅收集测试,不执行
    2. pytest --collect-only
    3. # 最小化输出
    4. pytest -q
    5. # 增加冗余度,显示更多信息
    6. pytest -v
    7. # 修改结果显示方式
    8. pytest -r CHAR
    9. # 修改traceback打印方式
    10. pytest --tb=long
    11. # 禁用traceback局部变量
    12. pytest --no-showlocals
    13. # 显示附加的汇总报告
    14. pytest --report
    15. # 报告所有测试状态更改
    16. pytest --report-frailties
    17. # 显示测试汇总
    18. pytest --report-summary
    19. # 显示每个测试调用的详细信息
    20. pytest --report-calls
    21. # 显示测试持续时间
    22. pytest --report-time
    23. # 限制报告的项目(passed, skipped, failed, errors)
    24. pytest --report-only=passed
    25. # 不显示多个测试文件的状态行和汇总
    26. pytest --no-header
    27. # 禁用警告摘要报告
    28. pytest -W ignore
    29. # 在第一个失败后停止测试
    30. pytest -x
    31. # 在多个失败后停止测试
    32. pytest --maxfail=2
    33. # 未知标记时报错
    34. pytest --strict-markers
    35. # 未知配置值时报错
    36. pytest --strict-config
    37. # 强制测试即使存在收集错误
    38. pytest --continue-on-collection-errors
    39. # 定义测试根目录
    40. pytest --rootdir=/path/to/root/dir
    41. # 控制assertion调试工具
    42. pytest --assert=plain
    43. # 在错误或KeyboardInterrupt时启动交互式Python调试器
    44. pytest --pdb
    45. # 跟踪conftest.py文件的考虑事项
    46. pytest --trace-config
    47. # 将断言消息写入日志而不是标准输出
    48. pytest --assert-passed-to-logs
    49. # 仅安装fixtures,不执行测试
    50. pytest --setup-only
    51. # 在执行测试时显示fixtures的安装
    52. pytest --setup-show
    53. # 显示将执行的fixtures和测试,但不执行任何操作
    54. pytest --setup-plan
    55. # 安装fixtures的超时时间
    56. pytest --setup-timeout=3
    57. # 拆卸fixtures时抛弃已执行的fixtures
    58. pytest --setup-discard=2
    59. # 设置fixtures的默认作用域
    60. pytest --setup-scope=function
    61. # 递归fixture设置的最大深度
    62. pytest --setup-depth=2
    63. # 设置安装fixtures的顺序,默认是"先到先得"
    64. pytest --setup-order=scoped
    65. # 设置fixture的最长启动时间
    66. pytest --setup-start-timeout=3
    67. # 在fixture设置过程中显示更详细的输出
    68. pytest --setup-debug
    69. # 拆卸fixture的超时时间
    70. pytest --teardown-timeout=3
    71. # 拆卸fixtures时抛弃已执行的fixtures
    72. pytest --teardown-discard=2
    73. # 设置fixture的作用域
    74. 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



    运行示例:

    1. # 运行所有测试文件,增加冗余度,显示更多信息
    2. pytest -v
    3. # 运行指定的测试文件并显示每个测试调用的详细信息
    4. pytest test_sample.py --report-calls
    5. # 运行指定目录下所有测试文件,停止在第一个失败后
    6. pytest /path/to/tests/directory -x
    7. # 运行指定的测试文件,启动交互式Python调试器
    8. pytest test_sample.py --pdb
    9. # 运行指定的测试文件,并显示fixtures的安装
    10. pytest test_sample.py --setup-show
    11. # 运行指定的测试文件,并安装fixtures的超时时间
    12. pytest test_sample.py --setup-timeout=3
    13. # 运行指定的测试文件,并设置fixture的最长启动时间
    14. pytest test_sample.py --setup-start-timeout=3

    运行结果

    1. # 运行所有测试文件,增加冗余度,显示更多信息
    2. pytest -v
    3. ================================== test session starts ==================================
    4. platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3
    5. cachedir: .pytest_cache
    6. rootdir: /path/to/project
    7. collected X items
    8. test_sample.py::test_function PASSED [ 33%]
    9. test_sample.py::test_another_function PASSED [ 66%]
    10. test_sample.py::test_yet_another_function PASSED [100%]
    11. ================================== X passed in Xs ===================================
    12. # 运行指定的测试文件并显示每个测试调用的详细信息
    13. pytest test_sample.py --report-calls
    14. ================================== test session starts ==================================
    15. platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3
    16. cachedir: .pytest_cache
    17. rootdir: /path/to/project
    18. collected X items
    19. test_sample.py
    20. test_function
    21. call
    22. test_another_function
    23. call
    24. test_yet_another_function
    25. call
    26. ================================== X passed in Xs ===================================
    27. # 运行指定目录下所有测试文件,停止在第一个失败后
    28. pytest /path/to/tests/directory -x
    29. ================================== test session starts ==================================
    30. platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3
    31. cachedir: .pytest_cache
    32. rootdir: /path/to/tests/directory
    33. collected X items
    34. test_sample.py::test_function PASSED [ 50%]
    35. test_sample.py::test_another_function FAILED [100%]
    36. ====================================== FAILURES =======================================
    37. _________________________________ test_another_function ______________________________
    38. def test_another_function():
    39. > assert foo() == 3
    40. E assert 2 == 3
    41. E + where 2 = foo()
    42. test_sample.py:8: AssertionError
    43. ================================== X failed, X passed in Xs ==================================
    44. # 运行指定的测试文件,并显示fixtures的安装
    45. pytest test_sample.py --setup-show
    46. ================================== test session starts ==================================
    47. platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3
    48. cachedir: .pytest_cache
    49. rootdir: /path/to/project
    50. collected X items
    51. test_sample.py
    52. test_function
    53. SETUP F
    54. CALL F
    55. test_another_function
    56. SETUP S
    57. CALL S
    58. test_yet_another_function
    59. SETUP S
    60. CALL S
    61. ================================== FAILURES =======================================
    62. _______________________________ test_function _______________________________
    63. def test_function():
    64. > assert foo() == 3
    65. E assert 2 == 3
    66. E + where 2 = foo()
    67. test_sample.py:4: AssertionError
    68. ------------------------------- Captured stdout setup --------------------------------
    69. Fixture "some_fixture" called.
    70. --------------------------------- Captured log setup ---------------------------------
    71. INFO root:Running setup of test_function
    72. --------------------------------- Captured log call ----------------------------------
    73. INFO root:Running call of test_function
    74. ================================== X failed, X passed in Xs ==================================
    75. # 运行指定的测试文件,并安装fixtures的超时时间
    76. pytest test_sample.py --setup-timeout=3
    77. ================================== test session starts ==================================
    78. platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3
    79. cachedir: .pytest_cache
    80. rootdir: /path/to/project
    81. collected X items
    82. test_sample.py::test_function PASSED [ 33%]
    83. test_sample.py::test_another_function PASSED [ 66%]
    84. test_sample.py::test_yet_another_function PASSED [100%]
    85. ================================== X passed in Xs ===================================
    86. # 运行指定的测试文件,并设置fixture的最长启动时间
    87. pytest test_sample.py --setup-start-timeout=3
    88. ================================== test session starts ==================================
    89. platform linux -- Python 3.x.x, pytest-6.x.x, py-1.x.x, pluggy-1.x.x -- /usr/bin/python3
    90. cachedir: .pytest_cache
    91. rootdir: /path/to/project
    92. collected X items
    93. test_sample.py::test_function PASSED [ 33%]
    94. test_sample.py::test_another_function PASSED [ 66%]
    95. test_sample.py::test_yet_another_function PASSED [100%]
    96. ================================== X passed in Xs ===================================