|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import pytest |
| 3 | + |
| 4 | + |
| 5 | +def plot(line, **kwargs): |
| 6 | + fig = plt.figure() |
| 7 | + ax = fig.add_subplot(1, 1, 1) |
| 8 | + ax.plot(line, **kwargs) |
| 9 | + return fig |
| 10 | + |
| 11 | + |
| 12 | +# ### Test all permutations of: |
| 13 | +# baseline hash: match, diff, or missing |
| 14 | +# baseline image: match, diff, or missing |
| 15 | + |
| 16 | +# hash match |
| 17 | + |
| 18 | +@pytest.mark.mpl_image_compare() |
| 19 | +def test_hmatch_imatch(): |
| 20 | + return plot([1, 2, 3, 4]) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.mpl_image_compare() |
| 24 | +def test_hmatch_idiff(): |
| 25 | + return plot([1, 3, 2, 4]) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.mpl_image_compare() |
| 29 | +def test_hmatch_imissing(): |
| 30 | + return plot([4, 3, 2, 1]) |
| 31 | + |
| 32 | + |
| 33 | +# hash diff |
| 34 | + |
| 35 | +@pytest.mark.mpl_image_compare() |
| 36 | +def test_hdiff_imatch(): |
| 37 | + return plot([1, 4, 2, 3]) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.mpl_image_compare() |
| 41 | +def test_hdiff_idiff(): |
| 42 | + return plot([1, 2, 4, 3]) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.mpl_image_compare() |
| 46 | +def test_hdiff_imissing(): |
| 47 | + return plot([3, 2, 4, 1]) |
| 48 | + |
| 49 | + |
| 50 | +# hash missing |
| 51 | + |
| 52 | +@pytest.mark.mpl_image_compare() |
| 53 | +def test_hmissing_imatch(): |
| 54 | + return plot([1, 3, 4, 2]) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.mpl_image_compare() |
| 58 | +def test_hmissing_idiff(): |
| 59 | + return plot([1, 4, 3, 2]) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.mpl_image_compare() |
| 63 | +def test_hmissing_imissing(): |
| 64 | + return plot([2, 4, 3, 1]) |
| 65 | + |
| 66 | + |
| 67 | +# ### Specialized tests |
| 68 | + |
| 69 | +# Tolerance: high to force image match |
| 70 | +@pytest.mark.mpl_image_compare(tolerance=200) |
| 71 | +def test_hdiff_imatch_tolerance(): |
| 72 | + return plot([1, 2, 3, 4], linestyle='--') |
| 73 | + |
| 74 | + |
| 75 | +# Tolerance: non-default to verify option recorded in JSON |
| 76 | +@pytest.mark.mpl_image_compare(tolerance=3) |
| 77 | +def test_hdiff_idiff_tolerance(): |
| 78 | + return plot([1, 2, 3, 4], linestyle='--') |
| 79 | + |
| 80 | + |
| 81 | +# Savefig kwargs |
| 82 | +@pytest.mark.mpl_image_compare(savefig_kwargs={'facecolor': 'r'}) |
| 83 | +def test_hdiff_imatch_savefig(): |
| 84 | + return plot([1, 2, 3, 4]) |
| 85 | + |
| 86 | + |
| 87 | +# Different baseline directory |
| 88 | +# TODO: Test with a remote `baseline_dir` |
| 89 | +@pytest.mark.mpl_image_compare(baseline_dir='baseline/other') |
| 90 | +def test_hdiff_imatch_baselinedir(): |
| 91 | + return plot([4, 2, 1, 4]) |
| 92 | + |
| 93 | + |
| 94 | +# Different filename |
| 95 | +@pytest.mark.mpl_image_compare(filename='test_hdiff_imatch_filename_other.png') |
| 96 | +def test_hdiff_imatch_filename(): |
| 97 | + return plot([4, 2, 1, 4]) |
| 98 | + |
| 99 | + |
| 100 | +# Different hash library |
| 101 | +@pytest.mark.mpl_image_compare(hash_library='hashes/other/other.json') |
| 102 | +def test_hdiff_imatch_filename(): |
| 103 | + return plot([4, 2, 1, 4]) |
| 104 | + |
| 105 | + |
| 106 | +# Different style |
| 107 | +@pytest.mark.mpl_image_compare(style='fivethirtyeight') |
| 108 | +def test_hdiff_imatch_style(): |
| 109 | + return plot([4, 2, 1, 4]) |
| 110 | + |
| 111 | + |
| 112 | +# Remove text |
| 113 | +@pytest.mark.mpl_image_compare(remove_text=True) |
| 114 | +def test_hdiff_imatch_removetext(): |
| 115 | + return plot([4, 2, 1, 4]) |
0 commit comments