CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pytest-html

A pytest plugin for generating HTML reports of test results with enhanced visualizations and extensibility.

Pending
Overview
Eval results
Files

fixtures.mddocs/

Test Fixtures

Pytest fixtures that enable tests to add rich multimedia content and metadata to HTML reports. These fixtures provide a way to include additional context, debugging information, screenshots, logs, and other relevant data alongside test results.

Capabilities

extras Fixture

The primary fixture for adding extra content to test reports. Provides a list that tests can append extra content items to, which will be displayed in the HTML report.

@pytest.fixture
def extras(pytestconfig):
    """
    Add details to the HTML reports.

    This fixture provides a list that tests can append extra content to.
    Content added via extras will be displayed in the HTML report for
    the specific test.

    Parameters:
    - pytestconfig: pytest configuration object

    Yields:
    - list: List to append extra content items to

    Usage:
        def test_example(extras):
            extras.append(pytest_html.extras.url("https://example.com"))
            extras.append(pytest_html.extras.text("Debug info"))
    """

Usage Example:

import pytest_html

def test_with_extras(extras):
    # Add a URL for reference
    extras.append(pytest_html.extras.url("https://api.example.com/status"))
    
    # Add debugging text
    extras.append(pytest_html.extras.text("Database connection: OK"))
    
    # Add a screenshot (base64 encoded)
    with open("screenshot.png", "rb") as f:
        screenshot_data = base64.b64encode(f.read()).decode()
        extras.append(pytest_html.extras.png(screenshot_data, "Test Screenshot"))
    
    # Add JSON data
    debug_data = {"user_id": 123, "session": "abc123"}
    extras.append(pytest_html.extras.json(debug_data, "Debug Data"))
    
    # Test logic here
    assert True

extra Fixture (Deprecated)

Legacy fixture that provides the same functionality as extras. This fixture is deprecated and will be removed in a future release.

@pytest.fixture
def extra(pytestconfig):
    """
    DEPRECATED: Add details to the HTML reports.
    
    This fixture is deprecated and will be removed in a future release.
    Use 'extras' instead.

    Parameters:
    - pytestconfig: pytest configuration object

    Yields:
    - list: List to append extra content items to
    """

Integration with pytest-html

The fixtures work by storing extra content in pytest's stash system using a special key. The content is then processed during report generation and embedded into the HTML output.

Internal Implementation

# Stash key for storing extras data
extras_stash_key = pytest.StashKey[list]()

The fixtures store data in pytestconfig.stash[extras_stash_key] which is then accessed by the plugin during report generation via the pytest_runtest_makereport hook.

Content Processing

Extra content added through fixtures is processed during the pytest_runtest_makereport hook:

  1. Fixture extras are retrieved from the stash
  2. Plugin extras are retrieved from the report object
  3. Deprecated extras are retrieved from report.extra (with deprecation warning)
  4. All extras are combined and stored in report.extras
  5. The HTML report generator processes the extras during rendering

This ensures that all extra content from various sources is properly included in the final HTML report.

Install with Tessl CLI

npx tessl i tessl/pypi-pytest-html

docs

extras.md

fixtures.md

hooks.md

index.md

tile.json