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
- See also scrut
- 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 liketrycmdin one off cases or you need to customizetrycmds 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:
assert_data_eq!for quick and dirty snapshotting
Testing Commands:
cmd::Command: Process spawning for testing of non-interactive commandscmd::OutputAssert: Assert the state of aCommand’sOutput.
Testing Filesystem Interactions:
dir::DirRoot: Working directory for testsAssert: 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
detect-encoding— Smarter binary file detectiondir— Snapshotting of directoriespath— Deprecated since 0.5.11, replaced withdircmd— Snapshotting of commandsexamples— Building of examples for snapshottingregex— Regex text substitutionsjson— Snapshotting of jsonterm-svg— Snapshotting of term stylingstructured-data— Snapshotting of structured datadebug— Extra debugging information
Default Feature Flags
diff(enabled by default) — Fancy diffs on failurecolor(enabled by default) — Colored output supportcolor-auto(enabled by default) — Auto-detect whether to use colors
§Examples
snapbox::assert_data_eq!("Hello many people!", "Hello [..] people!");let actual = "...";
snapbox::Assert::new()
.action_env("SNAPSHOTS")
.eq(actual, snapbox::file!["help_output_is_clean.txt"]);Re-exports§
pub use assert::Assert;pub use data::Data;pub use data::IntoData;pub use data::IntoJson;jsonpub use data::ToDebug;pub use filter::RedactedValue;pub use filter::Redactions;
Modules§
- assert
- cmd
- Run commands and assert on their behavior
- data
actualandexpectedDatafor testing code- dir
- Initialize working directories and assert on how they’ve changed
- filter
- Filter
actualorexpectedData - 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
- file
- Declare an expected value for an assert from a file
- str
- Declare an expected value from within Rust source
Functions§
- assert_
subset_ eq dir - Check if a path matches the content of another path, recursively
- assert_
subset_ matches dir - Check if a path matches the pattern of another path, recursively