Skip to content

Commit 2796fad

Browse files
committed
Add subtests
1 parent 7e95ae8 commit 2796fad

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

tests/subtests/subtest.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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])

tests/subtests/test_subtest.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
RESULT_LIBRARY = Path(__file__).parent / 'result_hashes' / (VERSION_ID + ".json")
1919
HASH_LIBRARY_FLAG = rf'--mpl-hash-library={HASH_LIBRARY}'
2020

21+
BASELINE_IMAGES_FLAG = '--mpl-baseline-path=baseline'
22+
2123
TEST_FILE = Path(__file__).parent / 'subtest.py'
2224

2325
# Global settings to update baselines when running pytest
@@ -101,3 +103,48 @@ def run_subtest(baseline_summary_name, tmp_path, args, summaries=None, xfail=Tru
101103

102104
# Ensure reported images exist
103105
assert_existence(result_summary, path=results_path)
106+
107+
108+
def test_default(tmp_path):
109+
run_subtest('test_default', tmp_path, [])
110+
111+
112+
def test_hash(tmp_path):
113+
run_subtest('test_hash', tmp_path, [HASH_LIBRARY_FLAG])
114+
115+
116+
def test_hybrid(tmp_path):
117+
run_subtest('test_hybrid', tmp_path, [HASH_LIBRARY_FLAG, BASELINE_IMAGES_FLAG])
118+
119+
120+
def test_results_always(tmp_path):
121+
run_subtest('test_results_always', tmp_path,
122+
[HASH_LIBRARY_FLAG, BASELINE_IMAGES_FLAG, '--mpl-results-always'])
123+
124+
125+
def test_html(tmp_path):
126+
run_subtest('test_results_always', tmp_path,
127+
[HASH_LIBRARY_FLAG, BASELINE_IMAGES_FLAG], summaries=['html'])
128+
assert (tmp_path / 'results' / 'fig_comparison.html').exists()
129+
assert (tmp_path / 'results' / 'extra.js').exists()
130+
assert (tmp_path / 'results' / 'styles.css').exists()
131+
132+
133+
def test_html_hashes_only(tmp_path):
134+
run_subtest('test_html_hashes_only', tmp_path, [HASH_LIBRARY_FLAG], summaries=['html'])
135+
assert (tmp_path / 'results' / 'fig_comparison.html').exists()
136+
assert (tmp_path / 'results' / 'extra.js').exists()
137+
assert (tmp_path / 'results' / 'styles.css').exists()
138+
139+
140+
def test_html_images_only(tmp_path):
141+
run_subtest('test_html_images_only', tmp_path, [], summaries=['html'])
142+
assert (tmp_path / 'results' / 'fig_comparison.html').exists()
143+
assert (tmp_path / 'results' / 'extra.js').exists()
144+
assert (tmp_path / 'results' / 'styles.css').exists()
145+
146+
147+
def test_basic_html(tmp_path):
148+
run_subtest('test_results_always', tmp_path,
149+
[HASH_LIBRARY_FLAG, BASELINE_IMAGES_FLAG], summaries=['basic-html'])
150+
assert (tmp_path / 'results' / 'fig_comparison_basic.html').exists()

0 commit comments

Comments
 (0)