pytest-reporter

0.5.3 · active · verified Thu Apr 16

pytest-reporter is a Pytest plugin that enables the generation of highly customizable test reports using templates. It provides the data context from a Pytest session, allowing users to create various text-based reports like HTML, LaTeX, or CSV by implementing their own rendering logic. The library is actively maintained with a regular release cadence, often addressing compatibility with new `pytest` versions and enhancing report data.

Common errors

Warnings

Install

Imports

Quickstart

To get started, create a `templates` directory with a Jinja2 HTML template (e.g., `my_report.html`). Then, in your `conftest.py`, implement the `pytest_reporter_render` hook to define how templates are loaded and rendered. Finally, run `pytest` from your project root, specifying the template directory, template file, and output report path using command-line options.

# File: conftest.py
from jinja2 import Environment, FileSystemLoader, TemplateNotFound

def pytest_reporter_render(template_name, dirs, context):
    """This hook is called to render a report template."""
    env = Environment(loader=FileSystemLoader(dirs))
    try:
        template = env.get_template(template_name)
    except TemplateNotFound:
        # Let other template plugins handle it if not found here
        return None
    return template.render(context)

# File: templates/my_report.html
<!-- Example Jinja2 template -->
<!DOCTYPE html>
<html>
<head><title>Pytest Report</title></head>
<body>
    <h1>Test Report</h1>
    <p>Started: {{ started|strftime('%Y-%m-%d %H:%M:%S') }}</p>
    <p>Total tests: {{ tests|length }}</p>
    <ul>
    {% for test in tests %}
        <li>{{ test.item.nodeid }} - Status: {{ test.status.word }}
            {% if test.phases %}
                <ul>
                {% for phase in test.phases %}
                    <li>Phase: {{ phase.name }} - Status: {{ phase.status.word }}</li>
                {% endfor %}
                </ul>
            {% endif %}
        </li>
    {% endfor %}
    </ul>
</body>
</html>

# File: test_example.py
def test_success():
    assert True

def test_failure():
    assert False

# To run and generate report (from project root):
# pytest --template-dir=templates --template=my_report.html --report=report.html

view raw JSON →