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.
Parameters:
expectedstr
The filename of the expected image.
actualstr
The filename of the actual image.
tolfloat
The tolerance (a color value difference, where 255 is the
maximal difference). The test fails if the average pixel
difference is greater than this value.
in_decoratorbool
Determines the output format. If called from image_comparison
decorator, this should be True. (default=False)
Returns:
None or dict or str
Return None if the images are equal within the given tolerance.
If the images differ, the return value depends on in_decorator.
If in_decorator is true, a dict with the following entries is
returned:
rms: The RMS of the image difference.
expected: The filename of the expected image.
actual: The filename of the actual image.
diff_image: The filename of the difference image.
tol: The comparison tolerance.
Otherwise, a human-readable multi-line string representation of this
information is returned.
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.
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,...]:
Compare images generated by the test with those specified in
baseline_images, which must correspond, else an ImageComparisonFailure
exception will be raised.
Parameters:
baseline_imageslist or None
A list of strings specifying the names of the images generated by
calls to Figure.savefig.
If None, the test function must use the baseline_images fixture,
either as a parameter or with pytest.mark.usefixtures. This value is
only allowed when using pytest.
extensionsNone or list of str
The list of extensions to test, e.g. ['png','pdf'].
If None, defaults to all supported extensions: png, pdf, and svg.
When testing a single extension, it can be directly included in the
names passed to baseline_images. In that case, extensions must not
be set.
In order to keep the size of the test suite from ballooning, we only
include the svg or pdf outputs if the test is explicitly
exercising a feature dependent on that backend (see also the
check_figures_equal decorator for that purpose).
tolfloat, default: 0
The RMS threshold above which the test is considered failed.
Due to expected small differences in floating-point calculations, on
32-bit systems an additional 0.06 is added to this threshold.
freetype_versionstr or tuple
The expected freetype version or range of versions for this test to
pass.
remove_textbool
Remove the title and tick text from the figure before comparison. This
is useful to make the baseline images independent of variations in text
rendering between different versions of FreeType.
This does not remove other, more deliberate, text, such as legends and
annotations.
savefig_kwargdict
Optional arguments that are passed to the savefig method.
stylestr, dict, or list
The optional style(s) to apply to the image test. The test itself
can also apply additional styles if desired. Defaults to ["classic","_classic_test_patch"].