可以使用pytest的钩子拦截测试结果报告:
conftest.py
:
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_logreport(report):
yield
# Define when you want to report:
# when=setup/call/teardown,
# fields: .failed/.passed/.skipped
if report.when == 'call' and report.failed:
# Add to the database or an issue tracker or wherever you want.
print(report.longreprtext)
print(report.sections)
print(report.capstdout)
print(report.capstderr)
同样,你可以拦截这些钩子一个在需要的阶段注入你的代码(在某些情况下,与尝试,唯独身边yield
):
pytest_runtest_protocol(item, nextitem)
pytest_runtest_setup(item)
pytest_runtest_call(item)
pytest_runtest_teardown(item, nextitem)
pytest_runtest_makereport(item, call)
pytest_runtest_logreport(report)
了解更多:Writing pytest plugins
所有这一切都可以轻松完成要么作为一个简单的安装库做了一个小小的插件,或者作为一个伪插件conftest.py
,它只是l在其中一个目录中进行测试。