This is a collection of common patterns for how to best use Insta in some more complex setups.


If you are using the rstest crate, more specifically the #[case] feature you might want to use this pattern to ensure a pleasant experience. Because rstest will parametrize your tests insta will normally have challenges associating your snapshots with the right parametrized test. In that situation you can use the following macro to add a suffix to your snapshot tests specific to your input values:

macro_rules! set_snapshot_suffix {
    ($($expr:expr),*) => {
        let mut settings = insta::Settings::clone_current();
        let _guard = settings.bind_to_scope();

After that you can trivially set the snapshot suffix in your parametrized test:

#[case(0, 2)]
#[case(2, 4)]
fn test_it(#[case] a: usize, #[case] b: usize) {
    set_snapshot_suffix!("{}-{}", a, b);
    insta::assert_debug_snapshot!(a * b);

In the above example the snapshots will then be named as follows:


Uploading Snapshots from GitHub Actions

By default when running in a CI environment, insta won't generate new snapshot files. When you use GitHub Actions however you can upload build artifacts after the test run to download the changed artifacts. To accomplish this you need to force insta to write new snapshot files.

The following configuration forces files to be created and creates an artifact of all new snapshot files.

    name: Snapshot Tests
    runs-on: ubuntu-latest

      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@master
          toolchain: stable
      - name: Test
        id: run_tests
        run: cargo test --all-features
          INSTA_UPDATE: new
      - name: Upload snapshots of failed tests
          if: ${{ failure() && steps.run_tests.outcome == 'failure' }}
          uses: actions/upload-artifact@v3
            name: failed-snapshots
            path: "**/snapshots/*"
Found an issue? You can edit this page on GitHub.