2024-04-19 00:48:42 发布
网友
我想得到所有测试的列表(例如py.测试测试报告)在所有测试结束时。在
我知道pytest_runtest_makereport也做了类似的事情,但只针对单个测试。但是我想在conftest.py中实现一个钩子或其他东西,以便在py.test应用程序终止之前处理整个测试列表。在
pytest_runtest_makereport
conftest.py
py.test
有办法吗?在
这里有一个可以帮助你的例子。文件结构:
/example: __init__.py # empty file /test_pack_1 __init__.py # empty file conftest.py # pytest hooks test_my.py # a few tests for demonstration
在test_my.py中有2个测试:
test_my.py
conftest.py示例:
import pytest from _pytest.runner import TestReport from _pytest.terminal import TerminalReporter @pytest.hookimpl(hookwrapper=True) def pytest_terminal_summary(terminalreporter): # type: (TerminalReporter) -> generator yield # you can do here anything - I just print report info print('*' * 8 + 'HERE CUSTOM LOGIC' + '*' * 8) for failed in terminalreporter.stats.get('failed', []): # type: TestReport print('failed! node_id:%s, duration: %s, details: %s' % (failed.nodeid, failed.duration, str(failed.longrepr))) for passed in terminalreporter.stats.get('passed', []): # type: TestReport print('passed! node_id:%s, duration: %s, details: %s' % (passed.nodeid, passed.duration, str(passed.longrepr)))
Documentation says that pytest_terminal_summary has exitstatus arg
不使用任何附加选项运行测试:py.test ./example。输出示例:
py.test ./example
example/test_pack_1/test_my.py .F ********HERE CUSTOM LOGIC******** failed! node_id:test_pack_1/test_my.py::test_two, duration: 0.000385999679565, details: def test_two(): > assert 1 == 2 E assert 1 == 2 example/test_pack_1/test_my.py:7: AssertionError passed! node_id:test_pack_1/test_my.py::test_one, duration: 0.00019907951355, details: None =================================== FAILURES =================================== ___________________________________ test_two ___________________________________ def test_two(): > assert 1 == 2 E assert 1 == 2 example/test_pack_1/test_my.py:7: AssertionError ====================== 1 failed, 1 passed in 0.01 seconds ======================
希望这有帮助。在
Note! Make sure that .pyc files was removed before running tests
这里有一个可以帮助你的例子。文件结构:
在
^{pr2}$test_my.py
中有2个测试:conftest.py
示例:不使用任何附加选项运行测试:
py.test ./example
。输出示例:希望这有帮助。在
相关问题 更多 >
编程相关推荐