How to add additional variable to pytest html report

强颜欢笑 提交于 2019-12-07 11:52:24

问题


I am using pytest HTML report plugin for my selenium tests. It works great with just passing test.py --html==report.htmlin command line and generates a great report.

I also need to implement additional string/variable to each test case display. It doesn't matter if it's pass or failed it should just show "ticket number". This ticket id I can return in each test scenario.

I could just add ticket number to test name, but it will look ugly.

Please advise what's the best way to do it.

Thank you.


回答1:


You can insert custom html for each test by either adding html content to the "show details" section of each test, or customizing the result table (e.g. add a ticket column).

The first possibility is the simplest, you can just add the following to your 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

Where you can replace <p>some html</p> with your content.

The second solution would be:

@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()

Remember that you can always access the current test with the item object, this might help retrieving the information you need.



来源:https://stackoverflow.com/questions/43241640/how-to-add-additional-variable-to-pytest-html-report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!