pytest-html
pytest-html is a plugin for the pytest testing framework that generates comprehensive HTML reports for test results. It provides a visual overview of test runs, including pass/fail status, durations, and detailed information for each test. The library is actively maintained, with version 4.2.0 being the current release, and follows a frequent release cadence, addressing bug fixes and improvements regularly.
Warnings
- breaking Python 3.7 support was dropped in `pytest-html` v4.0.0. Ensure your Python environment is 3.8+ to use versions 4.x.x.
- breaking The `py` module was removed as a dependency in `pytest-html` v4.0.0. If your `conftest.py` or custom plugins directly imported `py` for XML/HTML generation (e.g., `from py.xml import html`), you may need to adjust your code. While `py.xml.html` is still commonly used in examples, its direct dependency on `py` is gone.
- deprecated Configuration options `duration_formatter` and `render_collapsed = True` were deprecated in v4.0.0. `duration_formatter` has no effect, and `render_collapsed = True` should be replaced with `render_collapsed = all` for consistency with query parameters.
- deprecated The `report.extra` attribute and the `extra` fixture were deprecated in v4.0.0. Use `report.extras` and the `extras` fixture instead for adding custom content to reports.
- gotcha When using `--self-contained-html`, images or links added as external resources (e.g., screenshots saved to separate files) may not display correctly as they are not embedded into the report. The plugin will issue a warning.
- gotcha Customizing the report table using hooks like `pytest_html_results_table_header` might lead to empty result tables when running tests with tags in `pytest-html` v4.1.1 and v4.2.0 due to the hook not being invoked.
Install
-
pip install pytest-html
Imports
- pytest_html
import pytest_html from py.xml import html # For HTML content in hooks
Quickstart
import pytest
# test_example.py
def test_pass():
assert True
def test_fail():
assert False
def test_skipped():
pytest.skip("This test is skipped")
# To run tests and generate a self-contained HTML report:
# $ pytest --html=report.html --self-contained-html
# (Run this command in your terminal where test_example.py is located)