SyntaxStudy
Sign Up
Python Intermediate 4 min read

Snapshot Testing

Snapshot Tests

Snapshot tests capture output on first run and compare it on subsequent runs — great for APIs, HTML, and serialised data.

Example
# Using syrupy (pytest plugin)
pip install syrupy
from syrupy.assertion import SnapshotAssertion

def test_api_response(snapshot: SnapshotAssertion):
    resp = client.get("/api/users")
    assert resp.json() == snapshot

# Update snapshots when output intentionally changes:
# pytest --snapshot-update
Pro Tip

Always review snapshot diffs before committing updates — accidentally approving wrong output is easy.