通过 python -m pytest 调用 pytest
您可以从命令行通过 Python 解释器调用测试:
python -m pytest [...]
这几乎等同于直接调用命令行脚本 pytest […],只不过通过 python 调用还会将当前目录添加到 sys.path。
可能的退出码
运行 pytest 可能会产生六种不同的退出代码:
- 退出代码 0: 所有测试用例均已收集并成功通过
- 退出代码 1: 收集并运行了测试用例,但有些测试失败了
- 退出代码 2 :测试执行被用户中断
- 退出代码 3 :执行测试时发生内部错误
- 退出代码 4: pytest 命令行使用错误
- 退出代码 5: 未收集到测试用例
它们由 pytest.ExitCode 枚举表示。作为公共 API 一部分的退出代码可以使用以下方法直接导入和访问:
from pytest import ExitCode
Note:如果您想在某些情况下自定义退出代码,特别是在未收集到测试用例时,请考虑使用 pytest-custom_exit_code 插件。
获取有关版本、选项名称、环境变量的帮助
pytest --version # shows where pytest was imported from
pytest --fixtures # show available builtin function arguments
pytest -h | --help # show help on command line and config file options
第一次(或 N 次)失败后停止
在前 (N) 次失败后停止测试过程:
pytest -x # stop after first failure
pytest --maxfail=2 # stop after two failures
指定/选择测试用例集
Pytest 支持多种从命令行运行和选择测试的方法。
pytest test_mod.py
pytest testing/
pytest -k "MyClass and not method"
这将运行包含与给定字符串表达式匹配的名称(不区分大小写)的测试,其中可以包括使用文件名、类名和函数名作为变量的 Python 运算符。上面的示例将运行 TestMyClass.test_something 而不执行 TestMyClass.test_method_simple。
每个被收集的测试用例都分配了一个唯一的 nodeid,它由模块文件名后跟类名、函数名和参数化参数等说明符组成,由 :: 字符分隔。
在模块中运行指定测试:
pytest test_mod.py::test_func
在命令行中指定测试方法的另一个示例:
pytest test_mod.py::TestClass::test_method
pytest -m slow
这将运行所有用 @pytest.mark.slow 装饰器装饰的测试用例。
pytest --pyargs pkg.testing
这将导入 pkg.testing 并使用其文件系统位置来查找和运行测试。
修改 Python 跟踪信息打印
修改跟踪信息打印的示例:
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
–full-trace 导致在打印错误时会有很长的跟踪(长于 –tb=long)。它还确保在 KeyboardInterrupt (Ctrl+C) 上打印堆栈回溯。如果测试花费的时间太长并且您使用 Ctrl+C 中断它们以找出测试挂起的位置,这将非常有用。默认情况下不会显示任何输出(因为 KeyboardInterrupt 被 pytest 捕获)。通过使用此选项,您可以确保显示跟踪信息。
详细总结报告
-r 标志可用于在测试会话结束时显示”简短的测试摘要信息”,以便在大型测试套件中轻松获得所有失败、跳过、xfail 等的清晰图表。
它默认为 fE 以列出失败和错误。
示例:
content of test_example.py
import pytest
@pytest.fixture
def error_fixture():
assert 0
def test_ok():
print("ok")
def test_fail():
assert 0
def test_error(error_fixture):
pass
def test_skip():
pytest.skip("skipping this test")
def test_xfail():
pytest.xfail("xfailing this test")
@pytest.mark.xfail(reason="always xfail")
def test_xpass():
pass
$ pytest -ra
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 6 items
test_example.py .FEsxX [100%]
================================== ERRORS ==================================
_______________________ ERROR at setup of test_error _______________________
@pytest.fixture
def error_fixture():
> assert 0
E assert 0
test_example.py:6: AssertionError
================================= FAILURES =================================
________________________________ test_fail _________________________________
def test_fail():
> assert 0
E assert 0
test_example.py:14: AssertionError
========================= short test summary info ==========================
SKIPPED [1] test_example.py:22: skipping this test
XFAIL test_example.py::test_xfail
reason: xfailing this test
XPASS test_example.py::test_xpass always xfail
ERROR test_example.py::test_error - assert 0
FAILED test_example.py::test_fail - assert 0
== 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12s ===
-r 选项接受其后的多个字符,上面a使用的意思是”除通过之外的所有”。
以下是可以使用的可用字符的完整列表:
f
– failedE
– errors
– skippedx
– xfailedX
– xpassedp
– passedP
– passed with output
用于(取消)选择组的特殊字符:
a
– all exceptpP
A
– allN
– none, this can be used to display nothing (sincefE
is the default)
可以使用多个字符,因此例如仅查看失败和跳过的测试,您可以执行:
$ pytest -rfs
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 6 items
test_example.py .FEsxX [100%]
================================== ERRORS ==================================
_______________________ ERROR at setup of test_error _______________________
@pytest.fixture
def error_fixture():
> assert 0
E assert 0
test_example.py:6: AssertionError
================================= FAILURES =================================
________________________________ test_fail _________________________________
def test_fail():
> assert 0
E assert 0
test_example.py:14: AssertionError
========================= short test summary info ==========================
FAILED test_example.py::test_fail - assert 0
SKIPPED [1] test_example.py:22: skipping this test
== 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12s ===
使用 p 列出通过的测试,而 P 添加一个额外的部分”PASSES”,其中包含那些通过并且已捕获输出的测试:
`python
$ pytest -rpP
=========================== test session starts ============================
platform linux — Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 6 items
test_example.py .FEsxX [100%]
================================== ERRORS ==================================
___ ERROR at setup of test_error ___
@pytest.fixture
def error_fixture():
assert 0
E assert 0
test_example.py:6: AssertionError
================================= FAILURES =================================
_____ testfail _______
def test_fail():
assert 0
E assert 0
test_example.py:14: AssertionError
================================== PASSES ==================================
_____ test_ok ________
Original: https://blog.csdn.net/m0_57581736/article/details/119772540
Author: m0_57581736
Title: Pytest-用法和调用
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/772936/
转载文章受原作者版权保护。转载请注明原作者出处!