13 releases (stable)

1.5.0 Apr 2, 2024
1.4.1 Feb 28, 2023
1.4.0 Jul 19, 2022
1.2.2 Dec 25, 2021
0.1.0 Aug 21, 2020

#14 in Testing

Download history 83304/week @ 2024-08-06 80836/week @ 2024-08-13 89216/week @ 2024-08-20 90011/week @ 2024-08-27 99335/week @ 2024-09-03 98840/week @ 2024-09-10 92599/week @ 2024-09-17 102956/week @ 2024-09-24 96679/week @ 2024-10-01 82597/week @ 2024-10-08 113862/week @ 2024-10-15 99842/week @ 2024-10-22 97283/week @ 2024-10-29 80477/week @ 2024-11-05 117632/week @ 2024-11-12 103580/week @ 2024-11-19

411,936 downloads per month
Used in 186 crates (174 directly)

MIT/Apache

28KB
662 lines

expect-test

Docs

Minimalistic snapshot testing for Rust.

Updating a failing test:

https://user-images.githubusercontent.com/1711539/120119633-73b3f100-c1a1-11eb-91be-4c61a23e7060.mp4

Adding a new test:

expect-fresh

Checkout the docs for more: https://docs.rs/expect-test.


lib.rs:

Minimalistic snapshot testing for Rust.

Introduction

expect_test is a small addition over plain assert_eq! testing approach, which allows to automatically update tests results.

The core of the library is the expect! macro. It can be though of as a super-charged string literal, which can update itself.

Let's see an example:

use expect_test::expect;

let actual = 2 + 2;
let expected = expect!["5"]; // or expect![["5"]]
expected.assert_eq(&actual.to_string())

Running this code will produce a test failure, as "5" is indeed not equal to "4". Running the test with UPDATE_EXPECT=1 env variable however would "magically" update the code to:

let actual = 2 + 2;
let expected = expect!["4"];
expected.assert_eq(&actual.to_string())

This becomes very useful when you have a lot of tests with verbose and potentially changing expected output.

Under the hood, the expect! macro uses file!, line! and column! to record source position at compile time. At runtime, this position is used to patch the file in-place, if UPDATE_EXPECT is set.

Guide

expect! returns an instance of Expect struct, which holds position information and a string literal. Use Expect::assert_eq for string comparison. Use Expect::assert_debug_eq for verbose debug comparison. Note that leading indentation is automatically removed.

use expect_test::expect;

#[derive(Debug)]
struct Foo {
    value: i32,
}

let actual = Foo { value: 92 };
let expected = expect![["
    Foo {
        value: 92,
    }
"]];
expected.assert_debug_eq(&actual);

Be careful with assert_debug_eq - in general, stability of the debug representation is not guaranteed. However, even if it changes, you can quickly update all the tests by running the test suite with UPDATE_EXPECT environmental variable set.

If the expected data is too verbose to include inline, you can store it in an external file using the expect_file! macro:

use expect_test::expect_file;

let actual = 42;
let expected = expect_file!["./the-answer.txt"];
expected.assert_eq(&actual.to_string());

File path is relative to the current file.

Suggested Workflows

I like to use data-driven tests with expect_test. I usually define a single driver function check and then call it from individual tests:

use expect_test::{expect, Expect};

fn check(actual: i32, expect: Expect) {
    let actual = actual.to_string();
    expect.assert_eq(&actual);
}

#[test]
fn test_addition() {
    check(90 + 2, expect![["92"]]);
}

#[test]
fn test_multiplication() {
    check(46 * 2, expect![["92"]]);
}

Each test's body is a single call to check. All the variation in tests comes from the input data.

When writing a new test, I usually copy-paste an old one, leave the expect blank and use UPDATE_EXPECT to fill the value for me:

#[test]
fn test_division() {
    check(92 / 2, expect![[]])
}

See https://blog.janestreet.com/using-ascii-waveforms-to-test-hardware-designs/ for a cool example of snapshot testing in the wild!

Alternatives

  • insta - a more feature full snapshot testing library.
  • k9 - a testing library which includes support for snapshot testing among other things.

Maintenance status

The main customer of this library is rust-analyzer. The library is stable, it is planned to not release any major versions past 1.0.

Minimal Supported Rust Version

This crate's minimum supported rustc version is 1.60.0. MSRV is updated conservatively, supporting roughly 10 minor versions of rustc. MSRV bump is not considered semver breaking, but will require at least minor version bump.

Dependencies

~115KB