Insta is a nice snapshot testing tool for Rust.
Snapshot testing
Snapshot tests are a more general extension of the normal tests we have in Rust.
Usually we use assert_eq!
and similar macros. Insta let you deal with the reference values in a richer way and store them inside or outside your codebase.
Pitfalls to keep in mind
I spent way too much time stumbling upon a bunch of annoyances while converting some tests in rust-code-analysis so it is better to list them so you will avoid to suffer from similar mistakes:
-
cargo insta
is git-aware, make sure to pass--no-ignore
if your snapshots directory is populated by different means even better if you can use git-submodules. - If you deal with floating points, round them to the precision you care about. Different arches/libc may have different precisions
- If you save paths keep in mind that Windows is special in this regard and you may have to manually serialize it.
Nice things to keep in mind
-
cargo test -- --nocapture
provides enough information to see what went wrong. -
cargo insta test --review
makes adding more tests and make sure you do not break anything by mistake.
That's all for today
Top comments (0)