inline-snapshot
inline-snapshot is a Python library for golden master/snapshot/approval testing, designed to store expected values directly within your source code. It automates the process of recording, storing, and updating these values, integrating seamlessly with pytest for an enhanced developer experience. The library is currently at version 0.32.6 and receives regular updates, often with minor patch releases to address bugs and improve compatibility.
Warnings
- breaking As of v0.31.0, using `external()` for external snapshots is restricted to files within your configured test directories (default `tests/`). Using it outside will raise an exception.
- breaking Support for Python 3.8 has been removed in v0.32.0.
- gotcha A single `snapshot()` instance can only be used with one assertion operation (e.g., `==`, `<=`, `in`). Reusing the same `snapshot` object for multiple comparisons in a single test will not work as expected.
- gotcha Copying tests with external snapshots (using `external()`) might lead to duplicate UUIDs, which can cause issues.
- gotcha Pydantic models, attrs, and dataclasses with `NewType`-typed fields generated incorrect snapshots, failing to wrap the field value with the `NewType` constructor.
- gotcha Running tests with `-k` (keyword filtering) could previously raise `UsageError("unmanaged values can not be compared with snapshots")` during session teardown.
Install
-
pip install inline-snapshot
Imports
- snapshot
from inline_snapshot import snapshot
- external
from inline_snapshot import external
- outsource
from inline_snapshot import outsource
Quickstart
from inline_snapshot import snapshot
import pytest
def test_calculation():
result = 1548 * 18489
assert result == snapshot()
# To run this test and generate the snapshot value:
# pytest --inline-snapshot=create your_test_file.py
# The 'snapshot()' in the code will be updated to 'snapshot(28620972)'.