![](/img/trans.png)
[英]In py.test is it possible to report arbitrary values generated in the test runs?
[英]how to customize html report file generated using py.test?
我正在尝试使用 pytest 自定义 html 报告。 例如,如果我有一个目录结构,如:
tests
temp1
test_temp1.py
conftest.py
一个 conftest.py 文件也在测试目录中,它应该对测试目录中的所有子目录是通用的。 我可以在 conftest.py 中使用哪些夹具和钩子包装器来更改使用以下命令生成的 html 文件的内容:
py.test 测试/temp1/test_temp1.py --html=report.html
更新:在最新版本中,如果您想修改 html 报告中的环境表,请添加到您的conftest.py
下一个代码:
@pytest.fixture(scope='session', autouse=True)
def configure_html_report_env(request)
request.config._metadata.update(
{'foo': 'bar'}
)
看起来您正在使用诸如 pytest-html 之类的插件。 如果是这种情况,请检查该插件的文档以了解提供的所有钩子。
为 pytest-html 提供了以下钩子您可以通过从夹具修改request.config._html.environment
来添加更改报告的环境部分:
@pytest.fixture(autouse=True)
def _environment(request):
request.config._environment.append(('foo', 'bar'))
您可以通过在报告对象上创建“额外”列表来向 HTML 报告添加详细信息。 以下示例使用pytest_runtest_makereport
钩子添加各种类型的附加功能,可以在插件或conftest.py
文件中实现:
import pytest
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call':
# always add url to report
extra.append(pytest_html.extras.url('http://www.example.com/'))
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
# only add additional html on failure
extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
report.extra = extra
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.