Skip to content

Commit 13e8c5e

Browse files
authored
Merge pull request #79 from astrofrog/better-error-shape
Improve error message if shapes disagree
2 parents df58b06 + 1118a37 commit 13e8c5e

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pytest_mpl/plugin.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
string_types = str
5050

5151

52+
SHAPE_MISMATCH_ERROR = """Error: Image dimensions did not match.
53+
Expected shape: {expected_shape}
54+
{expected_path}
55+
Actual shape: {actual_shape}
56+
{actual_path}"""
57+
58+
5259
def _download_file(baseline, filename):
5360
# Note that baseline can be a comma-separated list of URLs that we can
5461
# then treat as mirrors
@@ -183,6 +190,7 @@ def pytest_runtest_setup(self, item):
183190
if compare is None:
184191
return
185192

193+
from PIL import Image
186194
import matplotlib
187195
import matplotlib.pyplot as plt
188196
from matplotlib.testing.compare import compare_images
@@ -272,6 +280,17 @@ def item_function_wrapper(*args, **kwargs):
272280
baseline_image = os.path.abspath(os.path.join(result_dir, 'baseline-' + filename))
273281
shutil.copyfile(baseline_image_ref, baseline_image)
274282

283+
# Compare image size ourselves since the Matplotlib exception is a bit cryptic in this case
284+
# and doesn't show the filenames
285+
expected_shape = Image.open(baseline_image).size
286+
actual_shape = Image.open(test_image).size
287+
if expected_shape != actual_shape:
288+
error = SHAPE_MISMATCH_ERROR.format(expected_path=baseline_image,
289+
expected_shape=expected_shape,
290+
actual_path=test_image,
291+
actual_shape=actual_shape)
292+
pytest.fail(error, pytrace=False)
293+
275294
msg = compare_images(baseline_image, test_image, tol=tolerance)
276295

277296
if msg is None:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
long_description=long_description,
1919
packages=['pytest_mpl'],
2020
package_data={'pytest_mpl': ['classic.mplstyle']},
21-
install_requires=['pytest', 'matplotlib', 'nose'],
21+
install_requires=['pytest', 'matplotlib', 'pillow', 'nose'],
2222
license='BSD',
2323
author='Thomas Robitaille',
2424
author_email='thomas.robitaille@gmail.com',

0 commit comments

Comments
 (0)