-
Notifications
You must be signed in to change notification settings - Fork 251
Can no longer add images to report during teardown phase #777
Description
Hello,
I have noticed that adding images to the report during the teardown phase no longer works in pytest-html version 4.1.1. It used to work in version 3 though. I've added a minimal example of a conftest.py below. Setting report.when == "teardown" to "call" instead makes the plots appear.
I need this behavior as I'm using pytest for hardware in the loop testing. My fixtures represent actual lab equipment that disconnect and save all data to file in their teardown phase. In my actual pytest_runtest_makereport() I discover all generated plots and put them in the report under the correct test case.
Versions where adding images in teardown works:
- pytest==7.4.3
- pytest-html==3.2.0
Versions where adding images in teardown doesn't work:
- pytest==7.4.3
- pytest-html==4.1.1
import pytest
import pytest_html
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""Populates description column in report and adds images."""
outcome = yield # Get test results
report = outcome.get_result()
# Add description column with test function docstring
report.description = str(item.function.__doc__)
extras = getattr(report, "extras", [])
# Generate and add plots after pytest fixture teardown
if report.when == "teardown":
plot_path = "./some_plot.png"
extras.append(pytest_html.extras.png(plot_path))
report.extras = extrasImage of report when plotting in "call":
Image of report when plotting in "teadown":
If this is fixed I also propose that a mention of changing the report.when == "call" line is put in the documentation. It was super important to me to be able to change this, but it took a lot of staring to realize why plotting didn't work as expected. Took me a week to notice that the plots put into the report where from the previous run when adding them during "call", since plots were not generated by fixtures before "teardown".
Otherwise a super happy user of pytest-html! It has really been a godsend to generate a report with logs and plots for 150 test cases. Great work!

