x == snapshot()
General¶
A snapshot can be compared with any value using ==
.
The value can be recorded with --inline-snapshot=create
if the snapshot is empty.
The value can later be changed with --inline-snapshot=fix
if the value the snapshot is compared with has changed.
Example:
unmanaged snapshot values¶
inline-snapshots manages everything inside snapshot(...)
, which means that the developer should not change these parts, but there are cases where it is useful to give the developer the control over the snapshot content back.
Therefor some types will be ignored by inline-snapshot and will not be updated or fixed, even if they cause tests to fail.
These types are:
- dirty-equals expressions,
- dynamic code inside
Is(...)
, - snapshots inside snapshots and
- f-strings.
inline-snapshot is able to handle these types within the following containers:
- list
- tuple
- dict
- namedtuple
- dataclass
- attrs
Other types are converted with a customizable repr()
into code. It is not possible to use unmanaged snapshot values within these objects.
dirty-equals¶
It might be, that larger snapshots with many lists and dictionaries contain some values which change frequently and are not relevant for the test. They might be part of larger data structures and be difficult to normalize.
Example:
The date can be replaced with the dirty-equals expression IsDatetime()
.
Example:
from inline_snapshot import snapshot
from dirty_equals import IsDatetime
import datetime
def get_data():
return {
"date": datetime.datetime.utcnow(),
"payload": "data changed for some good reason",
}
def test_function():
assert get_data() == snapshot(
{
"date": IsDatetime(),
"payload": "some data",
}
)
from inline_snapshot import snapshot
from dirty_equals import IsDatetime
import datetime
def get_data():
return {
"date": datetime.datetime.utcnow(),
"payload": "data changed for some good reason",
}
def test_function():
assert get_data() == snapshot(
{
"date": IsDatetime(),
"payload": "data changed for some good reason",
}
)
Is(...)¶
Is()
can be used to put runtime values inside snapshots.
It tells inline-snapshot that the developer wants control over some part of the snapshot.
from inline_snapshot import snapshot, Is
current_version = "1.5"
def request():
return {"data": "page data", "version": current_version}
def test_function():
assert request() == snapshot(
{"data": "page data", "version": Is(current_version)}
)
The snapshot does not need to be fixed when current_version
changes in the future, but "page data"
will still be fixed if it changes.
Is()
can also be used when the snapshot is evaluated multiple times, which is useful in loops or parametrized tests.
inner snapshots¶
Snapshots can be used inside other snapshots in different use cases.
conditional snapshots¶
It is also possible to use snapshots inside snapshots.
This is useful to describe version specific parts of snapshots by replacing the specific part with snapshot() if some_condition else snapshot()
.
The test has to be executed in each specific condition to fill the snapshots.
The following example shows how this can be used to run a tests with two different library versions:
from inline_snapshot import snapshot
from my_lib import version, get_schema
def test_function():
assert get_schema() == snapshot(
[
{
"name": "var_1",
"type": snapshot("int") if version < 2 else snapshot("string"),
}
]
)
The advantage of this approach is that the test uses always the correct values for each library version.
You can also extract the version logic into its own function.
from inline_snapshot import snapshot, Snapshot
from my_lib import version, get_schema
def version_snapshot(v1: Snapshot, v2: Snapshot):
return v1 if version < 2 else v2
def test_function():
assert get_schema() == snapshot(
[
{
"name": "var_1",
"type": version_snapshot(
v1=snapshot("int"), v2=snapshot("string")
),
}
]
)
common snapshot parts¶
Another use case is the extraction of common snapshot parts into an extra snapshot:
from inline_snapshot import snapshot
def some_data(name):
return {"header": "really long header\n" * 5, "your name": name}
def test_function():
header = snapshot(
"""\
really long header
really long header
really long header
really long header
really long header
"""
)
assert some_data("Tom") == snapshot(
{
"header": header,
"your name": "Tom",
}
)
assert some_data("Bob") == snapshot(
{
"header": header,
"your name": "Bob",
}
)
This simplifies test data and allows inline-snapshot to update your values if required. It makes also sure that the header is the same in both cases.
f-strings¶
f-strings are not generated by inline-snapshot, but they can be used in snapshots if you want to replace some dynamic part of a string value.
from inline_snapshot import snapshot
def get_error():
# example code which generates an error message
return __file__ + ": error at line 5"
def test_get_error():
assert get_error() == snapshot(f"{__file__}: error at line 5")
It is not required to wrap the changed value in Is(f"...")
, because inline-snapshot knows that f-strings are only generated by the developer.
Limitation
inline-snapshot is currently not able to fix the string constants within f-strings.
f"...{var}..."
works currently like Is(f"...{var}...")
and issues a warning if the value changes, giving you the opportunity to fix your f-string.
f"...{var}..."
will in the future work like f"...{Is(var)}"
. inline-snapshot will then be able to fix the string parts within the f-string.
pytest options¶
It interacts with the following --inline-snapshot
flags:
create
create a new value if the snapshot value is undefined.fix
record the value parts and store them in the source code if it is different from the current one.update
update parts of the value if their representation has changed. Parts which are replaced with dirty-equals expressions are not updated.