信息输出是测试过程中重要的反馈渠道,它涵盖了用例收集结果、执行状态、失败原因、统计信息等内容。pytest 提供了丰富的参数和插件来控制输出的详细程度、格式和样式。我挑自认为有价值的说一下。
控制输出详细程度
pytest 提供 无参数、-v(详细)、-vv(更详细)、-q(安静)参数来调整输出的详细程度,我们依次了解。
无参数:
无参数时,pytest 默认输出简洁的测试结果,包括用例总数、通过数、失败数和跳过数。
======================================================== test session starts ========================================================
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/ether/projects/video-service-private-network-gateway
collected 3 items
cases/test_a.py .Fs [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_case2 _____________________________________________________________
def test_case2():
x = 1
y = 2
> assert type(x) is not type(y), "x 与 y 的类型不同"
E AssertionError: x 与 y 的类型不同
E assert <class 'int'> is not <class 'int'>
E + where <class 'int'> = type(1)
E + and <class 'int'> = type(2)
cases/test_a.py:10: AssertionError
====================================================== short test summary info ======================================================
FAILED cases/test_a.py::test_case2 - AssertionError: x 与 y 的类型不同
============================================== 1 failed, 1 passed, 1 skipped in 0.04s ===============================================
这种详细程度下,以核心状态符号表示结果。各个核心状态符号的含义如下:
| 符号 | 含义 | 说明 |
|---|---|---|
| . | Passed(通过) | 用例执行成功 |
| F | Failed(失败) | 断言失败或主动抛出异常 |
| E | Error(错误) | 用例执行过程中发生未捕获的异常(如代码错误) |
| s | Skipped(跳过) | 用例被 @pytest.mark.skip 跳过 |
| x | XFailed(预期失败) | 用例被 @pytest.mark.xfail 标记,且确实失败 |
| X | XPassed(意外通过) | 用例被 @pytest.mark.xfail 标记,但实际通过 |
| p | Pending(待定) | 用例被参数化且部分跳过(少见) |
| % | Failed (with warnings) | 用例失败且包含警告 |
-v 参数:
显示详细的测试执行信息,包括每个测试用例的完整名称(模块名、类名、方法名)和结果。
======================================================== test session starts ========================================================
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0 -- /home/ether/projects/video-service-private-network-gateway/.venv/bin/python3
cachedir: .pytest_cache
rootdir: /home/ether/projects/video-service-private-network-gateway
collected 3 items
cases/test_a.py::test_case1 PASSED [ 33%]
cases/test_a.py::test_case2 FAILED [ 66%]
cases/test_a.py::test_case3 SKIPPED (未实现) [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_case2 _____________________________________________________________
def test_case2():
x = 1
y = 2
> assert type(x) is not type(y), "x 与 y 的类型不同"
E AssertionError: x 与 y 的类型不同
E assert <class 'int'> is not <class 'int'>
E + where <class 'int'> = type(1)
E + and <class 'int'> = type(2)
cases/test_a.py:10: AssertionError
====================================================== short test summary info ======================================================
FAILED cases/test_a.py::test_case2 - AssertionError: x 与 y 的类型不同
============================================== 1 failed, 1 passed, 1 skipped in 0.03s ===============================================
-vv 参数:
显示更详细的测试执行信息,包括每个测试用例的完整名称(模块名、类名、方法名)和结果。
======================================================== test session starts ========================================================
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0 -- /home/ether/projects/video-service-private-network-gateway/.venv/bin/python3
cachedir: .pytest_cache
rootdir: /home/ether/projects/video-service-private-network-gateway
collected 3 items
cases/test_a.py::test_case1 PASSED [ 33%]
cases/test_a.py::test_case2 FAILED [ 66%]
cases/test_a.py::test_case3 SKIPPED (未实现) [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_case2 _____________________________________________________________
def test_case2():
x = 1
y = 2
> assert type(x) is not type(y), "x 与 y 的类型不同"
E AssertionError: x 与 y 的类型不同
E assert <class 'int'> is not <class 'int'>
E + where <class 'int'> = type(1)
E + and <class 'int'> = type(2)
cases/test_a.py:10: AssertionError
====================================================== short test summary info ======================================================
FAILED cases/test_a.py::test_case2 - AssertionError: x 与 y 的类型不同
assert <class 'int'> is not <class 'int'>
+ where <class 'int'> = type(1)
+ and <class 'int'> = type(2)
============================================== 1 failed, 1 passed, 1 skipped in 0.03s ===============================================
-q 参数:
显示较与 无参数 情况下,更为简洁的测试执行信息。
.Fs [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_case2 _____________________________________________________________
def test_case2():
x = 1
y = 2
> assert type(x) is not type(y), "x 与 y 的类型不同"
E AssertionError: x 与 y 的类型不同
E assert <class 'int'> is not <class 'int'>
E + where <class 'int'> = type(1)
E + and <class 'int'> = type(2)
cases/test_a.py:10: AssertionError
====================================================== short test summary info ======================================================
FAILED cases/test_a.py::test_case2 - AssertionError: x 与 y 的类型不同
1 failed, 1 passed, 1 skipped in 0.03s
控制输出特定类型测试用例的执行信息
这个参数主要控制 ‘short test summary info’ 里的信息输出。
-rf 参数(默认值):
只输出 ‘FAILED’ 类型的测试用例。
======================================================== test session starts ========================================================
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/ether/projects/video-service-private-network-gateway
collected 4 items
cases/test_a.py .Fsx [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_case2 _____________________________________________________________
def test_case2():
x = 1
y = 2
> assert type(x) is not type(y), "x 与 y 的类型不同"
E AssertionError: x 与 y 的类型不同
E assert <class 'int'> is not <class 'int'>
E + where <class 'int'> = type(1)
E + and <class 'int'> = type(2)
cases/test_a.py:10: AssertionError
====================================================== short test summary info ======================================================
FAILED cases/test_a.py::test_case2 - AssertionError: x 与 y 的类型不同
========================================= 1 failed, 1 passed, 1 skipped, 1 xfailed in 0.04s =========================================
-ra 参数:
输出除 ‘PASSED’ 之外的所有类型的测试用例。
======================================================== test session starts ========================================================
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/ether/projects/video-service-private-network-gateway
collected 4 items
cases/test_a.py .Fsx [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_case2 _____________________________________________________________
def test_case2():
x = 1
y = 2
> assert type(x) is not type(y), "x 与 y 的类型不同"
E AssertionError: x 与 y 的类型不同
E assert <class 'int'> is not <class 'int'>
E + where <class 'int'> = type(1)
E + and <class 'int'> = type(2)
cases/test_a.py:10: AssertionError
====================================================== short test summary info ======================================================
SKIPPED [1] cases/test_a.py:12: 未实现
XFAIL cases/test_a.py::test_case4 - 预期失败
FAILED cases/test_a.py::test_case2 - AssertionError: x 与 y 的类型不同
========================================= 1 failed, 1 passed, 1 skipped, 1 xfailed in 0.04s =========================================
-rA 参数:
输出所有类型的测试用例。
======================================================== test session starts ========================================================
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/ether/projects/video-service-private-network-gateway
collected 4 items
cases/test_a.py .Fsx [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_case2 _____________________________________________________________
def test_case2():
x = 1
y = 2
> assert type(x) is not type(y), "x 与 y 的类型不同"
E AssertionError: x 与 y 的类型不同
E assert <class 'int'> is not <class 'int'>
E + where <class 'int'> = type(1)
E + and <class 'int'> = type(2)
cases/test_a.py:10: AssertionError
============================================================== PASSES ===============================================================
====================================================== short test summary info ======================================================
PASSED cases/test_a.py::test_case1
SKIPPED [1] cases/test_a.py:12: 未实现
XFAIL cases/test_a.py::test_case4 - 预期失败
FAILED cases/test_a.py::test_case2 - AssertionError: x 与 y 的类型不同
========================================= 1 failed, 1 passed, 1 skipped, 1 xfailed in 0.03s =========================================
-rp 参数:
只输出 ‘PASSED’ 类型的测试用例。
======================================================== test session starts ========================================================
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/ether/projects/video-service-private-network-gateway
collected 4 items
cases/test_a.py .Fsx [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_case2 _____________________________________________________________
def test_case2():
x = 1
y = 2
> assert type(x) is not type(y), "x 与 y 的类型不同"
E AssertionError: x 与 y 的类型不同
E assert <class 'int'> is not <class 'int'>
E + where <class 'int'> = type(1)
E + and <class 'int'> = type(2)
cases/test_a.py:10: AssertionError
====================================================== short test summary info ======================================================
PASSED cases/test_a.py::test_case1
========================================= 1 failed, 1 passed, 1 skipped, 1 xfailed in 0.04s =========================================
-rx 参数:
输出 ‘XFAIL’ 类型的测试用例。
======================================================== test session starts ========================================================
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/ether/projects/video-service-private-network-gateway
collected 4 items
cases/test_a.py .Fsx [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_case2 _____________________________________________________________
def test_case2():
x = 1
y = 2
> assert type(x) is not type(y), "x 与 y 的类型不同"
E AssertionError: x 与 y 的类型不同
E assert <class 'int'> is not <class 'int'>
E + where <class 'int'> = type(1)
E + and <class 'int'> = type(2)
cases/test_a.py:10: AssertionError
====================================================== short test summary info ======================================================
XFAIL cases/test_a.py::test_case4 - 预期失败
========================================= 1 failed, 1 passed, 1 skipped, 1 xfailed in 0.03s =========================================
-rs 参数:
输出被 @pytest.mark.skip 装饰的测试用例。
======================================================== test session starts ========================================================
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/ether/projects/video-service-private-network-gateway
collected 4 items
cases/test_a.py .Fsx [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_case2 _____________________________________________________________
def test_case2():
x = 1
y = 2
> assert type(x) is not type(y), "x 与 y 的类型不同"
E AssertionError: x 与 y 的类型不同
E assert <class 'int'> is not <class 'int'>
E + where <class 'int'> = type(1)
E + and <class 'int'> = type(2)
cases/test_a.py:10: AssertionError
====================================================== short test summary info ======================================================
SKIPPED [1] cases/test_a.py:12: 未实现
========================================= 1 failed, 1 passed, 1 skipped, 1 xfailed in 0.03s =========================================
-rn 参数:
不输出任何内容
======================================================== test session starts ========================================================
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/ether/projects/video-service-private-network-gateway
collected 4 items
cases/test_a.py .Fsx [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_case2 _____________________________________________________________
def test_case2():
x = 1
y = 2
> assert type(x) is not type(y), "x 与 y 的类型不同"
E AssertionError: x 与 y 的类型不同
E assert <class 'int'> is not <class 'int'>
E + where <class 'int'> = type(1)
E + and <class 'int'> = type(2)
cases/test_a.py:10: AssertionError
========================================= 1 failed, 1 passed, 1 skipped, 1 xfailed in 0.03s =========================================
读者可以查阅pytest 官方文档中更多信息。
END
© 转载需要保留原始链接,未经明确许可,禁止商业使用。CC BY-NC-ND 4.0