insta

mitsuhikoApache-2.0活跃维护

Rust 的快照测试库,自动记录并比对程序输出,输出变化时给出清晰的 diff,省去手写断言的麻烦。

A snapshot testing library for rust

1.49.0发布于 2026-10-03 01:17 UTC2,984star172fork同步于 2026-11-10 11:42 UTC

怎么上手

由 README 提炼,执行前请确认
cargo add insta --dev
硬件要求按 README 说明

README 未说明

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

Rust 快照测试库,把复杂或易变的期望值存成快照文件并支持审阅变更

Rust 的快照测试库,自动记录并比对程序输出,输出变化时给出清晰的 diff,省去手写断言的麻烦。

核心能力

  • 用 assert_debug_snapshot! 等宏对复杂值做快照断言
  • 支持内联快照,快照直接写在源码里而非单独文件
  • 配套 cargo-insta 工具审阅与更新快照
  • 基于 similar 库提供快照差异对比
  • 提供 VSCode 扩展高亮并审阅 .snap 文件

容易踩的坑

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

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本1.49.0
  1. 1.49.0最新正式版2026-10-03 · 2026-10-03 01:17 UTC

    Release Notes

    • Allow Option<&OsStr> snapshot names, such as Path::file_name(), without explicit string conversion. #929 (@dislogical)
    • Add prebuilt cargo-insta binaries 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=always in snapshot mismatch hints for file snapshots. #927 (@hiro-nikaitou)
    • Update the lockfile's tempfile and rustix dependencies to fix builds with newer Rust toolchains. #939
    • Fix cargo insta test --all-targets --test-runner nextest failing with "Can't mix --doc with other target selecting options". Like cargo 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 | sh
    

    Install 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

    FilePlatformChecksum
    cargo-insta-aarch64-apple-darwin.tar.xzApple Silicon macOSchecksum
    cargo-insta-x86_64-apple-darwin.tar.xzIntel macOSchecksum
    cargo-insta-x86_64-pc-windows-msvc.zipx64 Windowschecksum
    cargo-insta-aarch64-unknown-linux-gnu.tar.xzARM64 Linuxchecksum
    cargo-insta-x86_64-unknown-linux-gnu.tar.xzx64 Linuxchecksum
    cargo-insta-x86_64-unknown-linux-musl.tar.xzx64 MUSL Linuxchecksum

    This was written by Codex on behalf of @max-sixty

  2. 1.48.02026-06-11 · 2026-06-11 14:16 UTC
    查看更新说明

    Release Notes

    • Add strip_ansi_escape_codes setting which removes ANSI escape sequences (color codes, cursor movement, etc.) from snapshot content before comparison. Requires the filters feature. #899 (@pierluigilenoci)
    • Add opt-in support for YAML literal blocks for multiline strings in snapshot metadata fields such as description and expression. Set INSTA_YAML_BLOCK_STYLE=1 to enable. #851 (@ivov)
    • Setting CI=true normally makes cargo insta test behave as though --check was passed. Explicit snapshot handling options such as --accept now take precedence over this environment variable, allowing users to override this behavior if they want to. #924
    • Fix cargo insta test --profile being forwarded to nextest as the nextest profile instead of the cargo build profile; it now translates to --cargo-profile for the nextest runner. Add --nextest-profile to select the nextest profile. #910
    • Fix cargo insta pending-snapshots printing unusable \\?\-prefixed paths on Windows. The --snapshot filter now also accepts partial paths: any trailing path suffix of the snapshot file matches, so a bare --snapshot my_test.snap works. #904
    • Accepting a binary snapshot no longer fails with os error 2 when 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 | sh
    

    Install 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

    FilePlatformChecksum
    cargo-insta-aarch64-apple-darwin.tar.xzApple Silicon macOSchecksum
    cargo-insta-x86_64-apple-darwin.tar.xzIntel macOSchecksum
    cargo-insta-x86_64-pc-windows-msvc.zipx64 Windowschecksum
    cargo-insta-x86_64-unknown-linux-gnu.tar.xzx64 Linuxchecksum
    cargo-insta-x86_64-unknown-linux-musl.tar.xzx64 MUSL Linuxchecksum
  3. 1.47.22026-03-30 · 2026-03-30 20:57 UTC
    查看更新说明

    Release Notes

    • Restore Send + Sync on Settings, Redactions, and Redaction by reverting the Arc to Rc change from 1.47.0, which was semver-breaking. #873 #874
    • Add Send + Sync bounds to Comparator trait for consistency with Arc-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 | sh
    

    Install 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

    FilePlatformChecksum
    cargo-insta-aarch64-apple-darwin.tar.xzApple Silicon macOSchecksum
    cargo-insta-x86_64-apple-darwin.tar.xzIntel macOSchecksum
    cargo-insta-x86_64-pc-windows-msvc.zipx64 Windowschecksum
    cargo-insta-x86_64-unknown-linux-gnu.tar.xzx64 Linuxchecksum
    cargo-insta-x86_64-unknown-linux-musl.tar.xzx64 MUSL Linuxchecksum
  4. 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 all Seq values (including Vec), not just non-deterministic collections like HashSet, 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 | sh
    

    Install 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

    FilePlatformChecksum
    cargo-insta-aarch64-apple-darwin.tar.xzApple Silicon macOSchecksum
    cargo-insta-x86_64-apple-darwin.tar.xzIntel macOSchecksum
    cargo-insta-x86_64-pc-windows-msvc.zipx64 Windowschecksum
    cargo-insta-x86_64-unknown-linux-gnu.tar.xzx64 Linuxchecksum
    cargo-insta-x86_64-unknown-linux-musl.tar.xzx64 MUSL Linuxchecksum
  5. 1.47.02026-03-27 · 2026-03-27 01:25 UTC
    查看更新说明

    Release Notes

    • Add Comparator trait for customizing how snapshot values are compared. #872 (@dstu)
    • Sort sequences in sort_maps to fix non-deterministic HashSet snapshots. #876
    • Improve TOML serialization error message for unsupported types, suggesting assert_json_snapshot! or assert_yaml_snapshot! as alternatives. #880
    • Remove unnecessary Send + Sync bounds from Redaction, allowing non-Send closures in dynamic redactions. #874
    • Don't use Arc in Settings unnecessarily. #873 (@dstu)
    • Upgrade console to 0.16 and MSRV to 1.66. #885
    • Upgrade toml-edit to 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 | sh
    

    Install 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

    FilePlatformChecksum
    cargo-insta-aarch64-apple-darwin.tar.xzApple Silicon macOSchecksum
    cargo-insta-x86_64-apple-darwin.tar.xzIntel macOSchecksum
    cargo-insta-x86_64-pc-windows-msvc.zipx64 Windowschecksum
    cargo-insta-x86_64-unknown-linux-gnu.tar.xzx64 Linuxchecksum
    cargo-insta-x86_64-unknown-linux-musl.tar.xzx64 MUSL Linuxchecksum

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Crates.io License Documentation VSCode Extension

    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.

    jump to definition

    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.

    License and Links