Skip to content

Can no longer add images to report during teardown phase #777

@zolveuran

Description

@zolveuran

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 = extras

Image of report when plotting in "call":

image

Image of report when plotting in "teadown":

image

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions