pytest-html add custom test results to report

3.9k Views Asked by At

I'm using pytest0html to generate my html report. my test record tested values and I need to show a table of this values with prettytable in case of success. I think that i need to implement this hook:

@pytest.mark.optionalhook
def pytest_html_results_table_html(report, data):
    if report.passed:
        del data[:]
        data.append(my_pretty_table_string)
        # or data.append(report.extra.text)

But how to pass my_pretty_table_string to the hook function or how to edit report.extra.text from my test function ? Thank you for your help

1

There are 1 best solutions below

0
On

Where is your my_pretty_table_string stored and generated?

Please give more details to your question so we can help :)

pytest_html_results_table_html gets report object from pytest_runtest_makereport hook. So you need to implement pytest_runtest_makereport to add your data to the result at 'call' step.

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
   outcome = yield # get makereport outcome
   if call.when == 'call':
      outcome.result.my_data = <your data object goes here>

then you can access report.my_data in pytest_html_results_table_html hook.

Alex