Skip to content

Commit 517d59b

Browse files
Merge pull request #199 from ConorMacBride/migrate-docs
Migrate docs from `README.rst` to website
2 parents e6c1308 + bc8108b commit 517d59b

15 files changed

+907
-656
lines changed

Diff for: README.rst

+40-343
Large diffs are not rendered by default.

Diff for: docs/conf.py

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
# ones.
3939
extensions = [
4040
'sample_summaries',
41+
'sphinx.ext.intersphinx',
4142
'sphinx_design',
4243
]
4344

@@ -49,6 +50,9 @@
4950
# This pattern also affects html_static_path and html_extra_path.
5051
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
5152

53+
intersphinx_mapping = {
54+
"matplotlib": ("https://matplotlib.org/stable", None),
55+
}
5256

5357
# -- Options for HTML output -------------------------------------------------
5458

Diff for: docs/configuration.rst

+386-146
Large diffs are not rendered by default.

Diff for: docs/hash_mode.rst

+83
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.

Diff for: docs/hybrid_mode.rst

+90
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.

Diff for: docs/image_mode.rst

+70
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.

Diff for: docs/index.rst

+14-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
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
1517
##################################
1618

17-
This is a plugin to facilitate image comparison for Matplotlib figures in pytest.
19+
``pytest-mpl`` is a `pytest <https://docs.pytest.org>`__ plugin to facilitate image comparison for `Matplotlib <http://www.matplotlib.org>`__ figures.
20+
21+
For each figure to test, an image is generated and then subtracted from an existing reference image.
22+
If the RMS of the residual is larger than a user-specified tolerance, the test will fail.
23+
Alternatively, the generated image can be hashed and compared to an expected value.
1824

1925
************
2026
Installation
@@ -53,21 +59,25 @@ Learning resources
5359
Tutorials
5460
^^^
5561

56-
- :doc:`Basic usage <usage>`
62+
- :doc:`Get started <usage>`
5763

5864
.. grid-item-card::
5965
:padding: 2
6066

6167
How-tos
6268
^^^
6369

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

6574
.. grid-item-card::
6675
:padding: 2
6776

6877
Understand how pytest-mpl works
6978
^^^
7079

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

7282
.. grid-item-card::
7383
:padding: 2
@@ -76,14 +86,12 @@ Learning resources
7686
^^^
7787

7888
- :doc:`Configuration <configuration>`
79-
- :doc:`Summary Reports <summaries>`
80-
8189

8290
************
8391
Contributing
8492
************
8593

86-
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.
8795
There are many ways you can help!
8896

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

0 commit comments

Comments
 (0)