snapshottest
`snapshottest` is a Python library that provides utilities for snapshot testing across various testing frameworks, including pytest, unittest, Django, and Nose. It captures the serialized output of APIs or data structures and compares them against a stored reference, helping developers identify unintended changes and regressions. The library is currently at version 0.6.0, with its last release in September 2020, suggesting a maintenance-focused cadence rather than active feature development.
Warnings
- gotcha Snapshot tests can be flaky or misleading if they include dynamic content such as dates, timestamps, unique IDs, or random numbers. These values change on every run, causing unnecessary test failures.
- gotcha Large or overly broad snapshots are difficult to review and maintain. Developers often become 'snapshot fatigued' and blindly update snapshots without scrutinizing the changes, which defeats the purpose of testing.
- gotcha `snapshottest` can be tightly coupled to implementation details. Minor refactoring or internal changes (e.g., changing HTML attributes or JSON keys that don't affect behavior) can cause widespread snapshot failures.
- gotcha When using `unittest.TestCase` subclasses in a pytest environment, the `snapshot` fixture is not directly available. Attempting to use `snapshot` as a parameter in a `TestCase` method will fail.
- gotcha The `snapshottest` library has not seen updates since September 2020. More actively maintained alternatives like `syrupy` or `pytest-snapshot` offer additional features such as custom serialization, better snapshot file organization, and support for newer Python versions and frameworks.
Install
-
pip install snapshottest
Imports
- snapshot
def test_something(snapshot): snapshot.assert_match(my_data)
- TestCase
from snapshottest import TestCase
Quickstart
import pytest
def test_my_data_structure(snapshot):
# Simulate some data or API response
my_complex_data = {
"id": 123,
"name": "Test User",
"settings": {"theme": "dark", "notifications": True},
"items": ["apple", "banana", "cherry"]
}
snapshot.assert_match(my_complex_data)
# To run this test:
# 1. Save it as e.g., `test_my_app.py`
# 2. Run `pytest` from your terminal. The first run will create a snapshot file.
# 3. If the output changes and it's intentional, run `pytest --snapshot-update` to update the snapshot.