ditto¶
The names most code imports: import ditto.
record = pytest.mark.record
module-attribute
¶
Mark a test's snapshots with a recorder, and optionally a target.
Use as @ditto.record("yaml"), @ditto.record("json", target=uri) or
@ditto.record("json", target_profile=name). The recorder name is required.
A test may have one record mark, set on its function, class or module.
yaml = record('yaml')
module-attribute
¶
Store a test's snapshots as YAML: @ditto.record("yaml").
json = record('json')
module-attribute
¶
Store a test's snapshots as strict JSON, the default: @ditto.record("json").
version = __version__
module-attribute
¶
The installed pytest-ditto version, such as "2.0.0".
Snapshot(group_name, module, target, _backend, recorder=_UNSET, recorder_name=_UNSET, mode=SnapshotMode.RECORD, nodeid='', target_id='', _tracker=_SessionTracker())
dataclass
¶
Where a test's snapshots are stored and how they are recorded.
The snapshot fixture builds one for each test, and calling it,
snapshot(data, key), stores or loads one snapshot. Construct a
Snapshot directly only when fixture injection isn't available; it is
immutable and holds no open resources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_name
|
str
|
Prefix used in snapshot keys. Derived from the pytest nodeid minus the file path (e.g. "test_something" or "TestClass.test_something"). |
required |
module
|
str
|
Rootdir-relative test file stem (e.g. "tests/bar/test_api"). Required for all backends. Provides namespace isolation across test files. |
required |
target
|
str
|
URI of the storage location. Its scheme sets the storage key format:
|
required |
_backend
|
MutableMapping[str, bytes]
|
The mapping snapshots are stored in, such as a |
required |
recorder
|
Recorder
|
Serialisation strategy. Defaults to strict JSON. |
_UNSET
|
recorder_name
|
str
|
The recorder's registered name, which is its persisted identifier: it
ends snapshot filenames and is recorded in |
_UNSET
|
mode
|
SnapshotMode
|
Whether a snapshot is recorded, updated, or only verified. Defaults to
|
RECORD
|
nodeid
|
str
|
Full pytest node id for the owning test, e.g. |
''
|
target_id
|
str
|
Portable lock-file target id (rootdir-relative for |
''
|
_tracker
|
_SessionTracker
|
Where snapshot activity is recorded. The fixture passes its pytest
session's tracker; a directly constructed |
_SessionTracker()
|
__call__(data, key)
¶
Store or load the snapshot for key, and return its value.
What happens depends on mode and on whether the snapshot is stored;
see SnapshotMode. The value returned is always what the recorder
reads back, never data itself.
Raises:
| Type | Description |
|---|---|
TypeError
|
When |
DuplicateSnapshotKeyError
|
When this test has already used |
DittoSnapshotNameCollisionError
|
When another snapshot this session has the same storage name. |
Source code in ditto/snapshot.py
SnapshotMode
¶
Bases: Enum
How a Snapshot treats the stored value for a key.
Attributes:
| Name | Type | Description |
|---|---|---|
RECORD |
Save the value when the key is absent; otherwise return the stored value. |
|
UPDATE |
Always save the value, overwriting a stored one. Set by |
|
VERIFY |
Never write: return the stored value, or the given value when the key is
absent. Set by |
|
A value that is saved, or returned under VERIFY for an absent key, is |
|
|
serialised and deserialised first, and the deserialised value is returned. |
|
DuplicateSnapshotKeyError(key)
¶
Bases: DittoException
Raised when a test calls snapshot() twice with the same key.