Skip to content

Plugin for generating HTML reports for py.test results

License

Notifications You must be signed in to change notification settings

pricingassistant/pytest-html

 
 

Repository files navigation

pytest-html

pytest-html is a plugin for pytest that generates a HTML report for the test results.

License PyPI Travis Issues Requirements

Requirements

You will need the following prerequisites in order to use pytest-html:

  • Python 2.7, 3.6, PyPy, or PyPy3
  • pytest 2.7 or newer

Installation

To install pytest-html:

$ pip install pytest-html

Then run your tests with:

$ pytest --html=report.html

ANSI codes

Note that ANSI code support depends on the ansi2html package. Due to the use of a less permissive license, this package is not included as a dependency. If you have this package installed, then ANSI codes will be converted to HTML in your report.

Creating a self-contained report

In order to respect the Content Security Policy (CSP), several assets such as CSS and images are stored separately by default. You can alternatively create a self-contained report, which can be more convenient when sharing your results. This can be done in the following way:

$ pytest --html=report.html --self-contained-html

Enhancing reports

Environment

You can add change the Environment section of the report by modifying request.config._html.environment from a fixture:

@pytest.fixture(autouse=True)
def _environment(request):
    request.config._environment.append(('foo', 'bar'))

Extra content

You can add details to the HTML reports by creating an 'extra' list on the report object. Here are the types of extra content that can be added:

Type Example
Raw HTML extra.html('<div>Additional HTML</div>')
JSON extra.json({'name': 'pytest'})
Plain text extra.text('Add some simple Text')
URL extra.url('http://www.example.com/')
Image extra.image(image, mime_type='image/gif', extension='gif')

There are also convenient types for several image formats:

Image format Example
PNG extra.png(image)
JPEG extra.jpg(image)
SVG extra.svg(image)

The following example adds the various types of extras using a pytest_runtest_makereport hook, which can be implemented in a plugin or conftest.py file:

import pytest
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])
    if report.when == 'call':
        # always add url to report
        extra.append(pytest_html.extras.url('http://www.example.com/'))
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            # only add additional html on failure
            extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
        report.extra = extra

Screenshots

Enhanced HTML report

Resources

About

Plugin for generating HTML reports for py.test results

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 71.9%
  • JavaScript 18.8%
  • HTML 5.0%
  • CSS 4.3%