Snapshot testing for Python — auto-create, diff on mismatch, pytest plugin, and CLI
Project description
snaptest
Snapshot testing for Python — automatically creates snapshots on first run, shows clear diffs on mismatch, pytest plugin included, and a CLI to manage snapshots. Zero dependencies.
Installation
pip install snaptest
Quick Start
# pytest style
def test_api_response(snap):
result = get_users()
snap(result) # creates snapshot on first run, asserts on next runs
# Update snapshots when output intentionally changes
pytest --snapshot-update
# or
SNAPTEST_UPDATE=1 pytest
Usage
pytest Fixture
def test_user_list(snap):
users = api.get_users()
snap(users)
def test_multiple_snapshots(snap):
snap(api.get_users(), key="users_list")
snap(api.get_profile(), key="user_profile")
Standalone (without pytest)
from snaptest import snapshot
def test_output():
result = transform_data(raw_input)
snapshot(result) # auto-named from function + line number
snapshot(result, key="data") # named snapshot
Updating Snapshots
# Via pytest flag
pytest --snapshot-update
# Via environment variable
SNAPTEST_UPDATE=1 pytest
# Programmatically
from snaptest import update_snapshot
update_snapshot(new_value, key="my_snapshot")
Snapshot Files
Snapshots are stored as human-readable text files next to your test files:
tests/
├── test_api.py
├── test_transform.py
└── __snapshots__/
├── test_api.snap
└── test_transform.snap
Commit __snapshots__/ to version control so diffs are visible in PRs.
Snapshot file format
# snaptest snapshot file
# Do not edit manually
[test_user_list_1]
{
"id": 1,
"name": "Alice"
}
[/test_user_list_1]
Diff Output on Mismatch
When a snapshot doesn't match, you get a clear diff:
SnapshotMismatch: 'test_user_list_1'
Snapshot file: tests/__snapshots__/test_api.snap
─── Diff (- expected, + actual) ───────────────────────
--- expected (stored)
+++ actual (current)
@@ -1,5 +1,5 @@
{
"id": 1,
- "name": "Alice",
+ "name": "Alice Smith",
"active": true
}
────────────────────────────────────────────────────────
To update: SNAPTEST_UPDATE=1 pytest
Supported Types
| Type | How it's stored |
|---|---|
dict |
JSON with sorted keys |
list, tuple |
JSON array |
set, frozenset |
Sorted JSON array |
str, int, float, bool, None |
JSON scalar |
dataclass |
JSON via dataclasses.asdict |
Any object with __dict__ |
JSON via vars() |
| Anything else | repr() |
CLI
# List all snapshot files and keys
snaptest list
# Show a stored snapshot value
snaptest show my_snapshot_key
# Instructions to update
snaptest update
# Delete all snapshot files (with confirmation)
snaptest clean
snaptest clean --yes # skip confirmation
API Reference
snapshot(value, key=None, update=False)
Assert value matches stored snapshot. Creates on first run.
update_snapshot(value, key=None)
Force-update a snapshot.
snap fixture (pytest)
def test_something(snap):
snap(value) # auto-named
snap(value, key="name") # named
Running Tests
pip install pytest
pytest tests/ -v
License
MIT © prabhay759
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file snaptestv2-1.0.0.tar.gz.
File metadata
- Download URL: snaptestv2-1.0.0.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd756b1323007e7f12f6d5fc81688798577ba153feb49fe9445116e8dda02b29
|
|
| MD5 |
7eb03bf08da46569620b8dcde35b2506
|
|
| BLAKE2b-256 |
8b99d0527ab69fa2a705df17ec3a088b4ea179f6e1264ce52d00992c4d6e5d78
|
Provenance
The following attestation bundles were made for snaptestv2-1.0.0.tar.gz:
Publisher:
publish.yml on prabhay759/snaptest
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
snaptestv2-1.0.0.tar.gz -
Subject digest:
dd756b1323007e7f12f6d5fc81688798577ba153feb49fe9445116e8dda02b29 - Sigstore transparency entry: 1224341968
- Sigstore integration time:
-
Permalink:
prabhay759/snaptest@d4e5567e89dca4bba95bced34836c34a87ad894c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/prabhay759
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d4e5567e89dca4bba95bced34836c34a87ad894c -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file snaptestv2-1.0.0-py3-none-any.whl.
File metadata
- Download URL: snaptestv2-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb5f86a08f21ebc22cedf3c9ad577e4bdd042723c5fa50ff2688bf66e1ff27e7
|
|
| MD5 |
4c41a1c8a5451c2cbd2823d46cc6b97b
|
|
| BLAKE2b-256 |
3e3fd4e0574bd5e97e1a7965bac65a290dc9df8ef1bb41e2866d4a3dbe4bddd4
|
Provenance
The following attestation bundles were made for snaptestv2-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on prabhay759/snaptest
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
snaptestv2-1.0.0-py3-none-any.whl -
Subject digest:
fb5f86a08f21ebc22cedf3c9ad577e4bdd042723c5fa50ff2688bf66e1ff27e7 - Sigstore transparency entry: 1224341969
- Sigstore integration time:
-
Permalink:
prabhay759/snaptest@d4e5567e89dca4bba95bced34836c34a87ad894c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/prabhay759
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d4e5567e89dca4bba95bced34836c34a87ad894c -
Trigger Event:
workflow_dispatch
-
Statement type: