File: test_custom_comparator.py

package info (click to toggle)
python-syrupy 4.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,368 kB
  • sloc: python: 5,978; makefile: 3
file content (88 lines) | stat: -rw-r--r-- 2,795 bytes parent folder | download
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
import pytest


@pytest.fixture
def testcases_initial(testdir):
    testdir.makeconftest(
        """
        import pytest
        import math

        from syrupy.extensions.amber import AmberSnapshotExtension

        class CustomSnapshotExtension(AmberSnapshotExtension):
            def matches(self, *, serialized_data, snapshot_data):
                try:
                    a = float(serialized_data)
                    b = float(snapshot_data)
                    return math.isclose(a, b, rel_tol=1e-5)
                except:
                    return False

        @pytest.fixture
        def snapshot_custom(snapshot):
            return snapshot.use_extension(CustomSnapshotExtension)
        """
    )
    return {
        "passed": (
            """
            def test_passed_custom(snapshot_custom):
                assert snapshot_custom == 3.0
            """
        ),
        "failed": (
            """
            def test_passed_custom(snapshot_custom):
                # this comment is required or the test breaks
                assert snapshot_custom == 4.0
            """
        ),
    }


@pytest.fixture
def generate_snapshots(testdir, testcases_initial):
    testdir.makepyfile(test_file=testcases_initial["passed"])
    result = testdir.runpytest("-v", "--snapshot-update")
    return result, testdir, testcases_initial


@pytest.mark.xfail(strict=False)
def test_generated_snapshots(generate_snapshots):
    result = generate_snapshots[0]
    result.stdout.re_match_lines((r"1 snapshot generated\.",))
    assert "snapshots unused" not in result.stdout.str()
    assert result.ret == 0


@pytest.mark.xfail(strict=False)
def test_approximate_match(generate_snapshots, plugin_args_fails_xdist):
    testdir = generate_snapshots[1]
    testdir.makepyfile(
        test_file="""
            def test_passed_custom(snapshot_custom):
                assert snapshot_custom == 3.2
            """
    )
    result = testdir.runpytest("-v", *plugin_args_fails_xdist)
    result.stdout.re_match_lines((r"test_file.py::test_passed_custom PASSED",))
    assert result.ret == 0


@pytest.mark.xfail(strict=False)
def test_failed_snapshots(generate_snapshots):
    testdir = generate_snapshots[1]
    testdir.makepyfile(test_file=generate_snapshots[2]["failed"])
    result = testdir.runpytest("-v")
    result.stdout.re_match_lines((r"1 snapshot failed\.",))
    assert result.ret == 1


@pytest.mark.xfail(strict=False)
def test_updated_snapshots(generate_snapshots, plugin_args_fails_xdist):
    _, testdir, initial = generate_snapshots
    testdir.makepyfile(test_file=initial["failed"])
    result = testdir.runpytest("-v", "--snapshot-update", *plugin_args_fails_xdist)
    result.stdout.re_match_lines((r"1 snapshot updated\.",))
    assert result.ret == 0