2017-04-05 33 views
2

我使用pytest HTML报告插件为我的硒测试。 它只是通过test.py --html==report.htmlin命令行很好用,并生成一个很棒的报告。如何添加额外的变量pytest html报告

我还需要为每个测试用例显示实现附加的字符串/变量。无论是通过还是失败都不要紧,只需显示“门票号码”即可。我可以在每个测试场景中返回此票证号。

我可以添加票号来测试名称,但它看起来很丑。

请告知什么是最好的办法。

谢谢。

回答

3

您可以为每个测试插入自定义html,方法是将html内容添加到每个测试的“显示详细信息”部分,或者自定义结果表(例如添加一个票据列)。

第一种可能是最简单的,你可以添加以下到您的conftest.py

@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 

在这里您可以与您的内容替换<p>some html</p>

第二个解决办法是:

@pytest.mark.optionalhook 
def pytest_html_results_table_header(cells): 
    cells.insert(1, html.th('Ticket')) 


@pytest.mark.optionalhook 
def pytest_html_results_table_row(report, cells): 
    cells.insert(1, html.td(report.ticket)) 


@pytest.mark.hookwrapper 
def pytest_runtest_makereport(item, call): 
    outcome = yield 
    report = outcome.get_result() 
    report.ticket = some_function_that_gets_your_ticket_number() 

记住,你可以随时与项目对象访问当前的测试,这可能帮助你获取需要的信息。