9 releases (5 breaking)

0.6.0 Oct 18, 2025
0.5.0 Apr 21, 2024
0.4.3 Jan 8, 2023
0.4.2 Nov 15, 2022
0.1.0 Oct 2, 2021

#64 in Testing

Download history 2062/week @ 2025-08-04 2932/week @ 2025-08-11 2442/week @ 2025-08-18 2280/week @ 2025-08-25 2560/week @ 2025-09-01 2468/week @ 2025-09-08 2597/week @ 2025-09-15 2614/week @ 2025-09-22 2811/week @ 2025-09-29 2320/week @ 2025-10-06 2103/week @ 2025-10-13 2204/week @ 2025-10-20 2259/week @ 2025-10-27 2177/week @ 2025-11-03 2145/week @ 2025-11-10 2148/week @ 2025-11-17

8,906 downloads per month
Used in 20 crates (19 directly)

MIT/Apache

25KB
346 lines

goldie

Crates.io Version Docs.rs Latest Build Status

Simple golden file testing for Rust.

goldie::assert!(text);

🚀 Getting started

Add goldie to your project as a dev dependency.

cargo add goldie --dev

In your test function assert the contents using goldie::assert!. The golden filename will be automatically determined based on the test file and test function name. Run tests with GOLDIE_UPDATE=1 to automatically update golden files.

#[test]
fn example() {
    let text = { /* ... run the test ... */ };

    // assert that the contents of ./testdata/example.golden
    // are equal to `text`
    goldie::assert!(text)
}

Usage

Golden file location

By default golden files are stored in a testdata directory next to the source test module. For example if your test is in src/a/tests.rs then the golden files will be stored in src/a/testdata/. This is configurable by using the Builder. For example:

# let text = "";
goldie::new!()
    .name("custom_name")
    .build()
    .assert(text);

assert!

Compares the provided value with the contents of a golden file. The value must implement Display. If they do not match the test will fail. If the environment variable GOLDIE_UPDATE=1 is set then the golden file will be updated.

assert_alt!

Compares the provided value with the contents of a golden file. The value must implement Display. The alternate formatting ({:#}) is used. If they do not match the test will fail. If the environment variable GOLDIE_UPDATE=1 is set then the golden file will be updated.

assert_debug!

Compares the provided value with the contents of a golden file. The value must implement Debug. If they do not match the test will fail. If the environment variable GOLDIE_UPDATE=1 is set then the golden file will be updated.

assert_debug_alt!

Compares the provided value with the contents of a golden file. The value must implement Debug. The alternate formatting ({:#?}) is used. If they do not match the test will fail. If the environment variable GOLDIE_UPDATE=1 is set then the golden file will be updated.

assert_json!

Golden files containing JSON data are supported using goldie::assert_json!. Something implementing serde::Serialize needs to be provided as the actual value. The golden file will be pretty-printed JSON. You can use GOLDIE_UPDATE=1 to automatically update JSON golden files.

#[test]
fn example() {
    #[derive(Serialize)]
    struct User {
        name: &'static str,
        surname: &'static str,
    }

    let u = User { name: "Steve", surname: "Harrington" };

    // assert that the contents of ./testdata/example.golden
   // are equal to the pretty-printed JSON representation of `u`
    goldie::assert_json!(&u);
}

assert_template!

Templated golden files are also supported using goldie::assert_template!. Something implementing serde::Serialize needs to be provided as context in order to render the template. Values are rendered using upon e.g. {{ value.field }}. You cannot use GOLDIE_UPDATE=1 to automatically update templated golden files.

#[test]
fn example() {
    let text = { /* ... run the test ... */ };

    // assert that the contents of ./testdata/example.golden
    // are equal to `text` after rendering with `ctx`.
    let ctx = upon::value!{ value: "Hello World!" };
    goldie::assert_template!(&ctx, text)
}

License

This project is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT for details.

Dependencies

~0–520KB
~10K SLoC