Pytest Snapshot Testing

0.9.0 · active · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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.

view raw JSON →