Skip to main content

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.

PyPI version Python License: MIT


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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snaptestv2-1.0.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snaptestv2-1.0.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

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

Hashes for snaptestv2-1.0.0.tar.gz
Algorithm Hash digest
SHA256 dd756b1323007e7f12f6d5fc81688798577ba153feb49fe9445116e8dda02b29
MD5 7eb03bf08da46569620b8dcde35b2506
BLAKE2b-256 8b99d0527ab69fa2a705df17ec3a088b4ea179f6e1264ce52d00992c4d6e5d78

See more details on using hashes here.

Provenance

The following attestation bundles were made for snaptestv2-1.0.0.tar.gz:

Publisher: publish.yml on prabhay759/snaptest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for snaptestv2-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb5f86a08f21ebc22cedf3c9ad577e4bdd042723c5fa50ff2688bf66e1ff27e7
MD5 4c41a1c8a5451c2cbd2823d46cc6b97b
BLAKE2b-256 3e3fd4e0574bd5e97e1a7965bac65a290dc9df8ef1bb41e2866d4a3dbe4bddd4

See more details on using hashes here.

Provenance

The following attestation bundles were made for snaptestv2-1.0.0-py3-none-any.whl:

Publisher: publish.yml on prabhay759/snaptest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page