内联快照依赖配套的 cargo-insta 工具,需额外安装;快照变更需用 cargo-insta 审阅接受,否则测试会失败;README 未说明其他依赖版本限制

insta
mitsuhikoApache-2.0活跃维护Rust 的快照测试库,自动记录并比对程序输出,输出变化时给出清晰的 diff,省去手写断言的麻烦。
A snapshot testing library for rust
Rust 快照测试库,把复杂或易变的期望值存成快照文件并支持审阅变更
Rust 的快照测试库,自动记录并比对程序输出,输出变化时给出清晰的 diff,省去手写断言的麻烦。
核心能力
- 用 assert_debug_snapshot! 等宏对复杂值做快照断言
- 支持内联快照,快照直接写在源码里而非单独文件
- 配套 cargo-insta 工具审阅与更新快照
- 基于 similar 库提供快照差异对比
- 提供 VSCode 扩展高亮并审阅 .snap 文件
容易踩的坑
- 1.49.0最新正式版2026-10-03 · 2026-10-03 01:17 UTC
Release Notes
- Allow
Option<&OsStr>snapshot names, such asPath::file_name(), without explicit string conversion. #929 (@dislogical) - Add prebuilt
cargo-instabinaries for ARM64 Linux (aarch64-unknown-linux-gnu). #942 (@aljohri) - Fix JSON serialization of maps keyed by unit enum variants. Keys use the serialized variant name, including serde renames. #934 (@teddytennant)
- Remove trailing semicolons from serialization macro bodies to avoid compiler warnings when used in expression position. #940 (@Tiwalun)
- Mention
INSTA_UPDATE=alwaysin snapshot mismatch hints for file snapshots. #927 (@hiro-nikaitou) - Update the lockfile's
tempfileandrustixdependencies to fix builds with newer Rust toolchains. #939 - Fix
cargo insta test --all-targets --test-runner nextestfailing with "Can't mix --doc with other target selecting options". Likecargo test --all-targets, it no longer runs doctests. #460
Install cargo-insta 1.49.0
Install prebuilt binaries via shell script
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/mitsuhiko/insta/releases/download/1.49.0/cargo-insta-installer.sh | shInstall prebuilt binaries via powershell script
powershell -ExecutionPolicy Bypass -c "irm https://github.com/mitsuhiko/insta/releases/download/1.49.0/cargo-insta-installer.ps1 | iex"Download cargo-insta 1.49.0
File Platform Checksum cargo-insta-aarch64-apple-darwin.tar.xz Apple Silicon macOS checksum cargo-insta-x86_64-apple-darwin.tar.xz Intel macOS checksum cargo-insta-x86_64-pc-windows-msvc.zip x64 Windows checksum cargo-insta-aarch64-unknown-linux-gnu.tar.xz ARM64 Linux checksum cargo-insta-x86_64-unknown-linux-gnu.tar.xz x64 Linux checksum cargo-insta-x86_64-unknown-linux-musl.tar.xz x64 MUSL Linux checksum This was written by Codex on behalf of @max-sixty
- Allow
- 1.48.02026-06-11 · 2026-06-11 14:16 UTC
查看更新说明
Release Notes
- Add
strip_ansi_escape_codessetting which removes ANSI escape sequences (color codes, cursor movement, etc.) from snapshot content before comparison. Requires thefiltersfeature. #899 (@pierluigilenoci) - Add opt-in support for YAML literal blocks for multiline strings in snapshot
metadata fields such as
descriptionandexpression. SetINSTA_YAML_BLOCK_STYLE=1to enable. #851 (@ivov) - Setting
CI=truenormally makescargo insta testbehave as though--checkwas passed. Explicit snapshot handling options such as--acceptnow take precedence over this environment variable, allowing users to override this behavior if they want to. #924 - Fix
cargo insta test --profilebeing forwarded to nextest as the nextest profile instead of the cargo build profile; it now translates to--cargo-profilefor the nextest runner. Add--nextest-profileto select the nextest profile. #910 - Fix
cargo insta pending-snapshotsprinting unusable\\?\-prefixed paths on Windows. The--snapshotfilter now also accepts partial paths: any trailing path suffix of the snapshot file matches, so a bare--snapshot my_test.snapworks. #904 - Accepting a binary snapshot no longer fails with
os error 2when its data file is missing (e.g. gitignored and not committed). #914
Install cargo-insta 1.48.0
Install prebuilt binaries via shell script
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/mitsuhiko/insta/releases/download/1.48.0/cargo-insta-installer.sh | shInstall prebuilt binaries via powershell script
powershell -ExecutionPolicy Bypass -c "irm https://github.com/mitsuhiko/insta/releases/download/1.48.0/cargo-insta-installer.ps1 | iex"Download cargo-insta 1.48.0
File Platform Checksum cargo-insta-aarch64-apple-darwin.tar.xz Apple Silicon macOS checksum cargo-insta-x86_64-apple-darwin.tar.xz Intel macOS checksum cargo-insta-x86_64-pc-windows-msvc.zip x64 Windows checksum cargo-insta-x86_64-unknown-linux-gnu.tar.xz x64 Linux checksum cargo-insta-x86_64-unknown-linux-musl.tar.xz x64 MUSL Linux checksum - Add
- 1.47.22026-03-30 · 2026-03-30 20:57 UTC
查看更新说明
Release Notes
- Restore
Send + SynconSettings,Redactions, andRedactionby reverting theArctoRcchange from 1.47.0, which was semver-breaking. #873 #874 - Add
Send + Syncbounds toComparatortrait for consistency withArc-based storage. #872 - Add compile-time assertion to prevent future auto-trait regressions.
Install cargo-insta 1.47.2
Install prebuilt binaries via shell script
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/mitsuhiko/insta/releases/download/1.47.2/cargo-insta-installer.sh | shInstall prebuilt binaries via powershell script
powershell -ExecutionPolicy Bypass -c "irm https://github.com/mitsuhiko/insta/releases/download/1.47.2/cargo-insta-installer.ps1 | iex"Download cargo-insta 1.47.2
File Platform Checksum cargo-insta-aarch64-apple-darwin.tar.xz Apple Silicon macOS checksum cargo-insta-x86_64-apple-darwin.tar.xz Intel macOS checksum cargo-insta-x86_64-pc-windows-msvc.zip x64 Windows checksum cargo-insta-x86_64-unknown-linux-gnu.tar.xz x64 Linux checksum cargo-insta-x86_64-unknown-linux-musl.tar.xz x64 MUSL Linux checksum - Restore
- 1.47.12026-03-29 · 2026-03-29 05:32 UTC
查看更新说明
Release Notes
- Revert sorting of sequences in
sort_maps. The change in 1.47.0 sorted allSeqvalues (includingVec), not just non-deterministic collections likeHashSet, which was a breaking change. #876
Install cargo-insta 1.47.1
Install prebuilt binaries via shell script
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/mitsuhiko/insta/releases/download/1.47.1/cargo-insta-installer.sh | shInstall prebuilt binaries via powershell script
powershell -ExecutionPolicy Bypass -c "irm https://github.com/mitsuhiko/insta/releases/download/1.47.1/cargo-insta-installer.ps1 | iex"Download cargo-insta 1.47.1
File Platform Checksum cargo-insta-aarch64-apple-darwin.tar.xz Apple Silicon macOS checksum cargo-insta-x86_64-apple-darwin.tar.xz Intel macOS checksum cargo-insta-x86_64-pc-windows-msvc.zip x64 Windows checksum cargo-insta-x86_64-unknown-linux-gnu.tar.xz x64 Linux checksum cargo-insta-x86_64-unknown-linux-musl.tar.xz x64 MUSL Linux checksum - Revert sorting of sequences in
- 1.47.02026-03-27 · 2026-03-27 01:25 UTC
查看更新说明
Release Notes
- Add
Comparatortrait for customizing how snapshot values are compared. #872 (@dstu) - Sort sequences in
sort_mapsto fix non-deterministicHashSetsnapshots. #876 - Improve TOML serialization error message for unsupported types, suggesting
assert_json_snapshot!orassert_yaml_snapshot!as alternatives. #880 - Remove unnecessary
Send + Syncbounds fromRedaction, allowing non-Sendclosures in dynamic redactions. #874 - Don't use
ArcinSettingsunnecessarily. #873 (@dstu) - Upgrade
consoleto 0.16 and MSRV to 1.66. #885 - Upgrade
toml-editto 0.25. #882 (@alexanderkjall)
Install cargo-insta 1.47.0
Install prebuilt binaries via shell script
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/mitsuhiko/insta/releases/download/1.47.0/cargo-insta-installer.sh | shInstall prebuilt binaries via powershell script
powershell -ExecutionPolicy Bypass -c "irm https://github.com/mitsuhiko/insta/releases/download/1.47.0/cargo-insta-installer.ps1 | iex"Download cargo-insta 1.47.0
File Platform Checksum cargo-insta-aarch64-apple-darwin.tar.xz Apple Silicon macOS checksum cargo-insta-x86_64-apple-darwin.tar.xz Intel macOS checksum cargo-insta-x86_64-pc-windows-msvc.zip x64 Windows checksum cargo-insta-x86_64-unknown-linux-gnu.tar.xz x64 Linux checksum cargo-insta-x86_64-unknown-linux-musl.tar.xz x64 MUSL Linux checksum - Add
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Introduction
Snapshots tests (also sometimes called approval tests) are tests that
assert values against a reference value (the snapshot). This is similar
to how assert_eq! lets you compare a value against a reference value but
unlike simple string assertions, snapshot tests let you test against complex
values and come with comprehensive tools to review changes.
Snapshot tests are particularly useful if your reference values are very large or change often.
Example
#[test]
fn test_hello_world() {
insta::assert_debug_snapshot!(vec![1, 2, 3]);
}
Curious? There is a screencast that shows the entire workflow: watch the insta introduction screencast. Or if you're not into videos, read the 5 minute introduction.
Insta also supports inline snapshots which are stored right in your source file instead of separate files. This is accomplished by the companion cargo-insta tool.
Editor Support
For looking at .snap files there is a vscode extension
which can syntax highlight snapshot files, review snapshots and more. It can be installed from the
marketplace: view on marketplace.

Diffing
Insta uses similar for all its diffing
operations. You can use it independently of insta. You can use the
similar-asserts crate to get
inline diffs for the standard assert_eq! macro to achieve insta like diffs
for regular comparisons:
use similar_asserts::assert_eq;
fn main() {
let reference = vec![1, 2, 3, 4];
assert_eq!(reference, (0..4).collect::<Vec<_>>());
}
Sponsor
If you like the project and find it useful you can become a sponsor.