Registry / testing / pytest-snapshot

pytest-snapshot

JSON →
library0.9.0pypypi✓ verified 25d ago

pytest-snapshot is a plugin for snapshot testing with pytest. It enables developers to compare current test outputs (e.g., data structures, HTML, JSON) against a pre-recorded 'snapshot', failing tests if a mismatch occurs. The current version is 0.9.0, and it maintains an active release schedule with regular minor and patch updates.

pip install pytest-snapshot
INSTALL
IMPORT
SIG · PYTEST-SNAPSHOT
P
pytest-snapshot
testingpythonv0.9.0
Install
2.6s avg
Import
Disk
30MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 31.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.6s · import 0.000s · 32MB
30MB installed
● package 30MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

snapshot
def test_example(snapshot): # ... use snapshot.assert_match(data, 'snapshot_name.json') ...
The 'snapshot' fixture is automatically provided by the pytest-snapshot plugin to any test function that declares it as an argument. No explicit 'import' statement from 'pytest_snapshot' is needed to use the fixture.

This quickstart demonstrates how to use the `snapshot` fixture within a pytest function to test a Python dictionary. The first execution with `--snapshot-update` creates the baseline snapshot; subsequent runs verify that the data structure remains consistent.

import pytest def test_data_structure(snapshot): # Simulate some data generated by your application data = { "id": 101, "name": "Sample Product", "config": { "enabled": True, "features": ["search", "filter"] } } # The snapshot file (e.g., my_product_snapshot.json) will be created # or updated relative to the test file's directory. snapshot.assert_match(data, "my_product_snapshot.json") # To run this test: # 1. Save the code as `test_product.py`. # 2. Run `pytest --snapshot-update` in your terminal to create or update the initial snapshot. # 3. Subsequent runs of `pytest` (without `--snapshot-update`) will compare against the saved snapshot.
Debug
Known issues
gotchaTo create or update snapshots, you MUST explicitly run pytest with the `--snapshot-update` CLI flag. Without this flag, tests will fail if snapshots are missing or do not match, but new snapshots will not be generated or updated.
fix
Always use `pytest --snapshot-update` when you intend to create or modify snapshot files.
affects: All versions
breakingPython 2 support was officially dropped in version 0.5.0. pytest-snapshot now requires Python 3.5 or newer to function.
fix
Ensure your testing environment uses Python 3.5 or a more recent version.
affects: >=0.5.0
breakingBeginning with version 0.5.0, all snapshot files are saved using UTF-8 encoding. If you have existing snapshots created with an older version that might have used a different encoding, they could lead to comparison errors after upgrading.
fix
It is recommended to regenerate all existing snapshots after upgrading to version 0.5.0 or later to ensure consistent UTF-8 encoding.
affects: >=0.5.0
gotchaAttempting to snapshot test certain unsupported values, such as strings containing the carriage return character ('\r'), will raise a `ValueError`. This behavior was introduced to clarify unsupported inputs.
fix
Modify the data to remove unsupported characters or types before passing it to `snapshot.assert_match`, or handle such cases explicitly in your test logic.
affects: >=0.6.3
gotchaIn versions prior to 0.8.1, `snapshot.assert_match` might not correctly raise exceptions when running pytest with both `--assert=plain` and without `--snapshot-update`. This could mask actual failures.
fix
Upgrade to version 0.8.1 or newer to ensure robust exception handling. If unable to upgrade, avoid using `--assert=plain` in conjunction with `pytest-snapshot` without `--snapshot-update`.
affects: <0.8.1
Upgrade
Version history
0.9.0latest on PyPI · released Apr 23, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
Resources
pytest-snapshot — pip install pytest-snapshot · libregistry