|
| 1 | +.. title:: Configuration |
| 2 | + |
| 3 | +############# |
| 4 | +Configuration |
| 5 | +############# |
| 6 | + |
| 7 | +Tolerance |
| 8 | +^^^^^^^^^ |
| 9 | + |
| 10 | +The RMS tolerance for the image comparison (which defaults to 2) can be |
| 11 | +specified in the ``mpl_image_compare`` decorator with the ``tolerance`` |
| 12 | +argument: |
| 13 | + |
| 14 | +.. code:: python |
| 15 | +
|
| 16 | + @pytest.mark.mpl_image_compare(tolerance=20) |
| 17 | + def test_image(): |
| 18 | + ... |
| 19 | +
|
| 20 | +Savefig options |
| 21 | +^^^^^^^^^^^^^^^ |
| 22 | + |
| 23 | +You can pass keyword arguments to ``savefig`` by using |
| 24 | +``savefig_kwargs`` in the ``mpl_image_compare`` decorator: |
| 25 | + |
| 26 | +.. code:: python |
| 27 | +
|
| 28 | + @pytest.mark.mpl_image_compare(savefig_kwargs={'dpi':300}) |
| 29 | + def test_image(): |
| 30 | + ... |
| 31 | +
|
| 32 | +Baseline images |
| 33 | +^^^^^^^^^^^^^^^ |
| 34 | + |
| 35 | +The baseline directory (which defaults to ``baseline`` ) and the |
| 36 | +filename of the plot (which defaults to the name of the test with a |
| 37 | +``.png`` suffix) can be customized with the ``baseline_dir`` and |
| 38 | +``filename`` arguments in the ``mpl_image_compare`` decorator: |
| 39 | + |
| 40 | +.. code:: python |
| 41 | +
|
| 42 | + @pytest.mark.mpl_image_compare(baseline_dir='baseline_images', |
| 43 | + filename='other_name.png') |
| 44 | + def test_image(): |
| 45 | + ... |
| 46 | +
|
| 47 | +The baseline directory in the decorator above will be interpreted as |
| 48 | +being relative to the test file. Note that the baseline directory can |
| 49 | +also be a URL (which should start with ``http://`` or ``https://`` and |
| 50 | +end in a slash). If you want to specify mirrors, set ``baseline_dir`` to |
| 51 | +a comma-separated list of URLs (real commas in the URL should be encoded |
| 52 | +as ``%2C``). |
| 53 | + |
| 54 | +Finally, you can also set a custom baseline directory globally when |
| 55 | +running tests by running ``pytest`` with:: |
| 56 | + |
| 57 | + pytest --mpl --mpl-baseline-path=baseline_images |
| 58 | + |
| 59 | +This directory will be interpreted as being relative to where pytest |
| 60 | +is run. However, if the ``--mpl-baseline-relative`` option is also |
| 61 | +included, this directory will be interpreted as being relative to |
| 62 | +the current test directory. |
| 63 | +In addition, if both this option and the ``baseline_dir`` |
| 64 | +option in the ``mpl_image_compare`` decorator are used, the one in the |
| 65 | +decorator takes precedence. |
| 66 | + |
| 67 | +Results always |
| 68 | +^^^^^^^^^^^^^^ |
| 69 | + |
| 70 | +By default, result images are only saved for tests that fail. |
| 71 | +Passing ``--mpl-results-always`` to pytest will force result images |
| 72 | +to be saved for all tests, even for tests that pass. |
| 73 | + |
| 74 | +When in **hybrid mode**, even if a test passes hash comparison, |
| 75 | +a comparison to the baseline image will also be carried out, |
| 76 | +with the baseline image and diff image (if image comparison fails) |
| 77 | +saved for all tests. This secondary comparison will not affect |
| 78 | +the success status of the test. |
| 79 | + |
| 80 | +This option is useful for always *comparing* the result images against |
| 81 | +the baseline images, while only *assessing* the tests against the |
| 82 | +hash library. |
| 83 | +If you only update your baseline images after merging a PR, this |
| 84 | +option means that the generated summary will always show how the |
| 85 | +PR affects the baseline images, with the success status of each |
| 86 | +test (based on the hash library) also shown in the generated |
| 87 | +summary. This option is applied automatically when generating |
| 88 | +a HTML summary. |
| 89 | + |
| 90 | +When the ``--mpl-results-always`` option is active, and some hash |
| 91 | +comparison tests are performed, a hash library containing all the |
| 92 | +result hashes will also be saved to the root of the results directory. |
| 93 | +The filename will be extracted from ``--mpl-generate-hash-library``, |
| 94 | +``--mpl-hash-library`` or ``hash_library=`` in that order. |
| 95 | + |
| 96 | +Base style |
| 97 | +^^^^^^^^^^ |
| 98 | + |
| 99 | +By default, tests will be run using the Matplotlib 'classic' style |
| 100 | +(ignoring any locally defined RC parameters). This can be overridden by |
| 101 | +using the ``style`` argument: |
| 102 | + |
| 103 | +.. code:: python |
| 104 | +
|
| 105 | + @pytest.mark.mpl_image_compare(style='fivethirtyeight') |
| 106 | + def test_image(): |
| 107 | + ... |
| 108 | +
|
| 109 | +Package version dependencies |
| 110 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 111 | +Different versions of Matplotlib and FreeType may result in slightly |
| 112 | +different images. When testing on multiple platforms or as part of a |
| 113 | +pipeline, it is important to ensure that the versions of these |
| 114 | +packages match the versions used to generate the images used for |
| 115 | +comparison. It can be useful to pin versions of Matplotlib and FreeType |
| 116 | +so as to avoid automatic updates that fail tests. |
| 117 | + |
| 118 | +Removing text |
| 119 | +^^^^^^^^^^^^^ |
| 120 | + |
| 121 | +If you are running a test for which you are not interested in comparing |
| 122 | +the text labels, you can use the ``remove_text`` argument to the |
| 123 | +decorator: |
| 124 | + |
| 125 | +.. code:: python |
| 126 | +
|
| 127 | + @pytest.mark.mpl_image_compare(remove_text=True) |
| 128 | + def test_image(): |
| 129 | + ... |
| 130 | +
|
| 131 | +This will make the test insensitive to changes in e.g. the freetype |
| 132 | +library. |
| 133 | + |
| 134 | +Test failure example |
| 135 | +-------------------- |
| 136 | + |
| 137 | +If the images produced by the tests are correct, then the test will |
| 138 | +pass, but if they are not, the test will fail with a message similar to |
| 139 | +the following:: |
| 140 | + |
| 141 | + E Exception: Error: Image files did not match. |
| 142 | + E RMS Value: 142.2287807767823 |
| 143 | + E Expected: |
| 144 | + E /var/folders/zy/t1l3sx310d3d6p0kyxqzlrnr0000gr/T/tmp4h4oxr7y/baseline-coords_overlay_auto_coord_meta.png |
| 145 | + E Actual: |
| 146 | + E /var/folders/zy/t1l3sx310d3d6p0kyxqzlrnr0000gr/T/tmp4h4oxr7y/coords_overlay_auto_coord_meta.png |
| 147 | + E Difference: |
| 148 | + E /var/folders/zy/t1l3sx310d3d6p0kyxqzlrnr0000gr/T/tmp4h4oxr7y/coords_overlay_auto_coord_meta-failed-diff.png |
| 149 | + E Tolerance: |
| 150 | + E 10 |
| 151 | + |
| 152 | +The image paths included in the exception are then available for |
| 153 | +inspection: |
| 154 | + |
| 155 | ++----------------+----------------+-------------+ |
| 156 | +| Expected | Actual | Difference | |
| 157 | ++================+================+=============+ |
| 158 | +| |expected| | |actual| | |diff| | |
| 159 | ++----------------+----------------+-------------+ |
| 160 | + |
| 161 | +In this case, the differences are very clear, while in some cases it may |
| 162 | +be necessary to use the difference image, or blink the expected and |
| 163 | +actual images, in order to see what changed. |
| 164 | + |
| 165 | +The default tolerance is 2, which is very strict. In some cases, you may |
| 166 | +want to relax this to account for differences in fonts across different |
| 167 | +systems. |
| 168 | + |
| 169 | +By default, the expected, actual and difference files are written to a |
| 170 | +temporary directory with a non-deterministic path. If you want to instead |
| 171 | +write them to a specific directory, you can use:: |
| 172 | + |
| 173 | + pytest --mpl --mpl-results-path=results |
| 174 | + |
| 175 | +The ``results`` directory will then contain one sub-directory per test, and each |
| 176 | +sub-directory will contain the three files mentioned above. If you are using a |
| 177 | +continuous integration service, you can then use the option to upload artifacts |
| 178 | +to upload these results to somewhere where you can view them. For more |
| 179 | +information, see: |
| 180 | + |
| 181 | +* `Uploading artifacts on Travis-CI <https://docs.travis-ci.com/user/uploading-artifacts/>`_ |
| 182 | +* `Build Artifacts (CircleCI) <https://circleci.com/docs/1.0/build-artifacts/>`_ |
| 183 | +* `Packaging Artifacts (AppVeyor) <https://www.appveyor.com/docs/packaging-artifacts/>`_ |
| 184 | + |
| 185 | +.. |expected| image:: images/baseline-coords_overlay_auto_coord_meta.png |
| 186 | +.. |actual| image:: images/coords_overlay_auto_coord_meta.png |
| 187 | +.. |diff| image:: images/coords_overlay_auto_coord_meta-failed-diff.png |
0 commit comments