Overview of pytest-html
masterpytest that generates an HTML report containing your test results. It allows you to visualize test outcomes in a browser-based format.repository·master·Indexed 20 days ago
https://github.com/pytest-dev/pytest-htmlA pytest plugin that generates interactive HTML reports for test results. It provides features for visualizing test outcomes, customizing report appearance via CSS, adding extra content (HTML, JSON, images), and controlling result visibility and sorting through query parameters or configuration files.
pytest that generates an HTML report containing your test results. It allows you to visualize test outcomes in a browser-based format.pytest that automatically generates a detailed HTML report containing your test results. It integrates directly into your existing pytest workflow to provide visual feedback on test successes, failures, and execution details.The Environment section is populated by the pytest-metadata plugin. You can modify this section using pytest_configure (before tests run) or pytest_sessionfinish (after tests run).
To ensure your changes are picked up before other plugins finish, use @pytest.hookimpl(tryfirst=True) when implementing pytest_sessionfinish.
from pytest_metadata.plugin import metadata_key
# Modify BEFORE tests run
def pytest_configure(config):
config.stash[metadata_key]["foo"] = "bar"
# Modify AFTER tests run
import pytest
from pytest_metadata.plugin import metadata_key
@pytest.hookimpl(tryfirst=True)
def pytest_sessionfinish(session, exitstatus):
session.config.stash[metadata_key]["foo"] = "bar"You can add rich content (HTML, JSON, text, URLs, or images) to a report using the extras list on the report object. This can be done via the pytest_runtest_makereport hook or by using the extras fixture directly in a test function.
Supported types include:
extras.html(string): Raw HTMLextras.json(dict): JSON dataextras.text(string): Plain textextras.url(url_string): Hyperlinkextras.image(path_or_url, ...): Images (supports absolute/relative paths and URLs)extras.png(image), extras.jpg(image), extras.svg(image)For all types except html, you can provide a name argument to customize the hyperlink text.
import pytest
import pytest_html
# Method 1: Using a hook in conftest.py
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
extras = getattr(report, "extras", [])
if report.when == "call":
extras.append(pytest_html.extras.url("http://www.example.com/"))
report.extras = extras
# Method 2: Using the fixture in a test
def test_extra(extras):
extras.append(pytest_html.extras.text("some string", name="Different title"))To contribute to pytest-html, use Hatch to manage the Python virtual environment and pre-commit for styling and formatting.
If using Hatch, run:
$ hatch -e test run pre-commit installIf not using Hatch, install and set up pre-commit manually:
$ pip install pre-commit
$ pre-commit installPython tests require Tox and Docker. Integration tests specifically use Docker to render reports via Selenium and BeautifulSoup.
1. Start the Docker image:
Run ./start to begin the required image. If the image becomes unresponsive, restart it using:
$ ./start down && ./startYou can monitor the tests in your browser at localhost:7900 (password: secret).
2. Execute tests:
If using Hatch:
$ hatch -e test run toxOtherwise, install tox and run it directly:
$ pip install tox
$ toxCSS is generated via SASS/SCSS and requires npm.
To build the application assets:
$ npm install$ npm run build$ npm install
$ npm run buildYou can install pytest-html directly from PyPI using pip.
$ pip install pytest-htmlBy default, assets like CSS and images are stored as separate files to respect Content Security Policy (CSP). To create a single, standalone HTML file that includes all assets (useful for sharing), use the --self-contained-html flag.
Warning: When using --self-contained-html, images added via local file paths or external links may not display correctly. The plugin will issue a warning in these cases.
$ pytest --html=report.html --self-contained-htmlTo install pytest-html in editable mode from a local source repository, use pip install -e ..
$ pip install -e .Documentation is written in RST. When adding new files, ensure they are added to the toctree section in index.rst.
To build the documentation locally using tox:
$ tox -e docsJavaScript tests require npm. The project uses Mocha, Chai, and Sinon internally.
To run the tests:
$ npm install$ npm run unit$ npm install
$ npm run unit