2017-06-01 93 views
2

我正在使用pytest-html插件來生成我的測試用例的報告。如果腳本失敗,我想添加一個訂單項。所以這裏是我的代碼---Pytest-html自定義

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的行項目。

也有一種方法,我可以通過它將我的腳本之間的這些行項添加到報表中。

真的很感謝您的幫助。

回答

0

這應該工作:

@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