forked from matplotlib/pytest-mpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_hash_library.py
More file actions
138 lines (122 loc) · 4.29 KB
/
Copy pathtest_hash_library.py
File metadata and controls
138 lines (122 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import json
import pytest
from helpers import pytester_path
def test_skip_hash(pytester):
"""Test that skip_hash=True skips hash comparison and uses baseline instead."""
path = pytester_path(pytester)
hash_library = path / "hash_library.json"
baseline_dir = path / "baseline"
# Generate baseline image (no hash library needed for generation)
pytester.makepyfile(
"""
import matplotlib.pyplot as plt
import pytest
@pytest.mark.mpl_image_compare()
def test_mpl():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 3, 2])
return fig
"""
)
pytester.runpytest(f"--mpl-generate-path={baseline_dir}")
# Create hash library with bad hash
with open(hash_library, "w") as fp:
json.dump({"test_skip_hash.test_mpl": "bad-hash-value"}, fp)
# Without skip_hash: should fail (hash mismatch)
result = pytester.runpytest("--mpl",
f"--mpl-hash-library={hash_library}",
f"--mpl-baseline-path={baseline_dir}")
result.assert_outcomes(failed=1)
# With skip_hash=True: should pass (uses baseline comparison, skips hash)
pytester.makepyfile(
"""
import matplotlib.pyplot as plt
import pytest
@pytest.mark.mpl_image_compare(skip_hash=True)
def test_mpl():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 3, 2])
return fig
"""
)
result = pytester.runpytest("--mpl",
f"--mpl-hash-library={hash_library}",
f"--mpl-baseline-path={baseline_dir}")
result.assert_outcomes(passed=1)
def test_skip_hash_not_generated(pytester):
"""Test that skip_hash=True tests are not included in generated hash library."""
path = pytester_path(pytester)
hash_library = path / "hash_library.json"
pytester.makepyfile(
"""
import matplotlib.pyplot as plt
import pytest
@pytest.mark.mpl_image_compare()
def test_normal():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3])
return fig
@pytest.mark.mpl_image_compare(skip_hash=True)
def test_skip():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([3, 2, 1])
return fig
"""
)
pytester.runpytest(f"--mpl-generate-hash-library={hash_library}")
# Check generated hash library
with open(hash_library) as fp:
hashes = json.load(fp)
# test_normal should be in the hash library
assert "test_skip_hash_not_generated.test_normal" in hashes
# test_skip should NOT be in the hash library
assert "test_skip_hash_not_generated.test_skip" not in hashes
@pytest.mark.parametrize(
"ini, cli, kwarg, success_expected",
[
("bad", None, None, False),
("good", None, None, True),
("bad", "good", None, True),
("bad", "bad", "good", False), # Note: CLI overrides kwarg
("bad", "good", "bad", True),
],
)
def test_config(pytester, ini, cli, kwarg, success_expected):
path = pytester_path(pytester)
hash_libraries = {
"good": path / "good_hash_library.json",
"bad": path / "bad_hash_library.json",
}
ini = f"mpl-hash-library = {hash_libraries[ini]}" if ini else ""
pytester.makeini(
f"""
[pytest]
mpl-deterministic: true
{ini}
"""
)
kwarg = f"hash_library=r'{hash_libraries[kwarg]}'" if kwarg else ""
pytester.makepyfile(
f"""
import matplotlib.pyplot as plt
import pytest
@pytest.mark.mpl_image_compare({kwarg})
def test_mpl():
fig, ax = plt.subplots()
ax.plot([1, 3, 2])
return fig
"""
)
pytester.runpytest(f"--mpl-generate-hash-library={hash_libraries['good']}")
with open(hash_libraries["bad"], "w") as fp:
json.dump({"test_config.test_mpl": "bad-value"}, fp)
cli = f"--mpl-hash-library={hash_libraries[cli]}" if cli else ""
result = pytester.runpytest("--mpl", cli)
if success_expected:
result.assert_outcomes(passed=1)
else:
result.assert_outcomes(failed=1)