简体   繁体   English

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

[英]Pytest-html: Customize report title based on marks

I want to indicate the pytest test execution marks in the pytest-html report title I am trying the approach as shown below.我想在 pytest-html 报告标题中指明 pytest 测试执行标记我正在尝试如下所示的方法。 It does not work.这没用。

conftest.py 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"

The problem is solved by adding label as an attribute to pytest in pytest_configure hook:通过在 pytest_configure 钩子中将标签作为属性添加到 pytest 来解决该问题:

#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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM