Snapshot Tests
Snapshot tests capture output on first run and compare it on subsequent runs — great for APIs, HTML, and serialised data.
Snapshot tests capture output on first run and compare it on subsequent runs — great for APIs, HTML, and serialised data.
# 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
Always review snapshot diffs before committing updates — accidentally approving wrong output is easy.