@@ -177,6 +177,34 @@ def get_marker(item, marker_name):
177
177
return item .keywords .get (marker_name )
178
178
179
179
180
+ def _raise_on_image_difference (expected , actual , tol ):
181
+ """
182
+ Based on matplotlib.testing.decorators._raise_on_image_difference
183
+
184
+ Compare image size ourselves since the Matplotlib
185
+ exception is a bit cryptic in this case and doesn't show
186
+ the filenames
187
+ """
188
+ from matplotlib .image import imread
189
+ from matplotlib .testing .compare import compare_images
190
+
191
+ expected_shape = imread (expected ).shape [:2 ]
192
+ actual_shape = imread (actual ).shape [:2 ]
193
+ if expected_shape != actual_shape :
194
+ error = SHAPE_MISMATCH_ERROR .format (expected_path = expected ,
195
+ expected_shape = expected_shape ,
196
+ actual_path = actual ,
197
+ actual_shape = actual_shape )
198
+ pytest .fail (error , pytrace = False )
199
+
200
+ msg = compare_images (expected , actual , tol = tol )
201
+
202
+ if msg is None :
203
+ shutil .rmtree (os .path .dirname (expected ))
204
+ else :
205
+ pytest .fail (msg , pytrace = False )
206
+
207
+
180
208
class ImageComparison (object ):
181
209
182
210
def __init__ (self , config , baseline_dir = None , generate_dir = None , results_dir = None ):
@@ -195,9 +223,7 @@ def pytest_runtest_setup(self, item):
195
223
return
196
224
197
225
import matplotlib
198
- from matplotlib .image import imread
199
226
import matplotlib .pyplot as plt
200
- from matplotlib .testing .compare import compare_images
201
227
try :
202
228
from matplotlib .testing .decorators import remove_ticks_and_titles
203
229
except ImportError :
@@ -288,24 +314,11 @@ def item_function_wrapper(*args, **kwargs):
288
314
'baseline-' + filename ))
289
315
shutil .copyfile (baseline_image_ref , baseline_image )
290
316
291
- # Compare image size ourselves since the Matplotlib
292
- # exception is a bit cryptic in this case and doesn't show
293
- # the filenames
294
- expected_shape = imread (baseline_image ).shape [:2 ]
295
- actual_shape = imread (test_image ).shape [:2 ]
296
- if expected_shape != actual_shape :
297
- error = SHAPE_MISMATCH_ERROR .format (expected_path = baseline_image ,
298
- expected_shape = expected_shape ,
299
- actual_path = test_image ,
300
- actual_shape = actual_shape )
301
- pytest .fail (error , pytrace = False )
302
-
303
- msg = compare_images (baseline_image , test_image , tol = tolerance )
304
-
305
- if msg is None :
306
- shutil .rmtree (result_dir )
307
- else :
308
- pytest .fail (msg , pytrace = False )
317
+ _raise_on_image_difference (
318
+ expected = baseline_image ,
319
+ actual = test_image ,
320
+ tol = tolerance
321
+ )
309
322
310
323
else :
311
324
0 commit comments