Skip to content
Become a
Sponsor or follow @15r10nk on
or
for updates

# 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.

test_something.py

    from inline_snapshot import snapshot

    def test_something():
        assert 1 == snapshot()
        assert 2 <= snapshot(5)

    > pytest test_something.py --inline-snapshot=create,report
    ============================= test session starts ==============================
    platform linux -- Python 3.13.2, pytest-8.3.5, pluggy-1.5.0
    rootdir: /tmp/tmp.ep3C2PFVWj
    plugins: pytest_freezer-0.4.9, subtests-0.13.1, mock-3.14.0, time-machine-2.16.0, inline-snapshot-0.20.3, hypothesis-6.127.4
    collected 1 item

    test_something.py .                                                      [100%]
    =============================== inline snapshot ================================
    ─────────────────────────────── Create snapshots ───────────────────────────────
    +----------------------------- test_something.py ------------------------------+
    | @@ -2,5 +2,5 @@                                                              |
    |                                                                              |
    |                                                                              |
    |                                                                              |
    |  def test_something():                                                       |
    | -    assert 1 == snapshot()                                                  |
    | +    assert 1 == snapshot(1)                                                 |
    |      assert 2 <= snapshot(5)                                                 |
    +------------------------------------------------------------------------------+
    These changes will be applied, because you used --inline-snapshot=create

    ──────────────────────────────── Trim snapshots ────────────────────────────────
    +----------------------------- test_something.py ------------------------------+
    | @@ -3,4 +3,4 @@                                                              |
    |                                                                              |
    |                                                                              |
    |  def test_something():                                                       |
    |      assert 1 == snapshot(1)                                                 |
    | -    assert 2 <= snapshot(5)                                                 |
    | +    assert 2 <= snapshot(2)                                                 |
    +------------------------------------------------------------------------------+
    These changes are not applied.
    Use --inline-snapshot=trim to apply them, or use the interactive mode with
    --inline-snapshot=review

    ============================== 1 passed in 0.21s ===============================

## --inline-snapshot=short-report

give a short report over which changes can be made to the snapshots

    > pytest test_something.py --inline-snapshot=short-report
    ============================= test session starts ==============================
    platform linux -- Python 3.13.2, pytest-8.3.5, pluggy-1.5.0
    rootdir: /tmp/tmp.SXNyazvt9H
    plugins: pytest_freezer-0.4.9, subtests-0.13.1, mock-3.14.0, time-machine-2.16.0, inline-snapshot-0.20.3, hypothesis-6.127.4
    collected 1 item

    test_something.py .E                                                     [100%]

    ==================================== ERRORS ====================================
    _____________________ ERROR at teardown of test_something ______________________
    your snapshot is missing one value.
    =============================== inline snapshot ================================
    Info: one snapshot can be trimmed (--inline-snapshot=trim)
    Error: one snapshot is missing a value (--inline-snapshot=create)

    You can also use --inline-snapshot=review to approve the changes interactively
    =========================== short test summary info ============================
    ERROR test_something.py::test_something - Failed: your snapshot is missing one value.
    ========================== 1 passed, 1 error in 0.19s ==========================

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

    > pytest test_something.py --inline-snapshot=report
    ============================= test session starts ==============================
    platform linux -- Python 3.13.2, pytest-8.3.5, pluggy-1.5.0
    rootdir: /tmp/tmp.DJysywB63x
    plugins: pytest_freezer-0.4.9, subtests-0.13.1, mock-3.14.0, time-machine-2.16.0, inline-snapshot-0.20.3, hypothesis-6.127.4
    collected 1 item

    test_something.py .E                                                     [100%]

    ==================================== ERRORS ====================================
    _____________________ ERROR at teardown of test_something ______________________
    your snapshot is missing one value.
    =============================== inline snapshot ================================
    ─────────────────────────────── Create snapshots ───────────────────────────────
    +----------------------------- test_something.py ------------------------------+
    | @@ -2,5 +2,5 @@                                                              |
    |                                                                              |
    |                                                                              |
    |                                                                              |
    |  def test_something():                                                       |
    | -    assert 1 == snapshot()                                                  |
    | +    assert 1 == snapshot(1)                                                 |
    |      assert 2 <= snapshot(5)                                                 |
    +------------------------------------------------------------------------------+
    These changes are not applied.
    Use --inline-snapshot=create to apply them, or use the interactive mode with
    --inline-snapshot=review

    ──────────────────────────────── Trim snapshots ────────────────────────────────
    +----------------------------- test_something.py ------------------------------+
    | @@ -3,4 +3,4 @@                                                              |
    |                                                                              |
    |                                                                              |
    |  def test_something():                                                       |
    |      assert 1 == snapshot()                                                  |
    | -    assert 2 <= snapshot(5)                                                 |
    | +    assert 2 <= snapshot(2)                                                 |
    +------------------------------------------------------------------------------+
    These changes are not applied.
    Use --inline-snapshot=trim to apply them, or use the interactive mode with
    --inline-snapshot=review

    =========================== short test summary info ============================
    ERROR test_something.py::test_something - Failed: your snapshot is missing one value.
    ========================== 1 passed, 1 error in 0.19s ==========================

## --inline-snapshot=review

Shows a diff report for each category and ask if you want to apply the changes

    > pytest test_something.py --inline-snapshot=review
    ============================= test session starts ==============================
    platform linux -- Python 3.13.2, pytest-8.3.5, pluggy-1.5.0
    rootdir: /tmp/tmp.pGhoKRnQZC
    plugins: pytest_freezer-0.4.9, subtests-0.13.1, mock-3.14.0, time-machine-2.16.0, inline-snapshot-0.20.3, hypothesis-6.127.4
    collected 1 item

    test_something.py .                                                      [100%]
    =============================== inline snapshot ================================
    ─────────────────────────────── Create snapshots ───────────────────────────────
    +----------------------------- test_something.py ------------------------------+
    | @@ -2,5 +2,5 @@                                                              |
    |                                                                              |
    |                                                                              |
    |                                                                              |
    |  def test_something():                                                       |
    | -    assert 1 == snapshot()                                                  |
    | +    assert 1 == snapshot(1)                                                 |
    |      assert 2 <= snapshot(5)                                                 |
    +------------------------------------------------------------------------------+
    do you want to create these snapshots? [y/n] (n):
    ──────────────────────────────── Trim snapshots ────────────────────────────────
    +----------------------------- test_something.py ------------------------------+
    | @@ -3,4 +3,4 @@                                                              |
    |                                                                              |
    |                                                                              |
    |  def test_something():                                                       |
    |      assert 1 == snapshot(1)                                                 |
    | -    assert 2 <= snapshot(5)                                                 |
    | +    assert 2 <= snapshot(2)                                                 |
    +------------------------------------------------------------------------------+
    do you want to trim these snapshots? [y/n] (n):

    ============================== 1 passed in 0.20s ===============================

## --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`