22
22
23
23
WIN = sys .platform .startswith ('win' )
24
24
25
+ # In some cases, the fonts on Windows can be quite different
26
+ DEFAULT_TOLERANCE = 10 if WIN else None
25
27
26
- @pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_local )
28
+
29
+ @pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_local ,
30
+ tolerance = DEFAULT_TOLERANCE )
27
31
def test_succeeds ():
28
32
fig = plt .figure ()
29
33
ax = fig .add_subplot (1 , 1 , 1 )
30
34
ax .plot ([1 , 2 , 3 ])
31
35
return fig
32
36
33
37
34
- @pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_remote )
38
+ @pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_remote ,
39
+ tolerance = DEFAULT_TOLERANCE )
35
40
def test_succeeds_remote ():
36
41
fig = plt .figure ()
37
42
ax = fig .add_subplot (1 , 1 , 1 )
@@ -42,7 +47,8 @@ def test_succeeds_remote():
42
47
# The following tries an invalid URL first (or at least a URL where the baseline
43
48
# image won't exist), but should succeed with the second mirror.
44
49
@pytest .mark .mpl_image_compare (baseline_dir = 'http://www.python.org,' + baseline_dir_remote ,
45
- filename = 'test_succeeds_remote.png' )
50
+ filename = 'test_succeeds_remote.png' ,
51
+ tolerance = DEFAULT_TOLERANCE )
46
52
def test_succeeds_faulty_mirror ():
47
53
fig = plt .figure ()
48
54
ax = fig .add_subplot (1 , 1 , 1 )
@@ -52,15 +58,18 @@ def test_succeeds_faulty_mirror():
52
58
53
59
class TestClass (object ):
54
60
55
- @pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_local )
61
+ @pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_local ,
62
+ tolerance = DEFAULT_TOLERANCE )
56
63
def test_succeeds (self ):
57
64
fig = plt .figure ()
58
65
ax = fig .add_subplot (1 , 1 , 1 )
59
66
ax .plot ([1 , 2 , 3 ])
60
67
return fig
61
68
62
69
63
- @pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_local , savefig_kwargs = {'dpi' : 30 })
70
+ @pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_local ,
71
+ savefig_kwargs = {'dpi' : 30 },
72
+ tolerance = DEFAULT_TOLERANCE )
64
73
def test_dpi ():
65
74
fig = plt .figure ()
66
75
ax = fig .add_subplot (1 , 1 , 1 )
@@ -87,11 +96,11 @@ def test_fails(tmpdir):
87
96
f .write (TEST_FAILING )
88
97
89
98
# If we use --mpl, it should detect that the figure is wrong
90
- code = subprocess .call ('{0} -m pytest --mpl {1}' . format ( sys . executable , test_file ), shell = True )
99
+ code = subprocess .call ([ sys . executable , '-m' , ' pytest' , ' --mpl' , test_file ] )
91
100
assert code != 0
92
101
93
102
# If we don't use --mpl option, the test should succeed
94
- code = subprocess .call ('{0} -m pytest {1}' . format ( sys . executable , test_file ), shell = True )
103
+ code = subprocess .call ([ sys . executable , '-m' , ' pytest' , test_file ] )
95
104
assert code == 0
96
105
97
106
@@ -114,9 +123,9 @@ def test_output_dir(tmpdir):
114
123
115
124
# When we run the test, we should get output images where we specify
116
125
output_dir = tmpdir .join ('test_output_dir' ).strpath
117
- code = subprocess .call ('{0} -m pytest --mpl-results-path={1} --mpl {2}'
118
- .format (sys . executable , output_dir , test_file ),
119
- shell = True )
126
+ code = subprocess .call ([ sys . executable , '-m' , ' pytest' ,
127
+ '--mpl-results-path={0}' .format (output_dir ),
128
+ '--mpl' , test_file ] )
120
129
121
130
assert code != 0
122
131
assert os .path .exists (output_dir )
@@ -138,12 +147,6 @@ def test_gen():
138
147
"""
139
148
140
149
141
- # TODO: We skip the following test on Windows since the first subprocess calls.
142
- # This should be fixed in the long term, but is not critical since we already
143
- # test this on Linux.
144
-
145
-
146
- @pytest .mark .skipif ("WIN" )
147
150
def test_generate (tmpdir ):
148
151
149
152
test_file = tmpdir .join ('test.py' ).strpath
@@ -153,14 +156,15 @@ def test_generate(tmpdir):
153
156
gen_dir = tmpdir .mkdir ('spam' ).mkdir ('egg' ).strpath
154
157
155
158
# If we don't generate, the test will fail
156
- p = subprocess . Popen ( '{0} -m pytest --mpl {1}' . format ( sys . executable , test_file ), shell = True ,
157
- stdout = subprocess . PIPE , stderr = subprocess . PIPE )
158
- p . wait ()
159
- assert b'Image file not found for comparison test' in p . stdout . read ()
159
+ try :
160
+ subprocess . check_output ([ sys . executable , '-m' , 'pytest' , '--mpl' , test_file ] )
161
+ except subprocess . CalledProcessError as exc :
162
+ assert b'Image file not found for comparison test' in exc . output
160
163
161
164
# If we do generate, the test should succeed and a new file will appear
162
- code = subprocess .call ('{0} -m pytest --mpl-generate-path={1} {2}'
163
- .format (sys .executable , gen_dir , test_file ), shell = True )
165
+ code = subprocess .call ([sys .executable , '-m' , 'pytest' ,
166
+ '--mpl-generate-path={0}' .format (gen_dir ),
167
+ test_file ])
164
168
assert code == 0
165
169
assert os .path .exists (os .path .join (gen_dir , 'test_gen.png' ))
166
170
@@ -180,7 +184,8 @@ def test_nofigure():
180
184
@pytest .mark .skipif (MPL_LT_2 , reason = "the fivethirtyeight style is only available "
181
185
"in Matplotlib 2.0 and later" )
182
186
@pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_local ,
183
- style = 'fivethirtyeight' )
187
+ style = 'fivethirtyeight' ,
188
+ tolerance = DEFAULT_TOLERANCE )
184
189
def test_base_style ():
185
190
fig = plt .figure ()
186
191
ax = fig .add_subplot (1 , 1 , 1 )
@@ -215,7 +220,8 @@ def setup_method(self, method):
215
220
self .x = [1 , 2 , 3 ]
216
221
217
222
@pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_local ,
218
- filename = 'test_succeeds.png' )
223
+ filename = 'test_succeeds.png' ,
224
+ tolerance = DEFAULT_TOLERANCE )
219
225
def test_succeeds (self ):
220
226
fig = plt .figure ()
221
227
ax = fig .add_subplot (1 , 1 , 1 )
0 commit comments