Skip to content

Commit 05c71b9

Browse files
committed
Split out _raise_image_difference logic into its own function
1 parent 83266c5 commit 05c71b9

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

pytest_mpl/plugin.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,34 @@ def get_marker(item, marker_name):
177177
return item.keywords.get(marker_name)
178178

179179

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+
180208
class ImageComparison(object):
181209

182210
def __init__(self, config, baseline_dir=None, generate_dir=None, results_dir=None):
@@ -195,9 +223,7 @@ def pytest_runtest_setup(self, item):
195223
return
196224

197225
import matplotlib
198-
from matplotlib.image import imread
199226
import matplotlib.pyplot as plt
200-
from matplotlib.testing.compare import compare_images
201227
try:
202228
from matplotlib.testing.decorators import remove_ticks_and_titles
203229
except ImportError:
@@ -288,24 +314,11 @@ def item_function_wrapper(*args, **kwargs):
288314
'baseline-' + filename))
289315
shutil.copyfile(baseline_image_ref, baseline_image)
290316

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+
)
309322

310323
else:
311324

0 commit comments

Comments
 (0)