Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add additional variable to pytest html report

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.

like image 707
olia Avatar asked Apr 05 '17 21:04

olia


1 Answers

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.

like image 143
gbonetti Avatar answered Sep 21 '22 01:09

gbonetti