Skip to content

Commit 42484d1

Browse files
committed
Initial followup baseline checking
1 parent 7524344 commit 42484d1

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

pytest_mpl/plugin.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,11 @@ def compare_image_to_baseline(self, item, fig, result_dir):
361361
fig.savefig(str(test_image), **savefig_kwargs)
362362

363363
if not os.path.exists(baseline_image_ref):
364-
pytest.fail("Image file not found for comparison test in: "
365-
"\n\t{baseline_dir}"
366-
"\n(This is expected for new tests.)\nGenerated Image: "
367-
"\n\t{test}".format(baseline_dir=self.get_baseline_directory(item),
368-
test=test_image),
369-
pytrace=False)
364+
return ("Image file not found for comparison test in: \n\t"
365+
f"{self.get_baseline_directory(item)}\n"
366+
"(This is expected for new tests.)\n"
367+
"Generated Image: \n\t"
368+
f"{test_image}")
370369

371370
# distutils may put the baseline images in non-accessible places,
372371
# copy to our tmpdir to be sure to keep them in case of failure
@@ -379,11 +378,10 @@ def compare_image_to_baseline(self, item, fig, result_dir):
379378
expected_shape = imread(str(baseline_image)).shape[:2]
380379
actual_shape = imread(str(test_image)).shape[:2]
381380
if expected_shape != actual_shape:
382-
error = SHAPE_MISMATCH_ERROR.format(expected_path=baseline_image,
383-
expected_shape=expected_shape,
384-
actual_path=test_image,
385-
actual_shape=actual_shape)
386-
pytest.fail(error, pytrace=False)
381+
return SHAPE_MISMATCH_ERROR.format(expected_path=baseline_image,
382+
expected_shape=expected_shape,
383+
actual_path=test_image,
384+
actual_shape=actual_shape)
387385

388386
return compare_images(str(baseline_image), str(test_image), tol=tolerance)
389387

@@ -408,10 +406,28 @@ def compare_image_to_hash_library(self, item, fig, result_dir):
408406

409407
test_hash = self.generate_image_hash(item, fig)
410408

411-
if test_hash != hash_library[hash_name]:
412-
return (f"hash {test_hash} doesn't match hash "
413-
f"{hash_library[hash_name]} in library "
414-
f"{hash_library_filename} for test {hash_name}.")
409+
if test_hash == hash_library[hash_name]:
410+
return
411+
412+
try:
413+
baseline_image = self.obtain_baseline_image(item, result_dir)
414+
baseline_image = None if not baseline_image.exists() else baseline_image
415+
except Exception:
416+
baseline_image = None
417+
418+
hash_error = (f"hash {test_hash} doesn't match hash "
419+
f"{hash_library[hash_name]} in library "
420+
f"{hash_library_filename} for test {hash_name}.")
421+
422+
if baseline_image is None:
423+
return hash_error
424+
425+
comparison_error = self.compare_image_to_baseline(item, fig, result_dir)
426+
427+
if not comparison_error:
428+
return hash_error + "\nHowever, the comparison to the baseline image succeeded."
429+
430+
return f"{hash_error}\n{comparison_error}"
415431

416432
def pytest_runtest_setup(self, item): # noqa
417433

0 commit comments

Comments
 (0)