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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
`inline_snapshot.testing` provides tools that can be used to test inline-snapshot workflows.
This might be useful if you want to build your own libraries based on inline-snapshot.
The following example shows how you can use the `Example` class to test what inline-snapshot would do with the given source code. The snapshots in the argument are asserted inside the `run_*` methods. Some arguments are optional, and some are required. Please see the reference below for details.
<!-- inline-snapshot: first_block outcome-passed=1 outcome-errors=1 -->
``` python
from inline_snapshot.testing import Example
from inline_snapshot import snapshot
def test_something():
Example(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot()
"""
}
).run_pytest( # run with the create flag and check the changed files
["--inline-snapshot=create"],
changed_files=snapshot(),
returncode=snapshot(),
)
```
Inline-snapshot will then populate the empty snapshots.
<!-- inline-snapshot: create outcome-passed=1 outcome-errors=1 -->
``` python hl_lines="17 18 19 20 21 22 23 24 25 26"
from inline_snapshot.testing import Example
from inline_snapshot import snapshot
def test_something():
Example(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot()
"""
}
).run_pytest( # run with the create flag and check the changed files
["--inline-snapshot=create"],
changed_files=snapshot(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot(2)
"""
}
),
returncode=snapshot(1),
)
```
The `Example` object is immutable and not connected to any directory.
A temporary directory is only created when you call a `run_*` method.
The result of the `run_*` method is always a new `Example` object with the updated files.
This means that you can create an `Example` and call the `run_*` methods on it in any order you want, with the arguments that are useful for your test case.
This allows for more complex tests where you create one example and perform multiple test runs on it. Every test run will work on the changed files from the previous one.
<!-- inline-snapshot: create fix first_block outcome-passed=1 -->
``` python
from inline_snapshot.testing import Example
from inline_snapshot import snapshot
def test_something():
Example(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot()
assert 1+5 == snapshot(2)
"""
}
).run_pytest( # run with the create flag and check the changed files
["--inline-snapshot=create"],
changed_files=snapshot(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot(2)
assert 1+5 == snapshot(2)
"""
}
),
returncode=snapshot(1),
).run_pytest( # run with the create flag and check the changed files
["--inline-snapshot=fix"],
changed_files=snapshot(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot(2)
assert 1+5 == snapshot(6)
"""
}
),
returncode=snapshot(1),
)
```
You can also use the same example multiple times and call different methods on it.
<!-- inline-snapshot: create fix first_block outcome-failed=1 -->
``` python
from inline_snapshot.testing import Example
from inline_snapshot import snapshot
def test_something():
e = Example(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot()
assert 1+5 == snapshot(2)
"""
}
)
e.run_inline( # run without flags
reported_categories=snapshot(["create", "fix"]),
)
e.run_pytest(
["--inline-snapshot=short-report"], # check the pytest report
changed_files=snapshot({}),
report=snapshot(
"""\
Error: one snapshot is missing a value (--inline-snapshot=create)
You can also use --inline-snapshot=review to approve the changes interactively\
"""
),
returncode=snapshot(1),
)
e.run_pytest( # run with the create flag and check the changed files
["--inline-snapshot=create"],
changed_files=snapshot(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot(2)
"""
}
),
returncode=snapshot(1),
)
```
## API
::: inline_snapshot.testing.Example
options:
heading_level: 3
show_root_heading: true
show_root_full_path: false
show_source: false
annotations_path: brief
|