@@ -51,6 +51,8 @@ def assert_pytest_fails_with(args, output_substring):
5151 output = exc .output .decode ()
5252 assert output_substring in output , output
5353 return output
54+ else :
55+ raise RuntimeError (f'pytest did not fail with args { args } ' )
5456
5557
5658@pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_local ,
@@ -187,10 +189,10 @@ def test_generate(tmpdir):
187189 assert code == 0
188190 assert os .path .exists (os .path .join (gen_dir , 'test_gen.png' ))
189191
190- # If we do generate hash, the test should succeed and a new file will appear
192+ # If we do generate hash, the test will fail as no image is present
191193 hash_file = os .path .join (gen_dir , 'test_hashes.json' )
192194 code = call_pytest ([f'--mpl-generate-hash-library={ hash_file } ' , test_file ])
193- assert code == 0
195+ assert code == 1
194196 assert os .path .exists (hash_file )
195197
196198 with open (hash_file ) as fp :
@@ -348,6 +350,38 @@ def test_hash_fail_hybrid(tmpdir):
348350 assert code == 0
349351
350352
353+ TEST_FAILING_NEW_HASH = r"""
354+ import pytest
355+ import matplotlib.pyplot as plt
356+ @pytest.mark.mpl_image_compare
357+ def test_hash_fails():
358+ fig = plt.figure()
359+ ax = fig.add_subplot(1,1,1)
360+ ax.plot([1,2,2])
361+ return fig
362+ """
363+
364+
365+ @pytest .mark .skipif (ftv != '261' , reason = "Incorrect freetype version for hash check" )
366+ def test_hash_fail_new_hashes (tmpdir ):
367+ # Check that the hash comparison fails even if a new hash file is requested
368+ test_file = tmpdir .join ('test.py' ).strpath
369+ with open (test_file , 'w' , encoding = 'ascii' ) as f :
370+ f .write (TEST_FAILING_NEW_HASH )
371+
372+ # Assert that image comparison runs and fails
373+ assert_pytest_fails_with (['--mpl' , test_file ,
374+ f'--mpl-hash-library={ fail_hash_library } ' ],
375+ "doesn't match hash FAIL in library" )
376+
377+ hash_file = tmpdir .join ('new_hashes.json' ).strpath
378+ # Assert that image comparison runs and fails
379+ assert_pytest_fails_with (['--mpl' , test_file ,
380+ f'--mpl-hash-library={ fail_hash_library } ' ,
381+ f'--mpl-generate-hash-library={ hash_file } ' ],
382+ "doesn't match hash FAIL" )
383+
384+
351385TEST_MISSING_HASH = """
352386import pytest
353387import matplotlib.pyplot as plt
0 commit comments