pytest-dev / pytest-dev/pytest-html

How to add screenshot to report when result is ERROR

Open
#838 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
779
Forks
260
PR merge metrics
No merged PRs in 30d

Description

Hello,

I have an automation framework with selenium/pytest with some fixtures in the test modules which execute repetitive code.
In case the execution fails in the fixture this is marked as ERROR in the html report and despite being able to take a screenshot on this occurrence it is not attached to the report.
A few more details :

  • report.when == "setup"
  • report.failed is True
  • the extraHTML div is empty in the report
  • the same mechanism is used to attach the screenshot successfully if the failure occurs in the test code.

Example test & fixture

@pytest.fixture(autouse=True)
def open_userdata_page(vi_rewrite_gui):
    """
    Autouse fixture which opens userdata page before each test
    """
    vi_rewrite_gui.open_userdata_page(shortname)


def test_validation_errors(vi_rewrite_gui):
    """
    Check validation errors when trying to proceed without filling any info
    """
    vi_rewrite_gui.click_continue_btn()
    actual_validation_errors = vi_rewrite_gui.get_validation_errors()
    assert expected_validation_errors == actual_validation_errors

Conftest hook

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):

    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)
    extra = getattr(report, 'extra', [])
    if report.when == 'call' or report.when == "setup":
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            driver = DRIVER_REGISTRY.get(item.nodeid)
            if driver:
                # driver = item.funcargs['driver_mod']
                timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
                screenshot_name = f"{item.name}_{timestamp}.png"
                driver.save_screenshot(os.path.join(SCREENSHOTS_PATH, screenshot_name))
                build_img_path_and_div_for_html(screenshot_name, extra, pytest_html)
        report.extra = extra

report

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported pytest_runtest_makereport hook in conftest and compare the setup and call report paths, then inspect how pytest-html consumes report.extra. Reproduce the fixture failure from the example and verify that the screenshot is attached to the generated HTML report for setup errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.