matplotlib.testing

matplotlib.testing

Helper functions for testing.

matplotlib.testing.is_called_from_pytest()[source]

[Deprecated] Whether we are in a pytest run.

Notes

Deprecated since version 3.2.

matplotlib.testing.set_font_settings_for_testing()[source]
matplotlib.testing.set_reproducibility_for_testing()[source]
matplotlib.testing.setup()[source]

matplotlib.testing.compare

Utilities for comparing image results.

matplotlib.testing.compare.compare_images(expected, actual, tol, in_decorator=False)[source]

Compare two "image" files checking differences within a tolerance.

The two given filenames may point to files which are convertible to PNG via the converter dictionary. The underlying RMS is calculated with the calculate_rms function.

Examples

img1 = "./baseline/plot.png"
img2 = "./output/plot.png"
compare_images(img1, img2, 0.001)
matplotlib.testing.compare.comparable_formats()[source]

Return the list of file formats that compare_images can compare on this system.

Returns:
list of str

E.g. ['png', 'pdf', 'svg', 'eps'].

matplotlib.testing.decorators

class matplotlib.testing.decorators.CleanupTestCase(methodName='runTest')[source]

Bases: unittest.case.TestCase

A wrapper for unittest.TestCase that includes cleanup operations.

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

classmethod setUpClass()[source]

Hook method for setting up class fixture before running tests in the class.

classmethod tearDownClass()[source]

Hook method for deconstructing the class fixture after running all tests in the class.

matplotlib.testing.decorators.check_figures_equal(*, extensions=('png', 'pdf', 'svg'), tol=0)[source]

Decorator for test cases that generate and compare two figures.

The decorated function must take two keyword arguments, fig_test and fig_ref, and draw the test and reference images on them. After the function returns, the figures are saved and compared.

This decorator should be preferred over image_comparison when possible in order to keep the size of the test suite from ballooning.

Parameters:
extensionslist, default: ["png", "pdf", "svg"]

The extensions to test.

tolfloat

The RMS threshold above which the test is considered failed.

Examples

Check that calling Axes.plot with a single argument plots it against [0, 1, 2, ...]:

@check_figures_equal()
def test_plot(fig_test, fig_ref):
    fig_test.subplots().plot([1, 3, 5])
    fig_ref.subplots().plot([0, 1, 2], [1, 3, 5])
matplotlib.testing.decorators.check_freetype_version(ver)[source]
matplotlib.testing.decorators.cleanup(style=None)[source]

A decorator to ensure that any global state is reset before running a test.

Parameters:
stylestr, dict, or list, optional

The style(s) to apply. Defaults to ["classic", "_classic_test_patch"].

matplotlib.testing.decorators.image_comparison(baseline_images, extensions=None, tol=0, freetype_version=None, remove_text=False, savefig_kwarg=None, style=('classic', '_classic_test_patch'))[source]

Compare images generated by the test with those specified in baseline_images, which must correspond, else an ImageComparisonFailure exception will be raised.

matplotlib.testing.decorators.remove_ticks_and_titles(figure)[source]

matplotlib.testing.exceptions

exception matplotlib.testing.exceptions.ImageComparisonFailure[source]

Bases: AssertionError

Raise this exception to mark a test as a comparison between two images.