Pytesthtml自定义

2024-05-13 18:57:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pytesthtml插件为我的测试用例生成报告。如果脚本失败,我想添加一个行项目。这是我的密码——

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

def test_sayHello():

        assert False, "I mean for this to fail"
        print "hello"


def test_sayBye():
        print "Bye"


I am running the scipt using -
 pytest --html=report.html

我可以看到生成的报告,但它没有附加的HTML行项目。在

还有一种方法可以让我在脚本之间添加这样的行项目到报表中。在

非常感谢你的帮助。在


Tags: 项目report脚本addurlifpytestdef
1条回答
网友
1楼 · 发布于 2024-05-13 18:57:22

这应该是有效的:

@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':
        extra.append(pytest_html.extras.html('<p>some html</p>'))
        report.extra = extra

相关问题 更多 >