Skip to content

Commit 09df663

Browse files
authored
Merge pull request #121 from dstansby/hashes
Always generate hashes when requested
2 parents 246c2d3 + 3ab283b commit 09df663

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

pytest_mpl/plugin.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,8 @@ def item_function_wrapper(*args, **kwargs):
557557
hash_name = self.generate_test_name(item)
558558
self._generated_hash_library[hash_name] = self.generate_image_hash(item, fig)
559559

560-
# Only test figures if we are not generating hashes or images
561-
if self.generate_dir is None and self.generate_hash_library is None:
560+
# Only test figures if not generating images
561+
if self.generate_dir is None:
562562
result_dir = self.make_test_results_dir(item)
563563

564564
# Compare to hash library

tests/test_pytest_mpl.py

+36-2
Original file line numberDiff line numberDiff line change
@@ -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+
351385
TEST_MISSING_HASH = """
352386
import pytest
353387
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)