DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

How to Configure pytest-html Report Output, Self-Contained Files, and Test Metadata

Use pytest-html command-line options and hooks to save a report, make it shareable, set its title, add or redact environment metadata, and attach test extras.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Save a pytest-html report at a chosen path and make it portable with pytest --html=reports/test-results.html --self-contained-html. The report directory must already exist and be writable. From there, configure its title, environment panel, redaction rules, and per-test extras in pytest hooks or pytest configuration.

Save the report to a specific path

Use --html to select the destination file. Add --self-contained-html when you want the report delivered as one HTML file:

As an Amazon Associate I earn from qualifying purchases.

pytest --html=reports/test-results.html --self-contained-html

The directory portion of the path is a normal filesystem path; ensure reports exists and the test process can write to it. The plugin documents --html=report.html as the basic form of the option. pytest-html user guide

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose between separate assets and one self-contained file

Output choice What to expect Best fit
Default report Stores assets separately, following the plugin’s Content Security Policy approach. Keeping the report and its supporting assets together in a build or CI artifact.
--self-contained-html Embeds assets such as CSS and images into the HTML file. Images added as file or URL extras may still be external, so they can fail to display after moving the report. Sharing or publishing a single report file, while checking any attached images separately.

The standalone option improves portability, but does not guarantee that every extra image is embedded. The plugin specifically warns about file- and URL-based image extras. pytest-html user guide

Set a custom report title

Without a title hook, pytest-html uses the report filename as its title. Add pytest_html_report_title to your project’s conftest.py to set a more descriptive label:

def pytest_html_report_title(report):
    report.title = "Nightly API test results"

pytest-html user guide

Add metadata to the Environment panel

The Environment panel is provided through pytest-metadata. Import its metadata_key and modify the metadata mapping in a hook. Put this in conftest.py when the value is known at configuration time:

from pytest_metadata.plugin import metadata_key


def pytest_configure(config):
    config.stash[metadata_key]["Build"] = "nightly"

If the value is not available until tests have finished, update it in pytest_sessionfinish. Mark the hook tryfirst=True so the reporting plugins see the change before they consume the metadata:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import pytest
from pytest_metadata.plugin import metadata_key


@pytest.hookimpl(tryfirst=True)
def pytest_sessionfinish(session, exitstatus):
    session.config.stash[metadata_key]["Build"] = "nightly"

Metadata values are alphabetized unless the metadata uses an OrderedDict. pytest-html user guide

Redact sensitive environment values

Set environment_table_redact_list in pytest configuration to regex patterns matching Environment table variable names. Matching values are grayed out; their names remain visible. For example, add this to pytest.ini:

[pytest]
environment_table_redact_list = ^API_TOKEN$
    .*PASSWORD.*

Adjust the patterns to match the names your test environment actually emits. pytest-html user guide

Attach test-specific extras

On pytest-html 4.x, use report.extras and the extras fixture. The older report.extra attribute and extra fixture are deprecated. This hook attaches a text extra to failed test calls:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import pytest
import pytest_html


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    extras = getattr(report, "extras", [])
    if report.when == "call" and report.failed:
        extras.append(pytest_html.extras.text("Inspect the captured trace"))
    report.extras = extras

Documented extra types include HTML, JSON, plain text, URLs, and images. For image extras, remember that a self-contained report may still refer to an external file or URL. pytest-html user guide pytest-html deprecations

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Customize summary content, result columns, and sorting

Use plugin hooks for report content that is not controlled by the output command:

  • pytest_html_results_summary(prefix, summary, postfix) appends summary material.
  • pytest_html_results_table_header(cells) and pytest_html_results_table_row(report, cells) customize table headers and rows.
  • pytest_html_results_table_html(report, data) changes additional per-result HTML or log output.

The guide also documents initial_sort for the table’s starting sort order, URL parameters for sorting and visible outcomes, and render_collapsed for initially collapsed rows. A URL query parameter takes precedence over render_collapsed. pytest-html user guide

Account for pytest-html 4.x changes

The deprecations page dates the move from report.extra and the extra fixture to their plural forms to version 4.0.0. It also marks render_collapsed = True as deprecated in favor of values matching the query parameter, such as all, and says duration_formatter was removed and no longer has an effect. pytest-html deprecations

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The changelog records pytest-html 4.1.1 on 2023-11-07 and describes 4.0.0 as a major rewrite. That dated entry does not establish which release is latest now; check the project’s release information when selecting a version. pytest-html changelog

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.