PyTest HTML Reporter: Generating Beautiful Report

Pytest HTML Reporter (pytest-html)

PyTest-html: PyTest HTML Report

pytest-html is a powerful plugin for pytest. pytest-html enables the generation of visually appealing and informative HTML reports. Pytest HTML reporter provide a comprehensive overview of test execution, making it easier to analyze results, identify failures, and share testing outcomes with stakeholders.

You should review our earlier articles of pytest, compare with unittest and pytest-cov to know more about pytest features and functionalities.

Why Use pytest-html (PyTest HTML Reporter)?

  • Enhanced Readability: HTML reports offer a structured and user-friendly presentation of test results compared to plain text output.
  • Detailed Information: The reports include detailed information about each test, such as pass/fail status, duration, captured output (stdout/stderr), and traceback information for failures.
  • Easy Sharing: HTML reports can be easily shared with team members, clients, or other stakeholders, facilitating collaboration and communication.
  • Customization: pytest-html offers various options for customizing the appearance and content of the reports, allowing you to tailor them to your specific needs.
  • Integration: Seamlessly integrates with pytest, requiring minimal configuration.

Installation of PyTest HTML Reporter:

To install pytest-html use below pip command:

pip install pytest-html
pip install pytest-html
pip install pytest-html

Basic Usage:

After installation, pytest-html will automatically generate an HTML report when you run your tests with pytest. By default, the report will be saved as report.html in the current directory.

pytest –html=report.html
pytest --html=report.html
pytest –html=report.html

You can specify a different filename or path for the report using the –html option.

Key Features and Customization of PyTest HTML Reporter:

Title and Description

You can customize the title and description of the report using the –title and –description options:

pytest –html=report.html
pytest --html=report.html --title="My Test Report" --description="Regression tests for version 1.0"
pytest –html=report.html

Self-Contained Reports

Include all the styling and images in a single html file.

pytest html self-contained-html
pytest --html=report.html --self-contained-html
pytest html self-contained-html

Adding Environment Information

You can add environment information to the report using the pytest_configure hook in your conftest.py file

conftest.py
def pytest_configure(config): 
    config._metadata = { 
        "Project Name": "My Project", 
        "Environment": "Dev", 
        "Release": "1.0.0", 
       }
conftest.py config

Adding Extra Content:

You can add extra content to the report, such as screenshots or log files, using the pytest_runtest_makereport hook:

conftest.py pytest_runtest_makereport
import pytest @pytest.mark.hookwrapper 

def pytest_runtest_makereport(item, call):
    outcome = yield 
    report = outcome.get_result() 
    if report.when == "call" and report.failed:
       report.extra.append(pytest_html.extras.image("screenshot.png"))
       report.extra.append(pytest_html.extras.text("log.txt"))
conftest.py pytest_runtest_makereport

Custom CSS: You can provide a custom CSS file to style the report:

pytest --html=report.html --css=custom.css

Live logging: You can make the test log show in real time in the html report.

pytest --html=report.html --live-log

Collapsible Sections: Failures are shown in collapsible sections, making it easier to navigate large reports.

Duration and Timing: The report provides detailed information about the duration of each test and the overall test execution time.

Example Usage:

Let’s generate the html report for our Task Manager project developed in our pytest article.

Running pytest with the –html option will generate an HTML report:

pytest –html=report.html
pytest --html=report.html
pytest –html=report.html
Python
S C:\enodeas\TaskManager> pytest --html=report.html                                        
========================== test session starts ==========================
platform win32 -- Python 3.10.9, pytest-8.3.5, pluggy-1.5.0
rootdir: C:\enodeas\TaskManager
plugins: bdd-8.1.0, cov-6.0.0, html-4.1.1, metadata-3.1.1, mock-3.14.0, xdist-3.6.1, anyio-3.5.0
collected 13 items

tests\test_database.py ......                                                                                            [ 46%]
tests\test_task_manager.py .......                                                                                       [100%]
------ Generated html report: file:///C:/enodeas/TaskManager/report.htm----
Python

The report.html file will contain a detailed overview of the test results, including the failure of test_failure.

PyTest-html report

Conclusion:

pytest-html is an invaluable tool for generating comprehensive and visually appealing HTML reports for pytest test runs. Its ease of use, customization options, and detailed information make it an essential plugin for any Python testing workflow. By leveraging pytest-html, you can enhance your testing process, improve collaboration, and gain deeper insights into your test results.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.