pytest-html is a plugin for pytest that generates a HTML report for the test results.
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
To install pytest-html:
$ pip install pytest-html
Then run your tests with:
$ pytest --html=report.html
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.
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
The Environment section is provided by the pytest-metadata, plugin, and can be accessed
via the pytest_configure
hook:
def pytest_configure(config):
config._metadata['foo'] = 'bar'
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
You can modify the columns by implementing custom hooks for the header and
rows. The following example conftest.py
adds a description column with
the test function docstring, adds a sortable time column, and removes the links
column:
from datetime import datetime
from py.xml import html
import pytest
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
cells.insert(2, html.th('Description'))
cells.insert(0, html.th('Time', class_='sortable time', col='time'))
cells.pop()
@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
cells.insert(2, html.td(report.description))
cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))
cells.pop()
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
report.description = str(item.function.__doc__)
You can also remove results by implementing the
pytest_html_results_table_row
hook and removing all cells. The
following example removes all passed results from the report:
import pytest
@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
if report.passed:
del cells[:]
The log output and additional HTML can be modified by implementing the
pytest_html_results_html
hook. The following example replaces all
additional HTML and log output with a notice that the log is empty:
import pytest
@pytest.mark.optionalhook
def pytest_html_results_table_html(report, data):
if report.passed:
del data[:]
data.append(html.div('No log output captured.', class_='empty log'))
![Enhanced HTML report](https://cloud.githubusercontent.com/assets/122800/11952194/62daa964-a88e-11e5-9745-2aa5b714c8bb.png)