File: cmp_snapshot.md

package info (click to toggle)
python-inline-snapshot 0.23.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,116 kB
  • sloc: python: 6,888; makefile: 34; sh: 28
file content (143 lines) | stat: -rw-r--r-- 3,505 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
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
139
140
141
142
143
## General

A snapshot can be compared against any value with `<=` or `>=`.
This can be used to create a upper/lower bound for some result.
The snapshot value can be trimmed to the lowest/largest valid value.

Example:

=== "original code"
    <!-- inline-snapshot: first_block outcome-passed=1 outcome-errors=1 -->
    ``` python
    from inline_snapshot import snapshot


    def gcd(x, y):
        iterations = 0
        if x > y:
            small = y
        else:
            small = x
        for i in range(1, small + 1):
            iterations += 1
            if (x % i == 0) and (y % i == 0):
                gcd = i

        return gcd, iterations


    def test_gcd():
        result, iterations = gcd(12, 18)

        assert result == snapshot()
        assert iterations <= snapshot()
    ```

=== "--inline-snapshot=create"
    <!-- inline-snapshot: create outcome-passed=1 outcome-errors=1 -->
    ``` python hl_lines="21 22"
    from inline_snapshot import snapshot


    def gcd(x, y):
        iterations = 0
        if x > y:
            small = y
        else:
            small = x
        for i in range(1, small + 1):
            iterations += 1
            if (x % i == 0) and (y % i == 0):
                gcd = i

        return gcd, iterations


    def test_gcd():
        result, iterations = gcd(12, 18)

        assert result == snapshot(6)
        assert iterations <= snapshot(12)
    ```

=== "optimized code "
    <!-- inline-snapshot: outcome-passed=1 -->
    ``` python hl_lines="5 7 9 10"
    from inline_snapshot import snapshot


    def gcd(x, y):
        # use Euclidean Algorithm
        iterations = 0
        while y:
            iterations += 1
            x, y = y, x % y
        return abs(x), iterations


    def test_gcd():
        result, iterations = gcd(12, 18)

        assert result == snapshot(6)
        assert iterations <= snapshot(12)
    ```

=== "--inline-snapshot=trim"
    <!-- inline-snapshot: trim outcome-passed=1 -->
    ``` python hl_lines="17"
    from inline_snapshot import snapshot


    def gcd(x, y):
        # use Euclidean Algorithm
        iterations = 0
        while y:
            iterations += 1
            x, y = y, x % y
        return abs(x), iterations


    def test_gcd():
        result, iterations = gcd(12, 18)

        assert result == snapshot(6)
        assert iterations <= snapshot(3)
    ```

!!! warning
    This should not be used to check for any flaky values like the runtime of some code, because it will randomly break your tests.

The same snapshot value can also be used in multiple assertions.

=== "original code"
    <!-- inline-snapshot: first_block outcome-passed=1 outcome-errors=1 -->
    ``` python
    from inline_snapshot import snapshot


    def test_something():
        value = snapshot()

        assert 5 <= value
        assert 6 <= value
    ```
=== "--inline-snapshot=create"
    <!-- inline-snapshot: create outcome-passed=1 outcome-errors=1 -->
    ``` python hl_lines="5"
    from inline_snapshot import snapshot


    def test_something():
        value = snapshot(6)

        assert 5 <= value
        assert 6 <= value
    ```

## pytest options

It interacts with the following `--inline-snapshot` flags:

- `create` create a new value if the snapshot value is undefined.
- `fix` record the new value and store it in the source code if it is contradicts the comparison.
- `trim` record the new value and store it in the source code if it is more strict than the old one.