{"id":4261,"library":"snapshottest","title":"snapshottest","description":"`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.","status":"maintenance","version":"0.6.0","language":"en","source_language":"en","source_url":"https://github.com/syrusakbary/snapshottest","tags":["testing","pytest","unittest","snapshot-testing","api-testing"],"install":[{"cmd":"pip install snapshottest","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for pytest integration, a common use case.","package":"pytest","optional":true}],"imports":[{"note":"This is a pytest fixture injected into test functions.","symbol":"snapshot","correct":"def test_something(snapshot): snapshot.assert_match(my_data)"},{"note":"For unittest, Nose, or Django, inherit from `snapshottest.TestCase` instead of using the pytest fixture.","wrong":"from snapshottest.pytest import SnapshotTest","symbol":"TestCase","correct":"from snapshottest import TestCase"}],"quickstart":{"code":"import pytest\n\ndef test_my_data_structure(snapshot):\n    # Simulate some data or API response\n    my_complex_data = {\n        \"id\": 123,\n        \"name\": \"Test User\",\n        \"settings\": {\"theme\": \"dark\", \"notifications\": True},\n        \"items\": [\"apple\", \"banana\", \"cherry\"]\n    }\n    snapshot.assert_match(my_complex_data)\n\n# To run this test:\n# 1. Save it as e.g., `test_my_app.py`\n# 2. Run `pytest` from your terminal. The first run will create a snapshot file.\n# 3. If the output changes and it's intentional, run `pytest --snapshot-update` to update the snapshot.\n","lang":"python","description":"This quickstart demonstrates using `snapshottest` with pytest. The first time the test runs, it generates a snapshot file containing the serialized `my_complex_data`. Subsequent runs compare the current output against this snapshot. If an intentional change occurs, use `pytest --snapshot-update` to re-record the snapshot."},"warnings":[{"fix":"Mock or normalize dynamic values before taking a snapshot. For example, replace `datetime.now()` with a fixed, known date during testing.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Keep snapshots small and focused. Test specific, isolated parts of your output rather than entire component trees or API responses. Break down complex outputs into multiple, granular snapshots if necessary.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Use snapshot testing primarily for stable output contracts (like API responses or data transformations) rather than frequently changing UI structures. Supplement with more explicit unit or integration tests for core logic.","message":"`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.","severity":"gotcha","affected_versions":"All"},{"fix":"To use the pytest `snapshot` fixture within `unittest.TestCase` or `snapshottest.TestCase` methods, a custom pytest fixture must be created in `conftest.py` to wrap and expose the `snapshot` fixture to the class instance. This is shown in issue #53 on the GitHub repository.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Consider evaluating alternative snapshot testing libraries for Python, especially for new projects or if advanced features like custom serializers are required.","message":"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.","severity":"gotcha","affected_versions":"0.6.0 and earlier"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}