Welcome to inline-snapshot¶
inline-snapshot can be used for different things:
- golden master/approval/snapshot testing. The idea is that you have a function with a currently unknown result and you want to write a tests, which ensures that the result does not change during refactoring.
- Compare things which are complex like lists with lot of numbers or complex data structures.
- Things which might change during the development like error messages.
inline-snapshot
automates the process of recording, storing and updating the value you want to compare with.
The value is converted with repr()
and stored in the source file as argument of the snapshot()
function.
Usage¶
You can use snapshot()
instead of the value which you want to compare with and run the tests to record the correct values.
Your tests will break, if you change your code by adding // 18
.
Maybe that is correct and you should fix your code, or
your code is correct and you want to update your test results.
Please verify the new results. git diff
will give you a good overview over all changed results.
Use pytest -k test_something --inline-snapshot=fix
if you only want to change one test.
Supported operations¶
You can use snapshot(x)
like you can use x
in your assertion with a limited set of operations:
value == snapshot()
to compare with something,value <= snapshot()
to ensure that something gets smaller/larger over time (number of iterations of an algorithm you want to optimize for example),value in snapshot()
to check if your value is in a known set of values,snapshot()[key]
to generate new sub-snapshots on demand.
Warning
One snapshot can only be used with one operation. The following code will not work:
Supported usage¶
It is possible to place snapshot()
anywhere in the tests and reuse it multiple times.
snapshot()
can also be used in loops:
or passed as an argument to a function:
Feedback¶
inline-snapshot provides some advanced ways to work with snapshots.
I would like to know how these features are used to further improve this small library. Let me know if you've found interesting use cases for this library via twitter, fosstodon or in the github discussions.
Sponsors¶
I would like to thank my sponsors. Without them, I would not be able to invest so much time in my projects.
Bronze sponsor 🥉¶
Issues¶
If you encounter any problems, please report an issue along with a detailed description.