Generating HTML Reports with PyTest and Selenium

In test automation, execution results are just as important as writing robust test scripts. When using Selenium with PyTest, having a readable and visual HTML test report greatly helps in understanding the test outcomes, identifying failed cases, and sharing results with stakeholders. PyTest, one of the most popular Python testing frameworks, supports plugins that can generate rich and interactive HTML reports.

In this blog, we'll explore how to integrate HTML reporting in Selenium-PyTest automation projects, step-by-step, using the powerful pytest-html plugin.


✅ Why HTML Reports?

While PyTest provides plain terminal output, HTML reports offer:

A visual overview of passed/failed/skipped test cases

Screenshots for failed tests

Test durations and metadata

Shareable results for QA teams and managers


๐Ÿงฉ Project Setup

To begin, make sure you have the following installed:

bash


pip install selenium pytest pytest-html

๐Ÿงช Sample Selenium Test with PyTest

Here’s a basic Selenium test using PyTest:


python


# test_google.py

import pytest

from selenium import webdriver


@pytest.fixture

def setup():

    driver = webdriver.Chrome()

    driver.maximize_window()

    yield driver

    driver.quit()


def test_google_search(setup):

    setup.get("https://www.google.com")

    assert "Google" in setup.title

This simple test launches Chrome, opens Google, and asserts the title.


๐Ÿ“ Generating the HTML Report

To generate an HTML report, simply run the test using:


bash

pytest --html=report.html

This will create a file named report.html in your project directory, which contains a detailed test report.


๐Ÿ“ธ Adding Screenshots for Failed Tests

For visual debugging, you can capture screenshots on failure using hooks in PyTest:


python


# conftest.py

import pytest

from selenium import webdriver


@pytest.hookimpl(hookwrapper=True)

def pytest_runtest_makereport(item, call):

    outcome = yield

    report = outcome.get_result()

    if report.when == "call" and report.failed:

        driver = item.funcargs.get("setup")

        if driver:

            screenshot_name = f"screenshot_{item.name}.png"

            driver.save_screenshot(screenshot_name)

            # Attach to HTML report

            extra = getattr(report, 'extra', [])

            extra.append(pytest_html.extras.png(screenshot_name))

            report.extra = extra

Also, add this to your pytest.ini to enable metadata:


ini

Copy

Edit

[pytest]

addopts = --html=report.html --self-contained-html


๐Ÿงฐ Customizing the Report

You can customize the report with environment info and metadata:


python
def pytest_configure(config):
    config._metadata['Project Name'] = 'Fullstack Python Automation'
    config._metadata['Tester'] = 'Ganesh Iraganti'

You can also disable or edit default metadata:


python


def pytest_metadata(metadata):

    metadata.pop("Plugins", None)


๐Ÿ“‚ Final Folder Structure


project/

├── test_google.py

├── conftest.py

├── report.html

├── screenshot_test_google_search.png (if failed)

└── pytest.ini


๐ŸŽฏ Benefits of HTML Reporting with PyTest

✅ Visual clarity on test pass/fail/skipped status

✅ Easy debugging with screenshots

✅ Stakeholder-friendly format

✅ No extra GUI tools required


๐Ÿš€ Conclusion

Generating HTML reports using PyTest and Selenium is a best practice for modern test automation frameworks. With plugins like pytest-html, testers can create clean, interactive reports that enhance visibility, improve debugging, and make automation more team-friendly.

If you're building a scalable Selenium-PyTest framework, HTML reports are essential—and easy to implement. Start integrating them today to bring more clarity and professionalism to your testing process

Learn Selenium with Pyhton Training Hyderabad

Read More: Capturing Browser Logs in Selenium Python

Read More:  How to Handle Broken Links in Selenium Python

Read More: Capturing Console Logs using Selenium Python

Visit IHUB Talent Institute Hyderabad

Get Direction

Comments

Popular posts from this blog

How to Use Tosca's Test Configuration Parameters

Tosca Licensing: Types and Considerations

Using Hibernate ORM for Fullstack Java Data Management