Crate snapbox

source ·
Expand description

§Snapshot testing toolbox

When you have to treat your tests like pets, instead of cattle

snapbox is a snapshot-testing toolbox that is ready to use for verifying output from

  • Function return values
  • CLI stdout/stderr
  • Filesystem changes

It is also flexible enough to build your own test harness like trycmd.

§Which tool is right

  • cram: End-to-end CLI snapshotting agnostic of any programming language
  • trycmd: For running a lot of blunt tests (limited test predicates)
    • Particular attention is given to allow the test data to be pulled into documentation, like with mdbook
  • snapbox: When you want something like trycmd in one off cases or you need to customize trycmds behavior.
  • assert_cmd + assert_fs: Test cases follow a certain pattern but special attention is needed in how to verify the results.
  • Hand-written test cases: for peculiar circumstances

§Getting Started

Testing Functions:

Testing Commands:

Testing Filesystem Interactions:

  • path::PathFixture: Working directory for tests
  • Assert: Diff a directory against files present in a pattern directory

You can also build your own version of these with the lower-level building blocks these are made of.

Feature Flags

  • harness — Simple input/output test harness
  • detect-encoding — Smarter binary file detection
  • dir — Snapshotting of directories
  • path — Deprecated since 0.5.11, replaced with dir
  • cmd — Snapshotting of commands
  • examples — Building of examples for snapshotting
  • json — Snapshotting of json
  • term-svg — Snapshotting of term styling
  • structured-data — Snapshotting of structured data
  • debug — Extra debugging information

Default Feature Flags

  • diff (enabled by default) — Fancy diffs on failure
  • color — Colored output support
  • color-auto (enabled by default) — Auto-detect whether to use colors



snapbox::assert_data_eq!("Hello many people!", "Hello [..] people!");


let actual = "...";
    .eq_(actual, snapbox::file!["help_output_is_clean.txt"]);



fn setup(input_path: std::path::PathBuf) -> snapbox::harness::Case {
   let name = input_path.file_name().unwrap().to_str().unwrap().to_owned();
   let expected = input_path.with_extension("out");
   snapbox::harness::Case {
       fixture: input_path,

fn test(input_path: &std::path::Path) -> Result<usize, Box<dyn std::error::Error>> {
   let raw = std::fs::read_to_string(input_path)?;
   let num = raw.parse::<usize>()?;

   let actual = num + 10;




  • Run commands and assert on their behavior
  • actual and expected Data for testing code
  • Initialize working directories and assert on how they’ve changed
  • Filter actual or expected Data
  • harnessharness
    Harness for discovering test inputs and asserting against snapshot files
  • Initialize working directories and assert on how they’ve changed
  • Easier access to common traits
  • Utilities to report test results to users


  • Check if a value is the same as an expected value
  • Declare an expected value for an assert from a file
  • Declare an expected value from within Rust source



Type Aliases§