Skip to main content

pytest plugin for generating HTML reports

Project description

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

License PyPI Conda Forge Travis Issues Requirements

Requirements

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

  • Python 2.7, 3.6, PyPy, or PyPy3

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

Images added as files or links are going to be linked as external resources, meaning that the standalone report HTML-file may not display these images as expected.

The plugin will issue a warning when adding files or links to the standalone report.

Enhancing reports

Appearance

Custom CSS (Cascasding Style Sheets) can be passed on the command line using the --css option. These will be applied in the order specified, and can be used to change the appearance of the report.

$ pytest --html=report.html --css=highcontrast.css --css=accessible.css

Environment

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'

The generated table will be sorted alphabetically unless the metadata is a collections.OrderedDict.

Additional summary information

You can edit the Summary section by using the pytest_html_results_summary hook:

import pytest
from py.xml import html

@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
    prefix.extend([html.p("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')

Image

extra.image('/path/to/file.png')

Image

extra.image('http://some_image.png')

Note: When adding an image from file, the path can be either absolute or relative.

Note: When using --self-contained-html, images added as files or links may not work as expected, see section Creating a self-contained report for more info.

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 also specify the name argument for all types other than html which will change the title of the created hyper link:

extra.append(pytest_html.extras.text('some string', name='Different title'))

Modifying the results table

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(1, 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'))

Display options

By default, all rows in the Results table will be expanded except those that have Passed.

This behavior can be customized with a query parameter: ?collapsed=Passed,XFailed,Skipped.

Screenshots

Enhanced HTML report

Contributing

Fork the repository and submit PRs with bug fixes and enhancements, contributions are very welcome.

Tests can be run locally with tox, for example to execute tests for Python 2.7 and 3.6 execute:

tox -e py27,py36

Releasing a new version

Follow these steps to release a new version of the project:

  1. Update your local master with the upstream master (git pull --rebase upstream master)

  2. Create a new branch and update CHANGES.rst with the new version, today’s date, and all changes/new features

  3. Commit and push the new branch and then create a new pull request

  4. Wait for tests and reviews and then merge the branch

  5. Once merged, update your local master again (git pull --rebase upstream master)

  6. Tag the release with the new release version (git tag v<new tag>)

  7. Push the tag (git push upstream --tags)

  8. Done. You can monitor the progress on Travis

Resources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-html-1.21.1.tar.gz (27.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_html-1.21.1-py2.py3-none-any.whl (15.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pytest-html-1.21.1.tar.gz.

File metadata

  • Download URL: pytest-html-1.21.1.tar.gz
  • Upload date:
  • Size: 27.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for pytest-html-1.21.1.tar.gz
Algorithm Hash digest
SHA256 ac405ca2fc4a55b83ca59319c69552f9bc870db2378e851633970bfc7fb93928
MD5 12c428fc5ab69611e4740769e48cc8eb
BLAKE2b-256 e8bad7c99867efb04378540b97876446ddaac1118b584a868e222f3d5745b31a

See more details on using hashes here.

File details

Details for the file pytest_html-1.21.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pytest_html-1.21.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for pytest_html-1.21.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c2312f3bb1c78fac08580dd2cc9bd0a726cad369375a9bf30c838ff7532120cc
MD5 242862c0626bb0ff1de41d8f115f6e94
BLAKE2b-256 526c8f83100a5232c1ccf3c995e470039d7b646fa2cb6e760bce6691755def39

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page