Categories
Each snapshot change is assigned to a different category. This is done because inline-snapshot supports more than just ==
checks.
There are changes which:
- create new snapshot values
- fix your tests
- update only the syntax to a new representation
- trim unused pieces from your snapshots
Create and fix are mainly used, but it is good to know what type of change you are approving, because it helps with the decision if this changes should be applied.
Categories¶
Create¶
These changes are made when new snapshots are created.
The result of each comparison is True
, which allows to run the whole test to fill all new snapshots with values.
Example:
Fix¶
These changes are made when the snapshots comparison does not return True
any more (depending on the operation ==
, <=
, in
).
The result of each comparison is True
if you change something from this category, which allows to run the whole test and to fix other snapshots.
Info
The main reason for the different categories is to make the number of changes in the fix category as small as possible. The changes in the fix category are the only changes which change the value of the snapshots and should be reviewed carefully.
Trim¶
These changes are made when parts of the snapshots are removed which are no longer needed, or if limits can be reduced.
There might be problems in cases where you use the same snapshot in different tests, run only one test and trim the snapshot with pytest -k test_a --inline-snapshot=trim
in this case:
The value of the snapshot is reduced to 2
, because test_a()
was the only test running and inline-snapshot does not know about 5 <= s
.
It is recommended to use trim only if you run your complete test suite.
Update¶
Changes in the update category do not change the value in the code, just the representation. The reason might be that repr()
of the object has changed or that inline-snapshot provides some new logic which changes the representation. Like with the strings in the following example:
from inline_snapshot import snapshot
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
if not isinstance(other, Vector):
return NotImplemented
return self.x == other.x and self.y == other.y
def __repr__(self):
# return f"Vector(x={self.x}, y={self.y})"
return f"Vector({self.x}, {self.y})"
def test_something():
assert "a\nb\nc\n" == snapshot("a\nb\nc\n")
assert 5 == snapshot(4 + 1)
assert Vector(1, 2) == snapshot(Vector(x=1, y=2))
from inline_snapshot import snapshot
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
if not isinstance(other, Vector):
return NotImplemented
return self.x == other.x and self.y == other.y
def __repr__(self):
# return f"Vector(x={self.x}, y={self.y})"
return f"Vector({self.x}, {self.y})"
def test_something():
assert "a\nb\nc\n" == snapshot(
"""\
a
b
c
"""
)
assert 5 == snapshot(5)
assert Vector(1, 2) == snapshot(Vector(1, 2))
The approval of this type of changes is easier, because inline-snapshot assures that the value has not changed.
It is not necessary, but recommended to make these changes for the following reason:
The goal of inline-snapshot is to generate the values for you in the correct format so that no manual editing is required. This improves your productivity and saves time. Keep in mind that any changes you make to your snapshots will likely need to be redone if your program's behavior (and expected values) change. Inline-snapshot uses the update category to let you know when it has a different opinion than you about how the code should look. You can agree with inline-snapshot and accept the changes or you can use one of the following options to tell inline-snapshot what the code should look like:
-
change the
__repr__
implementation of your object or use customize repr if the class is not part of your codebase. -
define a format-command if another tool has a different opinion about how your code should look. Inline-snapshot will apply this formatting before reporting an update.
-
inline-snapshot manages everything within
snapshot(...)
, but you can take control by using Is() in cases where you want to use custom code (like local variables) in your snapshots. -
you can also open an issue if you have a specific problem with the way inline-snapshot generates the code.