Skip to main content

Crate snapbox

Crate snapbox 

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
  • tryfn: For running a lot of simple input/output tests
  • 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:

  • dir::DirRoot: 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.

§Examples

assert_data_eq!

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

Assert

let actual = "...";
snapbox::Assert::new()
    .action_env("SNAPSHOTS")
    .eq(actual, snapbox::file!["help_output_is_clean.txt"]);

Modules§

assert
cmd
Run commands and assert on their behavior
data
actual and expected Data for testing code
dir
Initialize working directories and assert on how they’ve changed
filter
Filter actual or expected Data
prelude
Easier access to common traits
report
Utilities to report test results to users
utils

Macros§

assert_data_eq
Check if a value is the same as an expected value
debug
Feature-flag controlled additional test debug information
file
Declare an expected value for an assert from a file
str
Declare an expected value from within Rust source

Structs§

Assert
Snapshot assertion against a file’s contents
Data
Test fixture, actual output, or expected result
RedactedValue
Redactions
Replace data with placeholders

Traits§

IntoData
Convert to Data with modifiers for expected data
IntoJson
Capture the serde representation of a value
ToDebug
Capture the pretty debug representation of a value

Functions§

assert_subset_eq
Check if a path matches the content of another path, recursively
assert_subset_matches
Check if a path matches the pattern of another path, recursively