Skip to content

Commit 1dce0de

Browse files
committed
Add how-to guides
1 parent 04e9c20 commit 1dce0de

File tree

7 files changed

+482
-187
lines changed

7 files changed

+482
-187
lines changed

docs/configuration.rst

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ To enable image comparison testing, pass ``--mpl`` when running pytest.
3636
3737
pytest --mpl
3838
39-
By default, this option will enable :ref:`baseline image comparison <image-comparison-mode>`.
40-
:ref:`Baseline hash comparison <hash-comparison-mode>` can be enabled by configuring the :ref:`hash library configuration option <hash-library>`.
39+
By default, this option will enable :doc:`baseline image comparison <image_mode>`.
40+
:doc:`Baseline hash comparison <hash_mode>` can be enabled by configuring the :ref:`hash library configuration option <hash-library>`.
4141

4242
Without this option, the tests will still run.
4343
However, the returned figures will be closed without being compared to a baseline image or hash.
@@ -90,6 +90,7 @@ Directory containing baseline images
9090
The directory containing the baseline images that will be compared to the test figures.
9191
The kwarg option (``baseline_dir``) is relative to the test file, while the CLI option (``--mpl-baseline-path``) is relative to where pytest was run.
9292
Absolute paths can also be used.
93+
If the directory does not exist, it will be created along with any missing parent directories.
9394

9495
.. code:: bash
9596
@@ -196,9 +197,12 @@ File containing baseline hashes
196197
The file containing the baseline hashes that will be compared to the test figures.
197198
Both the kwarg option (``hash_library``) and the CLI option (``--mpl-hash-library``) are relative to the test file.
198199
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.
199201

200202
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:
202206

203207
Controlling the sensitivity of the comparison
204208
=============================================
@@ -212,6 +216,8 @@ It can be useful to pin versions of Matplotlib and FreeType so as to avoid autom
212216
The ``pytest-mpl`` configuration options in this section allow you to control the sensitivity of the comparison.
213217
Adjusting these options *may* allow tests to pass across a range of Matplotlib and FreeType versions.
214218

219+
.. _tolerance:
220+
215221
RMS tolerance
216222
-------------
217223
| **kwarg**: ``tolerance=<value>``
@@ -231,7 +237,7 @@ The specified tolerance value can be a float or an integer between 0 and 255.
231237
.. rubric:: How the RMS difference is calculated
232238

233239
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.
235241
If the result image and the baseline image have different aspect ratios, the test will always fail.
236242
The RMS difference is calculated as the square root of the mean of the squared differences between the result image and the baseline image.
237243
If the RMS difference is greater than the tolerance, the test will fail.
@@ -352,6 +358,9 @@ Directory to write testing artifacts to
352358
The directory to write result images and test summary reports to.
353359
The path is relative to where pytest was run.
354360
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:
355364

356365
Whether to save result images for passing tests
357366
-----------------------------------------------
@@ -370,7 +379,7 @@ This option is applied automatically when generating a HTML summary.
370379

371380
.. rubric:: Relevance to "hybrid mode"
372381

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.
374383
However, enabling this option will force a comparison to the baseline image even if the test passes hash comparison.
375384
This option is useful for always *comparing* the result images against the baseline images, while only *assessing* the tests against the hash library.
376385
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
379388
This means that they cannot update the baseline images until after the PR is merged.
380389
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.
381390

391+
.. _generate-summary:
392+
382393
Generate test summaries
383394
-----------------------
384395
| **kwarg**: ---
@@ -408,4 +419,8 @@ The summaries will be written to the :ref:`results directory <results-path>`.
408419
When generating a HTML summary, the ``--mpl-results-always`` option is automatically applied.
409420
Therefore images for passing tests will also be shown.
410421

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:
423+
424+
* :doc:`image_mode`
425+
* :doc:`hash_mode`
426+
* :doc:`hybrid_mode`

docs/hash_mode.rst

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
.. title:: Hash comparison mode
2+
3+
#####################
4+
Hash Comparison Mode
5+
#####################
6+
7+
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``:
21+
22+
.. code-block:: bash
23+
24+
pytest --mpl-generate-hash-library=your_project/tests/hashes.json
25+
26+
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:
31+
32+
.. code-block:: bash
33+
34+
pytest \
35+
--mpl-generate-hash-library=your_project/tests/mpl35_ft261.json \
36+
--mpl-generate-path=baseline
37+
38+
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:
58+
59+
.. code-block:: bash
60+
61+
pytest --mpl \
62+
--mpl-hash-library=your_project/tests/mpl35_ft261.json
63+
64+
Optionally, a HTML summary report can be generated by setting ``--mpl-generate-summary``:
65+
66+
.. code-block:: bash
67+
68+
pytest --mpl \
69+
--mpl-hash-library=your_project/tests/mpl35_ft261.json \
70+
--mpl-results-path=results \
71+
--mpl-generate-summary=html,json
72+
73+
:summary:`test_html_hashes_only`
74+
75+
The ``--mpl-results-path`` flag can be used to set the directory where the generated HTML summary will be stored.
76+
If this is not set, the images will be stored in a temporary directory.
77+
78+
Continue reading
79+
================
80+
81+
``pytest-mpl`` has many configuration options that can be used to customize the behavior of the hash comparison mode.
82+
Only a few of the most commonly used options are covered in this guide.
83+
See the :doc:`configuration options documentation <configuration>` for full details.

docs/hybrid_mode.rst

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
.. title:: Hybrid mode
2+
3+
##############################
4+
Hybrid Mode: Hashes and Images
5+
##############################
6+
7+
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``:
30+
31+
.. code-block:: bash
32+
33+
pytest \
34+
--mpl-generate-hash-library=your_project/tests/test_hashes.json \
35+
--mpl-generate-path=baseline \
36+
--mpl-results-path=results \
37+
--mpl-generate-summary=html,json
38+
39+
:summary:`test_html_generate`
40+
41+
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:
50+
51+
.. code-block:: bash
52+
53+
pytest --mpl \
54+
--mpl-hash-library=your_project/tests/mpl35_ft261.json \
55+
--mpl-baseline-path=https://raw.githubusercontent.com/your-org/your-project-figure-tests/mpl35_ft261/ \
56+
--mpl-results-path=results \
57+
--mpl-generate-summary=html,json
58+
59+
:summary:`test_html`
60+
61+
The ``--mpl-results-path`` flag can be used to set the directory where the generated HTML summary will be stored.
62+
If this is not set, the images will be stored in a temporary directory.
63+
64+
Notice that the baseline image path is set to a URL, which is the location of the baseline images in the separate repository.
65+
When the baseline image comparison is carried out, the baseline images will be downloaded from this URL.
66+
67+
It is recommended to create a CI job that updates the baseline images in the separate repository once the PR has been merged.
68+
The CI job should run when new commits are pushed to the default branch.
69+
The baseline images should only be regenerated and updated if the tests pass in the hash comparison mode.
70+
71+
.. rubric:: Aside: basic HTML summary
72+
73+
This is what the basic HTML summary looks like for the same test as above:
74+
75+
.. code-block:: bash
76+
77+
pytest --mpl \
78+
--mpl-hash-library=your_project/tests/mpl35_ft261.json \
79+
--mpl-baseline-path=https://raw.githubusercontent.com/your-org/your-project-figure-tests/mpl35_ft261/ \
80+
--mpl-results-path=results \
81+
--mpl-generate-summary=basic-html,json
82+
83+
:summary:`test_basic_html`
84+
85+
Continue reading
86+
================
87+
88+
``pytest-mpl`` has many configuration options that can be used to customize the behavior of the hybrid mode.
89+
Only a few of the most commonly used options are covered in this guide.
90+
See the :doc:`configuration options documentation <configuration>` for full details.

docs/image_mode.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
.. title:: Image comparison mode
2+
3+
#####################
4+
Image Comparison Mode
5+
#####################
6+
7+
This how-to guide will show you how to use the image comparison mode of ``pytest-mpl``.
8+
This is the default mode when ``pytest-mpl`` is invoked with ``--mpl``.
9+
10+
In this mode, ``pytest-mpl`` will compare the images generated by the test with a baseline image.
11+
If the images are different, :ref:`to a specified RMS tolerance <tolerance>`, the test will fail.
12+
The test will also fail if the baseline image is not found or if the test does not generate a figure.
13+
If the figure has a different aspect ratio to the baseline image, the test will also fail.
14+
15+
Generating baseline images
16+
==========================
17+
18+
Once a suite of image comparison tests have been written, baseline images should be generated by setting ``--mpl-generate-path``:
19+
20+
.. code-block:: bash
21+
22+
pytest --mpl-generate-path=your_project/tests/baseline
23+
24+
You should choose a directory within you repository to store the baseline images.
25+
The generated images should be checked to ensure they are as expected.
26+
The images should then be committed to the repository.
27+
28+
To assist with inspecting the generated images, a HTML summary report can be generated by setting ``--mpl-generate-summary``:
29+
30+
.. code-block:: bash
31+
32+
pytest \
33+
--mpl-generate-path=your_project/tests/baseline \
34+
--mpl-generate-summary=html,json
35+
36+
:summary:`test_html_generate_images_only`
37+
38+
Running image comparison tests
39+
==============================
40+
41+
When running the tests, the ``--mpl`` flag should be used to enable baseline image comparison testing:
42+
43+
.. code-block:: bash
44+
45+
pytest --mpl \
46+
--mpl-baseline-path=your_project/tests/baseline
47+
48+
The :ref:`baseline path <baseline-dir>` should be set to the directory containing the baseline images.
49+
If the baseline images are not found, the test will fail.
50+
51+
Optionally, a HTML summary report can be generated by setting ``--mpl-generate-summary``:
52+
53+
.. code-block:: bash
54+
55+
pytest --mpl \
56+
--mpl-baseline-path=your_project/tests/baseline \
57+
--mpl-results-path=results \
58+
--mpl-generate-summary=html,json
59+
60+
:summary:`test_html_images_only`
61+
62+
The ``--mpl-results-path`` flag can be used to set the directory where the generated HTML summary will be stored.
63+
If this is not set, the images will be stored in a temporary directory.
64+
65+
Continue reading
66+
================
67+
68+
``pytest-mpl`` has many configuration options that can be used to customize the behavior of the image comparison mode.
69+
Only a few of the most commonly used options are covered in this guide.
70+
See the :doc:`configuration options documentation <configuration>` for full details.

docs/index.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
installing
99
usage
10+
image_mode
11+
hash_mode
12+
hybrid_mode
1013
configuration
11-
summaries
1214

1315
##################################
1416
pytest-mpl |release| documentation
@@ -57,21 +59,25 @@ Learning resources
5759
Tutorials
5860
^^^
5961

60-
- :doc:`Basic usage <usage>`
62+
- :doc:`Get started <usage>`
6163

6264
.. grid-item-card::
6365
:padding: 2
6466

6567
How-tos
6668
^^^
6769

70+
- :doc:`Image comparison mode <image_mode>`
71+
- :doc:`Hash comparison mode <hash_mode>`
72+
- :doc:`Hybrid mode <hybrid_mode>`
6873

6974
.. grid-item-card::
7075
:padding: 2
7176

7277
Understand how pytest-mpl works
7378
^^^
7479

80+
Explanatory information is included where relevant throughout the documentation.
7581

7682
.. grid-item-card::
7783
:padding: 2
@@ -80,14 +86,12 @@ Learning resources
8086
^^^
8187

8288
- :doc:`Configuration <configuration>`
83-
- :doc:`Summary Reports <summaries>`
84-
8589

8690
************
8791
Contributing
8892
************
8993

90-
pytest-mpl is a community project maintained for and by its users.
94+
``pytest-mpl`` is a community project maintained for and by its users.
9195
There are many ways you can help!
9296

9397
- Report a bug or request a feature `on GitHub <https://github.com/matplotlib/pytest-mpl/issues>`__

0 commit comments

Comments
 (0)