问题描述
我想在所有测试结束时获得所有测试的列表(例如以 py.test TestReport 的形式).
我知道 pytest_runtest_makereport
做了类似的事情,但只针对单个测试.但是我想在 conftest.py
中实现一个钩子或其他东西来在 py.test
应用程序终止之前处理整个测试列表.
有没有办法做到这一点?
这里有一个可以帮助您的示例.文件结构:
/示例:__init__.py # 空文件/test_pack_1__init__.py # 空文件conftest.py # pytest 钩子test_my.py # 一些演示测试
test_my.py
中有 2 个测试:
def test_one():断言 1 == 1打印('1==1')def test_two():断言 1 == 2打印('1!=2')
conftest.py
示例:
导入pytest从 _pytest.runner 导入 TestReport从 _pytest.terminal 导入 [email protected](hookwrapper=True)def pytest_terminal_summary(terminalreporter): # type: (TerminalReporter) ->发电机屈服# 你可以在这里做任何事情 - 我只是打印报告信息打印('*' * 8 + '这里自定义逻辑' + '*' * 8)for failed in terminalreporter.stats.get('failed', []): # type: TestReportprint('failed! node_id:%s, duration: %s, details: %s' % (failed.nodeid,失败.持续时间,str(failed.longrepr)))for pass in terminalreporter.stats.get('passed', []): # type: TestReportprint('passed! node_id:%s, duration: %s, details: %s' % (passed.nodeid,通过.duration,str(passed.longrepr)))
文档说 pytest_terminal_summary 有 exitstatus arg
在没有任何附加选项的情况下运行测试:py.test ./example
.输出示例:
example/test_pack_1/test_my.py .F********此处自定义逻辑********失败的!node_id:test_pack_1/test_my.py::test_two,持续时间:0.000385999679565,细节:def test_two():>断言 1 == 2E 断言 1 == 2示例/test_pack_1/test_my.py:7: 断言错误通过!node_id:test_pack_1/test_my.py::test_one,持续时间:0.00019907951355,细节:无==================================== 失败 ====================================___________________________________ 测试_二 ___________________________________def test_two():>断言 1 == 2E 断言 1 == 2示例/test_pack_1/test_my.py:7: 断言错误====================== 1 次失败,1 次在 0.01 秒内通过 ======================
希望这会有所帮助.
注意!确保在运行测试之前删除 .pyc 文件
I want to get a list of all tests (e.g. in the form of a py.test TestReport) at the end of all tests.
I know that pytest_runtest_makereport
does something similar, but only for a single test. But I want to implement a hook or something in conftest.py
to process the whole list of tests before the py.test
application terminates.
Is there a way to do this?
Here an example which can help you. Structure of files:
/example:
__init__.py # empty file
/test_pack_1
__init__.py # empty file
conftest.py # pytest hooks
test_my.py # a few tests for demonstration
There are 2 tests in test_my.py
:
def test_one():
assert 1 == 1
print('1==1')
def test_two():
assert 1 == 2
print('1!=2')
Example of 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)))
Run tests without any additional options: py.test ./example
. Example of output:
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 ======================
Hope this helps.
这篇关于如何在 py.test 运行结束时获取 TestReports 列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!