You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The file containing the baseline hashes that will be compared to the test figures.
197
198
Both the kwarg option (``hash_library``) and the CLI option (``--mpl-hash-library``) are relative to the test file.
198
199
The file must be a JSON file in the same format as one generated by ``--mpl-generate-hash-library``.
200
+
If its directory does not exist, it will be created along with any missing parent directories.
199
201
200
202
Configuring this option disables baseline image comparison.
201
-
If you want to enable both hash and baseline image comparison, which we call :ref:`"hybrid mode" <hybrid-mode>`, you must explicitly set the :ref:`baseline directory configuration option <baseline-dir>`.
203
+
If you want to enable both hash and baseline image comparison, which we call :doc:`"hybrid mode" <hybrid_mode>`, you must explicitly set the :ref:`baseline directory configuration option <baseline-dir>`.
204
+
205
+
.. _controlling-sensitivity:
202
206
203
207
Controlling the sensitivity of the comparison
204
208
=============================================
@@ -212,6 +216,8 @@ It can be useful to pin versions of Matplotlib and FreeType so as to avoid autom
212
216
The ``pytest-mpl`` configuration options in this section allow you to control the sensitivity of the comparison.
213
217
Adjusting these options *may* allow tests to pass across a range of Matplotlib and FreeType versions.
214
218
219
+
.. _tolerance:
220
+
215
221
RMS tolerance
216
222
-------------
217
223
|**kwarg**: ``tolerance=<value>``
@@ -231,7 +237,7 @@ The specified tolerance value can be a float or an integer between 0 and 255.
231
237
.. rubric:: How the RMS difference is calculated
232
238
233
239
Result images and baseline images are *always* converted to PNG files before comparison.
234
-
Each are read as a 4-dimensional array of RGBA pixels (or just RGB if fully opaque) with values between 0 and 255.
240
+
Each are read as an array of RGBA pixels (or just RGB if fully opaque) with values between 0 and 255.
235
241
If the result image and the baseline image have different aspect ratios, the test will always fail.
236
242
The RMS difference is calculated as the square root of the mean of the squared differences between the result image and the baseline image.
237
243
If the RMS difference is greater than the tolerance, the test will fail.
@@ -352,6 +358,9 @@ Directory to write testing artifacts to
352
358
The directory to write result images and test summary reports to.
353
359
The path is relative to where pytest was run.
354
360
Absolute paths are also supported.
361
+
If the directory does not exist, it will be created along with any missing parent directories.
362
+
363
+
.. _results-always:
355
364
356
365
Whether to save result images for passing tests
357
366
-----------------------------------------------
@@ -370,7 +379,7 @@ This option is applied automatically when generating a HTML summary.
370
379
371
380
.. rubric:: Relevance to "hybrid mode"
372
381
373
-
When in :ref:`"hybrid mode" <hybrid-mode>`, a baseline image comparison is only performed if the test fails hash comparison.
382
+
When in :doc:`"hybrid mode" <hybrid_mode>`, a baseline image comparison is only performed if the test fails hash comparison.
374
383
However, enabling this option will force a comparison to the baseline image even if the test passes hash comparison.
375
384
This option is useful for always *comparing* the result images against the baseline images, while only *assessing* the tests against the hash library.
376
385
This secondary comparison will **not** affect the success status of the test, but any failures (including diff images) will be included in generated summary reports.
@@ -379,6 +388,8 @@ Some projects store their baseline images in a separate repository, and only kee
379
388
This means that they cannot update the baseline images until after the PR is merged.
380
389
Enabling this option allows them to ensure the hashes are correct before merging the PR, but also see how the PR affects the baseline images, as the diff images will always be shown in the HTML summary.
381
390
391
+
.. _generate-summary:
392
+
382
393
Generate test summaries
383
394
-----------------------
384
395
|**kwarg**: ---
@@ -408,4 +419,8 @@ The summaries will be written to the :ref:`results directory <results-path>`.
408
419
When generating a HTML summary, the ``--mpl-results-always`` option is automatically applied.
409
420
Therefore images for passing tests will also be shown.
410
421
411
-
For more information, see the :doc:`summary reports documentation <summaries>`.
422
+
For examples of how the summary reports look in different operating modes, see:
This how-to guide will show you how to use the hash comparison mode of ``pytest-mpl``.
8
+
9
+
In this mode, the hash of the image is compared to the hash of the baseline image.
10
+
Only the hash value of the baseline image, rather than the full image, needs to be stored in the repository.
11
+
This means that the repository size is reduced, and the images can be regenerated if necessary.
12
+
This approach does however make it more difficult to visually inspect any changes to the images.
13
+
14
+
If your goal is to not commit any images to the code repository, then you should consider using :doc:`hybrid mode <hybrid_mode>` instead.
15
+
In this mode, the hashes can be stored in the code repository, while the baseline images are stored in a separate repository and accessed through a URL when testing.
16
+
17
+
Generating baseline hashes
18
+
==========================
19
+
20
+
Once a suite of image comparison tests have been written, baseline hashes should be generated by setting ``--mpl-generate-hash-library``:
It is important to visually inspect the figures before generating baseline hashes.
27
+
So, as well as generating baseline hashes, this command runs baseline image comparison tests.
28
+
If no baseline images exist in the default directory, this command will fail.
29
+
30
+
A better option is to generate baseline images along with the baseline hashes to ensure that the images are as expected, even if you do not wish to use them for comparison:
To assist with inspecting the generated images (and hashes), a HTML summary report can be generated by setting ``--mpl-generate-summary``:
39
+
40
+
.. code-block:: bash
41
+
42
+
pytest \
43
+
--mpl-generate-hash-library=test_hashes.json \
44
+
--mpl-generate-path=baseline \
45
+
--mpl-results-path=results \
46
+
--mpl-generate-summary=html,json
47
+
48
+
:summary:`test_html_generate`
49
+
50
+
You should choose a directory within you repository to store the baseline hashes.
51
+
It's usually a good idea to encode the Matplotlib version and the FreeType version in the filename, e.g. ``mpl35_ft261.json``.
52
+
The hash library file should then be committed to the repository.
53
+
54
+
Running hash comparison tests
55
+
=============================
56
+
57
+
When running the tests, the ``--mpl`` flag should be used along with a :ref:`configured hash library path <hash-library>` to enable baseline hash comparison testing:
This how-to guide will show you how to use the hybrid mode of ``pytest-mpl``.
8
+
9
+
For a full description of the hybrid mode, see the :ref:`hybrid mode section of the get started guide <hybrid-usage>`.
10
+
In summary, hybrid mode uses both baseline images and hashes.
11
+
First, the hash of the image is compared to the hash of the baseline image.
12
+
If the hashes match, the test passes.
13
+
If the hashes do not match, the test fails.
14
+
15
+
The difference with hybrid mode is that a baseline image comparison will also be carried out if the hashes do not match, or always :ref:`if this has been configured <results-always>`.
16
+
The purpose of the additional image comparison (which does not affect the test result) is to allow the user to visually inspect the difference between the baseline image and the image generated by the test.
17
+
18
+
In order to keep the code repository size small, it is recommended to store the baseline hashes in the code repository, and the baseline images in a separate repository.
19
+
The baseline hashes should be updated where appropriate in PRs to the code repository.
20
+
However, the baseline images are not updated in these PRs.
21
+
Instead, they should be updated once the PR has been merged, preferably by a CI job.
22
+
23
+
Another benefit of only updating the baseline images once the PR has been merged is that the PR tests will show the difference between the remote baseline images and the images generated by the PR.
24
+
Even though the tests will pass when the baseline hash matches, the images will still be compared and the difference will be shown in the HTML test summary report, which is useful when reviewing the PR.
25
+
26
+
Generating baseline hashes and images
27
+
=====================================
28
+
29
+
Once a suite of image comparison tests have been written, baseline hashes and images should be generated by setting ``--mpl-generate-path`` and ``--mpl-generate-hash-library``:
Open the HTML summary file and inspect the figures to ensure that the baseline images are correct.
42
+
If they are, the baseline hashes can be committed to the code repository.
43
+
It's usually a good idea to encode the Matplotlib version and the FreeType version in the filename, e.g. ``mpl35_ft261.json``.
44
+
The baseline images should be copied to a separate repository; preferably within a version specific directory, e.g. ``mpl35_ft261/``.
45
+
46
+
Running hash comparison tests
47
+
=============================
48
+
49
+
When running the tests, the ``--mpl`` flag should be used along with a configured :ref:`hash library path <hash-library>` and :ref:`baseline image path <baseline-dir>` to enable hybrid mode testing:
0 commit comments