pytest-mpl

pytest plugin to help with testing figures output from Matplotlib

24 个版本 Python >=3.10
安装
pip install pytest-mpl
poetry add pytest-mpl
pipenv install pytest-mpl
conda install pytest-mpl
描述

pytest-mpl

pytest-mpl is a pytest <https://docs.pytest.org>__ plugin to facilitate image comparison for Matplotlib <http://www.matplotlib.org>__ figures.

For each figure to test, an image is generated and then subtracted from an existing reference image. If the RMS of the residual is larger than a user-specified tolerance, the test will fail. Alternatively, the generated image can be hashed and compared to an expected value.

For more information, see the pytest-mpl documentation <https://pytest-mpl.readthedocs.io>__.

Installation

.. code-block:: bash

pip install pytest-mpl

For detailed instructions, see the installation guide <https://pytest-mpl.readthedocs.io/en/latest/installing.html>__ in the pytest-mpl docs.

Usage

First, write test functions that create a figure. These image comparison tests are decorated with @pytest.mark.mpl_image_compare and return the figure for testing:

.. code-block:: python

import matplotlib.pyplot as plt import pytest

@pytest.mark.mpl_image_compare def test_plot(): fig, ax = plt.subplots() ax.plot([1, 2]) return fig

Then, generate reference images by running the test suite with the --mpl-generate-path option:

.. code-block:: bash

pytest --mpl-generate-path=baseline

Then, run the test suite as usual, but pass --mpl to compare the returned figures to the reference images:

.. code-block:: bash

pytest --mpl

By also passing --mpl-generate-summary=html, a summary of the image comparison results will be generated in HTML format:

+---------------+---------------+---------------+ | |html all| | |html filter| | |html result| | +---------------+---------------+---------------+

For more information on how to configure and use pytest-mpl, see the pytest-mpl documentation <https://pytest-mpl.readthedocs.io>__.

Contributing

pytest-mpl is a community project maintained for and by its users. There are many ways you can help!

  • Report a bug or request a feature on GitHub <https://github.com/matplotlib/pytest-mpl/issues>__
  • Improve the documentation or code

.. |html all| image:: docs/images/html_all.png .. |html filter| image:: docs/images/html_filter.png .. |html result| image:: docs/images/html_result.png

版本列表
0.19.0 2026-03-25
0.18.0 2025-11-15
0.17.0 2024-02-14
0.16.1 2022-07-23
0.16.0 2022-06-15
0.15.1 2022-04-22
0.15.0 2022-04-21
0.14.0 2022-02-09
0.13 2021-07-02
0.12.1 2021-07-02
0.12 2020-11-05
0.11 2019-11-15
0.10 2018-09-25
0.9 2017-10-12
0.8 2017-07-19
0.7 2016-11-26
0.6 2016-11-22
0.5 2016-05-06
0.4 2016-05-04
0.3 2015-06-26
0.2 2015-06-25
0.1 2015-06-25
0.14rc1 2022-02-09