|
4 | 4 | from helpers import pytester_path |
5 | 5 |
|
6 | 6 |
|
| 7 | +def test_skip_hash(pytester): |
| 8 | + """Test that skip_hash=True skips hash comparison and uses baseline instead.""" |
| 9 | + path = pytester_path(pytester) |
| 10 | + hash_library = path / "hash_library.json" |
| 11 | + baseline_dir = path / "baseline" |
| 12 | + |
| 13 | + # Generate baseline image (no hash library needed for generation) |
| 14 | + pytester.makepyfile( |
| 15 | + """ |
| 16 | + import matplotlib.pyplot as plt |
| 17 | + import pytest |
| 18 | + @pytest.mark.mpl_image_compare() |
| 19 | + def test_mpl(): |
| 20 | + fig = plt.figure() |
| 21 | + ax = fig.add_subplot(1, 1, 1) |
| 22 | + ax.plot([1, 3, 2]) |
| 23 | + return fig |
| 24 | + """ |
| 25 | + ) |
| 26 | + pytester.runpytest(f"--mpl-generate-path={baseline_dir}") |
| 27 | + |
| 28 | + # Create hash library with bad hash |
| 29 | + with open(hash_library, "w") as fp: |
| 30 | + json.dump({"test_skip_hash.test_mpl": "bad-hash-value"}, fp) |
| 31 | + |
| 32 | + # Without skip_hash: should fail (hash mismatch) |
| 33 | + result = pytester.runpytest("--mpl", |
| 34 | + f"--mpl-hash-library={hash_library}", |
| 35 | + f"--mpl-baseline-path={baseline_dir}") |
| 36 | + result.assert_outcomes(failed=1) |
| 37 | + |
| 38 | + # With skip_hash=True: should pass (uses baseline comparison, skips hash) |
| 39 | + pytester.makepyfile( |
| 40 | + """ |
| 41 | + import matplotlib.pyplot as plt |
| 42 | + import pytest |
| 43 | + @pytest.mark.mpl_image_compare(skip_hash=True) |
| 44 | + def test_mpl(): |
| 45 | + fig = plt.figure() |
| 46 | + ax = fig.add_subplot(1, 1, 1) |
| 47 | + ax.plot([1, 3, 2]) |
| 48 | + return fig |
| 49 | + """ |
| 50 | + ) |
| 51 | + result = pytester.runpytest("--mpl", |
| 52 | + f"--mpl-hash-library={hash_library}", |
| 53 | + f"--mpl-baseline-path={baseline_dir}") |
| 54 | + result.assert_outcomes(passed=1) |
| 55 | + |
| 56 | + |
| 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 | + |
7 | 94 | @pytest.mark.parametrize( |
8 | 95 | "ini, cli, kwarg, success_expected", |
9 | 96 | [ |
|
0 commit comments