Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何自定义使用py.test生成的html报告文件?_Python_Html_Pytest - Fatal编程技术网

Python 如何自定义使用py.test生成的html报告文件?

Python 如何自定义使用py.test生成的html报告文件?,python,html,pytest,Python,Html,Pytest,我正在尝试使用pytest自定义html报告。 例如,如果我有如下目录结构: tests temp1 test_temp1.py conftest.py tests目录中还有一个conftest.py文件,它应该是tests目录中所有子目录的公用文件。 我可以在conftest.py中使用哪些fixture和hookwrappers来更改使用以下命令生成的html文件的内容: py.test tests/temp1/test_temp1.py--html=re

我正在尝试使用pytest自定义html报告。 例如,如果我有如下目录结构:

tests
    temp1
         test_temp1.py
    conftest.py
tests目录中还有一个conftest.py文件,它应该是tests目录中所有子目录的公用文件。 我可以在conftest.py中使用哪些fixture和hookwrappers来更改使用以下命令生成的html文件的内容:

py.test tests/temp1/test_temp1.py--html=report.html


看起来您正在使用类似pytest html的插件。 如果是这种情况,请检查该插件的文档,了解所有挂钩都提供了什么

对于pytest html,提供了以下挂钩 您可以通过修改fixture中的
request.config.\u html.Environment
来添加或更改报告的环境部分:

@pytest.fixture(autouse=True)
def _environment(request):
    request.config._environment.append(('foo', 'bar'))
通过在报表对象上创建“额外”列表,可以向HTML报表添加详细信息。下面的示例使用
pytest\u runtest\u 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
导入pytest
@pytest.mark.hookwrapper
def pytest_runtest_makereport(项目,调用):
pytest_html=item.config.pluginmanager.getplugin('html'))
结果=产量
报告=结果。获取结果()
extra=getattr(报告“extra”和[])
如果report.when==“调用”:
#始终将url添加到报告
附加(pytest_html.extras.url('http://www.example.com/'))
xfail=hasattr(报告“wasxfail”)
如果(report.skipped和xfail)或(report.failed和not xfail):
#仅在失败时添加其他html
append(pytest_html.extras.html('Additional html'))
report.extra=额外

更新:在最新版本中,如果要修改html报告中的环境表,请添加到
conftest.py
下一个代码:

@pytest.fixture(scope='session', autouse=True)
def configure_html_report_env(request)
    request.config._metadata.update(
        {'foo': 'bar'}
    )

你在使用pytest html插件吗?你能找到解决方案吗?我正在寻找解决方案。谢谢,我试过了,效果很好。但是,如果我想添加比现在更多的html元素呢?例如,我想向现有的表和列添加表和列。我在哪里可以获得更多关于它的详细信息?代码在下面的位置,您可以根据需要修改或重写这些函数
Python27\Lib\site packages\pytest\u html
saurabh baid,是否确保
request.config.\u environment.append(('foo','bar'))
能够修改环境表?在最新版本的pytest html中,nor
config
nor
HTMLReport
具有
\u环境
attr@Alex-博格达诺夫这是在那里直到1.12.0。谢谢你的加入。缺少第二行的
(用于定义函数)。