Skip to content

Commit 65181c0

Browse files
committed
Don't output tests with skip_hash=True to hash library
1 parent e016f86 commit 65181c0

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

pytest_mpl/plugin.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,10 +912,14 @@ def pytest_runtest_call(self, item): # noqa
912912
result_image.relative_to(self.results_dir).as_posix()
913913

914914
if self.generate_hash_library is not None:
915-
summary['hash_status'] = 'generated'
916-
image_hash = self.generate_image_hash(item, fig)
917-
self._generated_hash_library[test_name] = image_hash
918-
summary['baseline_hash'] = image_hash
915+
skip_hash = compare.kwargs.get('skip_hash', False)
916+
if skip_hash:
917+
summary['hash_status'] = 'skipped'
918+
else:
919+
summary['hash_status'] = 'generated'
920+
image_hash = self.generate_image_hash(item, fig)
921+
self._generated_hash_library[test_name] = image_hash
922+
summary['baseline_hash'] = image_hash
919923

920924
# Only test figures if not generating images
921925
if self.generate_dir is None:

tests/test_hash_library.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,43 @@ def test_mpl():
5454
result.assert_outcomes(passed=1)
5555

5656

57+
def test_skip_hash_not_generated(pytester):
58+
"""Test that skip_hash=True tests are not included in generated hash library."""
59+
path = pytester_path(pytester)
60+
hash_library = path / "hash_library.json"
61+
62+
pytester.makepyfile(
63+
"""
64+
import matplotlib.pyplot as plt
65+
import pytest
66+
67+
@pytest.mark.mpl_image_compare()
68+
def test_normal():
69+
fig = plt.figure()
70+
ax = fig.add_subplot(1, 1, 1)
71+
ax.plot([1, 2, 3])
72+
return fig
73+
74+
@pytest.mark.mpl_image_compare(skip_hash=True)
75+
def test_skip():
76+
fig = plt.figure()
77+
ax = fig.add_subplot(1, 1, 1)
78+
ax.plot([3, 2, 1])
79+
return fig
80+
"""
81+
)
82+
pytester.runpytest(f"--mpl-generate-hash-library={hash_library}")
83+
84+
# Check generated hash library
85+
with open(hash_library) as fp:
86+
hashes = json.load(fp)
87+
88+
# test_normal should be in the hash library
89+
assert "test_skip_hash_not_generated.test_normal" in hashes
90+
# test_skip should NOT be in the hash library
91+
assert "test_skip_hash_not_generated.test_skip" not in hashes
92+
93+
5794
@pytest.mark.parametrize(
5895
"ini, cli, kwarg, success_expected",
5996
[

0 commit comments

Comments
 (0)