x <= snapshot()
General¶
A snapshot can be compared against any value with <= or >=.
This can be used to create an upper/lower bound for some result.
The snapshot value can be trimmed to the lowest/largest valid value.
Example:
from inline_snapshot import snapshot
def gcd(x, y):
iterations = 0
if x > y:
small = y
else:
small = x
for i in range(1, small + 1):
iterations += 1
if (x % i == 0) and (y % i == 0):
gcd = i
return gcd, iterations
def test_gcd():
result, iterations = gcd(12, 18)
assert result == snapshot()
assert iterations <= snapshot()
from inline_snapshot import snapshot
def gcd(x, y):
iterations = 0
if x > y:
small = y
else:
small = x
for i in range(1, small + 1):
iterations += 1
if (x % i == 0) and (y % i == 0):
gcd = i
return gcd, iterations
def test_gcd():
result, iterations = gcd(12, 18)
assert result == snapshot(6)
assert iterations <= snapshot(12)
Warning
This should not be used to check for any flaky values like the runtime of some code, because it will randomly break your tests.
The same snapshot value can also be used in multiple assertions.
pytest options¶
It interacts with the following --inline-snapshot flags:
createcreate a new value if the snapshot value is undefined.fixrecord the new value and store it in the source code if it contradicts the comparison.trimrecord the new value and store it in the source code if it is stricter than the old one.