-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathtest_pytest_mpl.py
284 lines (213 loc) · 8.11 KB
/
test_pytest_mpl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import os
import sys
import subprocess
from distutils.version import LooseVersion
import pytest
import matplotlib
import matplotlib.pyplot as plt
MPL_VERSION = LooseVersion(matplotlib.__version__)
MPL_LT_2 = LooseVersion(matplotlib.__version__) < LooseVersion("2.0")
baseline_dir = 'baseline'
if MPL_VERSION >= LooseVersion('2'):
baseline_subdir = '2.0.x'
elif MPL_VERSION >= LooseVersion('1.5'):
baseline_subdir = '1.5.x'
baseline_dir_local = os.path.join(baseline_dir, baseline_subdir)
baseline_dir_remote = 'http://matplotlib.github.io/pytest-mpl/' + baseline_subdir + '/'
WIN = sys.platform.startswith('win')
# In some cases, the fonts on Windows can be quite different
DEFAULT_TOLERANCE = 10 if WIN else 2
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
tolerance=DEFAULT_TOLERANCE)
def test_succeeds():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3])
return fig
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_remote,
tolerance=DEFAULT_TOLERANCE)
def test_succeeds_remote():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3])
return fig
# The following tries an invalid URL first (or at least a URL where the baseline
# image won't exist), but should succeed with the second mirror.
@pytest.mark.mpl_image_compare(baseline_dir='http://www.python.org,' + baseline_dir_remote,
filename='test_succeeds_remote.png',
tolerance=DEFAULT_TOLERANCE)
def test_succeeds_faulty_mirror():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3])
return fig
class TestClass(object):
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
tolerance=DEFAULT_TOLERANCE)
def test_succeeds(self):
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3])
return fig
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
savefig_kwargs={'dpi': 30},
tolerance=DEFAULT_TOLERANCE)
def test_dpi():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3])
return fig
TEST_FAILING = """
import pytest
import matplotlib.pyplot as plt
@pytest.mark.mpl_image_compare
def test_fail():
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot([1,2,2])
return fig
"""
def test_fails(tmpdir):
test_file = tmpdir.join('test.py').strpath
with open(test_file, 'w') as f:
f.write(TEST_FAILING)
# If we use --mpl, it should detect that the figure is wrong
code = subprocess.call([sys.executable, '-m', 'pytest', '--mpl', test_file])
assert code != 0
# If we don't use --mpl option, the test should succeed
code = subprocess.call([sys.executable, '-m', 'pytest', test_file])
assert code == 0
TEST_OUTPUT_DIR = """
import pytest
import matplotlib.pyplot as plt
@pytest.mark.mpl_image_compare
def test_output_dir():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3])
return fig
"""
def test_output_dir(tmpdir):
test_file = tmpdir.join('test.py').strpath
with open(test_file, 'w') as f:
f.write(TEST_OUTPUT_DIR)
# When we run the test, we should get output images where we specify
output_dir = tmpdir.join('test_output_dir').strpath
code = subprocess.call([sys.executable, '-m', 'pytest',
'--mpl-results-path={0}'.format(output_dir),
'--mpl', test_file])
assert code != 0
assert os.path.exists(output_dir)
# Listdir() is to get the random name that the output for the one test is written into
assert os.path.exists(os.path.join(output_dir, os.listdir(output_dir)[0],
'test_output_dir.png'))
TEST_GENERATE = """
import pytest
import matplotlib.pyplot as plt
@pytest.mark.mpl_image_compare
def test_gen():
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot([1,2,3])
return fig
"""
def test_generate(tmpdir):
test_file = tmpdir.join('test.py').strpath
with open(test_file, 'w') as f:
f.write(TEST_GENERATE)
gen_dir = tmpdir.mkdir('spam').mkdir('egg').strpath
# If we don't generate, the test will fail
try:
subprocess.check_output([sys.executable, '-m', 'pytest', '--mpl', test_file])
except subprocess.CalledProcessError as exc:
assert b'Image file not found for comparison test' in exc.output
# If we do generate, the test should succeed and a new file will appear
code = subprocess.call([sys.executable, '-m', 'pytest',
'--mpl-generate-path={0}'.format(gen_dir),
test_file])
assert code == 0
assert os.path.exists(os.path.join(gen_dir, 'test_gen.png'))
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local, tolerance=20)
def test_tolerance():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 2])
return fig
def test_nofigure():
pass
@pytest.mark.skipif(MPL_LT_2, reason="the fivethirtyeight style is only available "
"in Matplotlib 2.0 and later")
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
style='fivethirtyeight',
tolerance=DEFAULT_TOLERANCE)
def test_base_style():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3])
return fig
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
remove_text=True)
def test_remove_text():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3])
return fig
@pytest.mark.parametrize('s', [5, 50, 500])
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
remove_text=True)
def test_parametrized(s):
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter([1, 3, 4, 3, 2], [1, 4, 3, 3, 1], s=s)
return fig
class TestClassWithSetup(object):
# Regression test for a bug that occurred when using setup_method
def setup_method(self, method):
self.x = [1, 2, 3]
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
filename='test_succeeds.png',
tolerance=DEFAULT_TOLERANCE)
def test_succeeds(self):
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(self.x)
return fig
@pytest.mark.mpl_image_compare
def test_check_equal():
fig_test, ax_test = plt.subplots()
ax_test.plot([1, 3, 5])
fig_ref, ax_ref = plt.subplots()
ax_ref.plot([0, 1, 2], [1, 3, 5])
return fig_test, fig_ref
TEST_GENERATE_2 = """
import pytest
import matplotlib.pyplot as plt
@pytest.mark.mpl_image_compare
def test_gen_two_figs():
fig_test, ax_test = plt.subplots()
ax_test.plot([1, 3, 5])
fig_ref, ax_ref = plt.subplots()
ax_ref.plot([0, 1, 2], [1, 3, 7])
return fig_test, fig_ref
"""
def test_check_unequal_fails(tmpdir):
test_file = tmpdir.join("test2.py").strpath
with open(test_file, "w") as f:
f.write(TEST_GENERATE_2)
# If we use --mpl, it should detect that the two figures are not the same
code = subprocess.call([sys.executable, "-m", "pytest", "--mpl", test_file])
assert code != 0
# If we don't use --mpl option, the test should succeed
code = subprocess.call([sys.executable, "-m", "pytest", test_file])
assert code == 0
def test_skip_generate_two_figures(tmpdir):
test_file = tmpdir.join("test2.py").strpath
with open(test_file, "w") as f:
f.write(TEST_GENERATE_2)
gen_dir = tmpdir.mkdir("spam").mkdir("egg").strpath
# If we try to generate, the test should be skipped and a new file won't appear
code = subprocess.call([sys.executable, "-m", "pytest",
"--mpl-generate-path={0}".format(gen_dir),
test_file])
assert code == 0
assert not os.path.exists(os.path.join(gen_dir, "test_gen_two_figs.png"))