How to add extra information to the report

In reports generated by pytest-html, you can add extra detailed information to the report by creating an extras list and adding it to the report object. This extra information can be of various types such as text, images, links, etc. Below are several common types of extra information and how to add them:

Common Extra Information Types and Examples

Information Type Example
Raw HTML extras.html('<div>Additional HTML</div>')
JSON Format extras.json({'name': 'pytest'})
Plain Text extras.text('Add some simple Text')
Link (URL) extras.url('http://www.example.com/')
GIF Image extras.image(image, mime_type='image/gif', extension='gif')
Link (URL) extras.url('http://www.example.com/', name="Click to view example")
PNG Image extras.png(base64_encoded_string, name='Image Name')
JPG Image extras.jpg(base64_encoded_string, name='Image Name')
SVG Image extras.svg(base64_encoded_string, name='Image Name')
Generic Image extras.image(base64_encoded_string, name='Image Name')
Local Image extras.image('/path/to/file.png')
Web Image extras.image('http://some_image.png')

Comparison:

  • When adding images, the path can be either an absolute path or a relative path.
  • If you use the --self-contained-html option, images added as files or links may not display correctly in the report. For details, please see Creating a self-contained report.

Adding Extra Information Using pytest hook

You can use the pytest_runtest_makereport hook function to add custom extra information to the report during test execution. This is typically implemented in conftest.py or a plugin. Below is an example showing how to add different types of extra information like URL and HTML:

Python
import pytest
from pytest_html import extras

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, "extras", [])
    if report.when == "call":
        # Always add a URL to the report
        extra.append(pytest_html.extras.url("http://www.example.com/"))
        xfail = hasattr(report, "wasxfail")
        if (report.skipped and xfail) or (report.failed and not xfail):
            # Add extra HTML only when the test fails
            extra.append(pytest_html.extras.html("<div>Additional HTML</div>"))
        report.extras = extra

Adding Information Directly in Test Functions

If you don't want to implement a hook function, you can use the extras fixture to add extra information directly inside the test function. This content will usually appear before the extra information added by hooks.

Python
from pytest_html import extras

def test_extra(extra):
    # Add text information
    extra.append(extras.text("some string"))

    # Add screenshot to the test report
    extra.append(extras.image(model.getWindow("Dialogs").takeScreenshot()))

Specifying Custom Titles for Extra Information

Except for the html type, you can specify a name parameter for other types of extra information to customize the display title of that information in the report. For example:

Python
extra.append(pytest_html.extras.text("some string", name="Custom Title"))

Automatically Screenshot on Failure Using Hook

It is recommended to use the pytest_runtest_makereport hook function to dynamically add information during test execution and report generation. This is a very practical example demonstrating how to automatically take a screenshot and add it to the report when a test case fails.

Python
# conftest.py
import pytest
from pytest_html import extras

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """
    Hook function to generate report after each test case execution.
    """
    outcome = yield
    report = outcome.get_result()
    
    # report.when is only considered during the "call" phase to avoid adding duplicates during setup and teardown
    if report.when == "call":
        # The extra list is used to store extra information to be added to the report
        extra = getattr(report, "extras", [])
        
        # Check if the test case failed
        if report.failed:
            # Add your screenshot logic here, e.g., using Selenium or other UI test libraries
            screenshot_base64 = model.getWindow("Standard_Dialogs").takeScreenshot()
            
            # Use extras.image to add the image and customize the title with the name parameter
            extra.append(extras.image(screenshot_base64, name="Failure Screenshot"))
        
        report.extras = extra


Global Customization of the Report

pytest-html v4+ introduced new hook functions that allow you to easily customize the global properties of the report.

1. Customizing Report Title

Use the pytest_html_report_title hook function to modify the <title> and main heading of the report.

Python
# conftest.py

def pytest_html_report_title(report):
    """
    Modify the title of the report.
    """
    report.title = "API Automation Test Report"

2. Customizing Duration Display Format

Use the pytest_html_duration_format hook function to modify the display format of the test duration, for example, adding units.

Python
# conftest.py

def pytest_html_duration_format(duration: float) -> str:
    """
    Customize the display format of duration, keeping two decimal places and adding "seconds" as unit.
    """
    return f"{duration:.2f} seconds"