tests.conftest

tests.conftest

This module facilitates the inclusion of images in pytest-html reports.

Generate images with matplotlib and use conftest.savefig(request) instead of matplotlib’s savefig. Example:

import conftest
from matplotlib import pyplot as plt

def test_html_report(request):
    plt.figure()
    plt.plot()
    conftest.savefig(request)

How to use it

  • To use this module, please create the file tests/conftest.py in your repository, with the following content: from core.conftest import *

  • Run pytest with the following options (can be added to pytest.ini): –html=tests/test_report.html –self-contained-html

    Sample pytest.ini: [pytest] addopts= -n auto –html=test_report.html –self-contained-html

Note: this requires pytest-html

Other features

  • If you generate an image as a ByteIO buffer, you can add it to the report like so:

    def test_with_image(request): plt.plot(…) fp = io.BytesIO() plt.savefig(fp) conftest.add_image_to_report(request, fp)

  • If you generate a file in your test, you can link it in the report using:

    conftest.add_link_to_report(request, path)

  • The images embedded in the html file can be dragged in a browser to view them in full size.

Parameters

Name Type Description Default
By required
img_collapsible Whether to use a collapsible section to embed the images (default true). Ex: true, false Otherwise, the standard image integration is used. required
img_size Image size, when using img_collapsible mode Ex: 100%, 250px (default) required
dark_mode Whether to enable dark mode theme in HTML reports (default false). Ex: true, false (default) required
monitor_peak_memory Whether to monitor and report peak RSS memory usage (default false). Ex: true, false (default) required

Functions

Name Description
add_extra_to_report Add extra content to pytest-html report (through request.node.extras)
add_image_to_report Appends image data to request.node.images
add_link_to_report Add a link to a local file path
pytest_configure Store config for later use in hooks.
pytest_html_results_summary Add embedded dark theme CSS to the HTML report if dark_mode is enabled.
pytest_html_results_table_header Insert per-test memory columns in pytest-html results table.
pytest_html_results_table_row Render per-test memory values in pytest-html results table.
savefig Wraps matplotlib’s savefig to add image data to request.node.images

add_extra_to_report

tests.conftest.add_extra_to_report(request, *args)

Add extra content to pytest-html report (through request.node.extras)

Examples: add_extra_to_report(request, ‘sample text’, ‘text’) add_extra_to_report(request, ‘sample html’, ‘html’) add_extra_to_report(request, ‘https://www.wikipedia.org/’, ‘url’, ‘Link text’)

add_image_to_report

tests.conftest.add_image_to_report(request, fp)

Appends image data to request.node.images

request: pytest request fixture

fp: BytesIO

pytest_configure

tests.conftest.pytest_configure(config)

Store config for later use in hooks.

pytest_html_results_summary

tests.conftest.pytest_html_results_summary(prefix, summary, postfix)

Add embedded dark theme CSS to the HTML report if dark_mode is enabled.

pytest_html_results_table_header

tests.conftest.pytest_html_results_table_header(cells)

Insert per-test memory columns in pytest-html results table.

pytest_html_results_table_row

tests.conftest.pytest_html_results_table_row(report, cells)

Render per-test memory values in pytest-html results table.

savefig

tests.conftest.savefig(request, **kwargs)

Wraps matplotlib’s savefig to add image data to request.node.images

kwargs are passed to plt.savefig