pytest integration
inline-snapshot provides one pytest option with different flags (create, fix, trim, update, short-report, report, disable).
Snapshot comparisons return always True
if you use one of the flags create, fix or review.
This is necessary because the whole test needs to be run to fix all snapshots like in this case:
from inline_snapshot import snapshot
def test_something():
assert 1 == snapshot(5)
assert 2 <= snapshot(5)
Note
Every flag with the exception of disable and short-report disables the pytest assert-rewriting.
--inline-snapshot=create,fix,trim,update¶
Approve the changes of the given category. These flags can be combined with report and review.
from inline_snapshot import snapshot
def test_something():
assert 1 == snapshot()
assert 2 <= snapshot(5)
[1;34m> pytest test_something.py --inline-snapshot=create,report
[0m[1m============================= test session starts ==============================[0m
platform linux -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0
rootdir: /tmp/tmp.t8HehclLGa
plugins: time-machine-2.16.0, subtests-0.13.1, mock-3.14.0, pytest_freezer-0.4.9, inline-snapshot-0.17.1, hypothesis-6.122.1, typeguard-4.4.1
collected 1 item
test_something.py [32m.[0m[32m [100%][0m
=============================== inline snapshot ================================
[92m─────────────────────────────── [0m[1;33mCreate snapshots[0m[92m ───────────────────────────────[0m
╭───────────────────────────── test_something.py ──────────────────────────────╮
│ [1;35m@@ -2,5 +2,5 @@[0m │
│ │
│ [37m [0m │
│ [37m [0m │
│ [37m [0mdef test_something(): │
│ [91m- assert 1 == snapshot()[0m │
│ [32m+ assert 1 == snapshot(1)[0m │
│ [37m [0m assert 2 <= snapshot(5) │
╰──────────────────────────────────────────────────────────────────────────────╯
These changes will be applied, because you used [1m--inline-snapshot=create[0m
[92m──────────────────────────────── [0m[1;33mTrim snapshots[0m[92m ────────────────────────────────[0m
╭───────────────────────────── test_something.py ──────────────────────────────╮
│ [1;35m@@ -3,4 +3,4 @@[0m │
│ │
│ [37m [0m │
│ [37m [0mdef test_something(): │
│ [37m [0m assert 1 == snapshot(1) │
│ [91m- assert 2 <= snapshot(5)[0m │
│ [32m+ assert 2 <= snapshot(2)[0m │
╰──────────────────────────────────────────────────────────────────────────────╯
These changes are not applied.
Use [1m--inline-snapshot=trim[0m to apply them, or use the interactive mode with
[1m--inline-snapshot=review[0m
[32m============================== [32m[1m1 passed[0m[32m in 0.30s[0m[32m ===============================[0m
--inline-snapshot=short-report¶
give a short report over which changes can be made to the snapshots
[1;34m> pytest test_something.py --inline-snapshot=short-report
[0m[1m============================= test session starts ==============================[0m
platform linux -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0
rootdir: /tmp/tmp.2WaErx2UKs
plugins: time-machine-2.16.0, subtests-0.13.1, mock-3.14.0, pytest_freezer-0.4.9, inline-snapshot-0.17.1, hypothesis-6.122.1, typeguard-4.4.1
collected 1 item
test_something.py [32m.[0m[31mE[0m[31m [100%][0m
==================================== ERRORS ====================================
[31m[1m_____________________ ERROR at teardown of test_something ______________________[0m
your snapshot is missing one value.
=============================== inline snapshot ================================
Info: one snapshot can be trimmed ([1m--inline-snapshot=trim[0m)
Error: one snapshot is missing a value ([1m--inline-snapshot=create[0m)
You can also use [1m--inline-snapshot=review[0m to approve the changes interactively
[36m[1m=========================== short test summary info ============================[0m
[31mERROR[0m test_something.py::[1mtest_something[0m - Failed: your snapshot is missing one value.
[31m========================== [32m1 passed[0m, [31m[1m1 error[0m[31m in 0.26s[0m[31m ==========================[0m
Info
short-report exists mainly to show that snapshots have changed with enabled pytest assert-rewriting. This option will be replaced with report when this restriction is lifted.
--inline-snapshot=report¶
Shows a diff report over which changes can be made to the snapshots
[1;34m> pytest test_something.py --inline-snapshot=report
[0m[1m============================= test session starts ==============================[0m
platform linux -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0
rootdir: /tmp/tmp.MvmHLRIiDi
plugins: time-machine-2.16.0, subtests-0.13.1, mock-3.14.0, pytest_freezer-0.4.9, inline-snapshot-0.17.1, hypothesis-6.122.1, typeguard-4.4.1
collected 1 item
test_something.py [32m.[0m[31mE[0m[31m [100%][0m
==================================== ERRORS ====================================
[31m[1m_____________________ ERROR at teardown of test_something ______________________[0m
your snapshot is missing one value.
=============================== inline snapshot ================================
[92m─────────────────────────────── [0m[1;33mCreate snapshots[0m[92m ───────────────────────────────[0m
╭───────────────────────────── test_something.py ──────────────────────────────╮
│ [1;35m@@ -2,5 +2,5 @@[0m │
│ │
│ [37m [0m │
│ [37m [0m │
│ [37m [0mdef test_something(): │
│ [91m- assert 1 == snapshot()[0m │
│ [32m+ assert 1 == snapshot(1)[0m │
│ [37m [0m assert 2 <= snapshot(5) │
╰──────────────────────────────────────────────────────────────────────────────╯
These changes are not applied.
Use [1m--inline-snapshot=create[0m to apply them, or use the interactive mode with
[1m--inline-snapshot=review[0m
[92m──────────────────────────────── [0m[1;33mTrim snapshots[0m[92m ────────────────────────────────[0m
╭───────────────────────────── test_something.py ──────────────────────────────╮
│ [1;35m@@ -3,4 +3,4 @@[0m │
│ │
│ [37m [0m │
│ [37m [0mdef test_something(): │
│ [37m [0m assert 1 == snapshot() │
│ [91m- assert 2 <= snapshot(5)[0m │
│ [32m+ assert 2 <= snapshot(2)[0m │
╰──────────────────────────────────────────────────────────────────────────────╯
These changes are not applied.
Use [1m--inline-snapshot=trim[0m to apply them, or use the interactive mode with
[1m--inline-snapshot=review[0m
[36m[1m=========================== short test summary info ============================[0m
[31mERROR[0m test_something.py::[1mtest_something[0m - Failed: your snapshot is missing one value.
[31m========================== [32m1 passed[0m, [31m[1m1 error[0m[31m in 0.16s[0m[31m ==========================[0m
--inline-snapshot=review¶
Shows a diff report for each category and ask if you want to apply the changes
[1;34m> pytest test_something.py --inline-snapshot=review
[0m[1m============================= test session starts ==============================[0m
platform linux -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0
rootdir: /tmp/tmp.BQgRaO2G5u
plugins: time-machine-2.16.0, subtests-0.13.1, mock-3.14.0, pytest_freezer-0.4.9, inline-snapshot-0.17.1, hypothesis-6.122.1, typeguard-4.4.1
collected 1 item
test_something.py [32m.[0m[32m [100%][0m
=============================== inline snapshot ================================
[92m─────────────────────────────── [0m[1;33mCreate snapshots[0m[92m ───────────────────────────────[0m
╭───────────────────────────── test_something.py ──────────────────────────────╮
│ [1;35m@@ -2,5 +2,5 @@[0m │
│ │
│ [37m [0m │
│ [37m [0m │
│ [37m [0mdef test_something(): │
│ [91m- assert 1 == snapshot()[0m │
│ [32m+ assert 1 == snapshot(1)[0m │
│ [37m [0m assert 2 <= snapshot(5) │
╰──────────────────────────────────────────────────────────────────────────────╯
[1mdo you want to [0m[1;34mcreate[0m[1m these snapshots?[0m [1;35m[y/n][0m [1;36m(n)[0m:
[92m──────────────────────────────── [0m[1;33mTrim snapshots[0m[92m ────────────────────────────────[0m
╭───────────────────────────── test_something.py ──────────────────────────────╮
│ [1;35m@@ -3,4 +3,4 @@[0m │
│ │
│ [37m [0m │
│ [37m [0mdef test_something(): │
│ [37m [0m assert 1 == snapshot(1) │
│ [91m- assert 2 <= snapshot(5)[0m │
│ [32m+ assert 2 <= snapshot(2)[0m │
╰──────────────────────────────────────────────────────────────────────────────╯
[1mdo you want to [0m[1;34mtrim[0m[1m these snapshots?[0m [1;35m[y/n][0m [1;36m(n)[0m:
[32m============================== [32m[1m1 passed[0m[32m in 0.30s[0m[32m ===============================[0m
--inline-snapshot=disable¶
Disables all the snapshot logic. snapshot(x)
will just return x
.
This can be used if you think exclude that snapshot logic causes a problem in your tests, or if you want to speedup your CI.
deprecation
This option was previously called --inline-snapshot-disable