pytest-html

repository·master·Indexed 20 days ago

https://github.com/pytest-dev/pytest-html

A 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.

Tokens
8.5K
Snippets
41
Records
62
Agent score
73%

What's inside pytest-html

  1. Use pytest-html to generate HTML test reports

    master
    pytest-html is a plugin for 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.
  2. Modify the Environment section

    master

    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"
  3. Add extra content to reports

    master

    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 HTML
    • extras.json(dict): JSON data
    • extras.text(string): Plain text
    • extras.url(url_string): Hyperlink
    • extras.image(path_or_url, ...): Images (supports absolute/relative paths and URLs)
    • Specialized image helpers: 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"))
  4. Set up the development environment

    master

    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 install

    If not using Hatch, install and set up pre-commit manually:

    $ pip install pre-commit
    $ pre-commit install
  5. Run Python tests

    master

    Python 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 && ./start

    You can monitor the tests in your browser at localhost:7900 (password: secret).

    2. Execute tests: If using Hatch:

    $ hatch -e test run tox

    Otherwise, install tox and run it directly:

    $ pip install tox
    $ tox
  6. Create a self-contained HTML report

    master

    By 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-html