2017-04-05 28 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() 

記住,你可以隨時與項目對象訪問當前的測試,這可能幫助你獲取需要的信息。