微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pytest-html:根据标记自定义报告标题

如何解决Pytest-html:根据标记自定义报告标题

我想在pytest-html报告标题中注明pytest测试执行标记 我正在尝试如下所示的方法。它不起作用。

conftest.py

#add test execution mark as a 'label' field to the report
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item,call):
    pytest_html = item.config.pluginmanager.getplugin("html")
    outcome = yield
    report = outcome.get_result()
    if item.config.getoption("-m"):
        report.label = item.config.getoption("-m")

#add 'label' field to the title
def pytest_html_report_title(report):
    mark = report.label if hasattr(report,"label") else ""
    report.title = f"My Software: {mark} Test Report"

解决方法

问题通过在pytest_configure钩子中为pytest添加label作为属性解决:

#add test execution mark as a 'label' field to the pytest namespace
def pytest_configure(config):
    pytest.label = config.getoption("-m")

#add 'label' field to the title
def pytest_html_report_title(report):
    report.title = f"My Software: {pytest.label} Test Report"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。