pub struct SnapshotTest { /* private fields */ }Expand description
Snapshot testing for rich console output.
SnapshotTest compares console output against stored snapshots.
If the output differs, the test fails with a diff. When running
with UPDATE_SNAPSHOTS=1, snapshots are created or updated.
§Snapshot Storage
By default, snapshots are stored in tests/snapshots/ relative to
the crate’s Cargo.toml. Use with_snapshot_dir
to customize this location.
§File Naming
- Plain text snapshots:
{name}.txt - Raw snapshots (with ANSI):
{name}.raw.txt
Implementations§
Source§impl SnapshotTest
impl SnapshotTest
Sourcepub fn with_snapshot_dir(self, dir: impl AsRef<Path>) -> Self
pub fn with_snapshot_dir(self, dir: impl AsRef<Path>) -> Self
Sourcepub fn with_update_mode(self, update: bool) -> Self
pub fn with_update_mode(self, update: bool) -> Self
Forces update mode, regardless of environment variable.
Use this for programmatic snapshot updates.
Sourcepub fn assert_snapshot(&self, console: &TestConsole)
pub fn assert_snapshot(&self, console: &TestConsole)
Asserts that the console output matches the stored snapshot.
Compares the stripped output (without ANSI codes) against the snapshot.
§Panics
- If the snapshot doesn’t exist and
UPDATE_SNAPSHOTSis not set - If the output doesn’t match the snapshot
§Example
let console = TestConsole::new();
console.console().print("Hello, world!");
SnapshotTest::new("hello")
.assert_snapshot(&console);Sourcepub fn assert_snapshot_string(&self, actual: &str)
pub fn assert_snapshot_string(&self, actual: &str)
Asserts that a string matches the stored snapshot.
§Panics
- If the snapshot doesn’t exist and
UPDATE_SNAPSHOTSis not set - If the string doesn’t match the snapshot
Sourcepub fn assert_raw_snapshot(&self, console: &TestConsole)
pub fn assert_raw_snapshot(&self, console: &TestConsole)
Asserts that the raw console output (with ANSI codes) matches the snapshot.
This is useful for verifying that ANSI styling is applied correctly.
§Panics
- If the raw snapshot doesn’t exist and
UPDATE_SNAPSHOTSis not set - If the raw output doesn’t match the snapshot
Sourcepub fn snapshot_path(&self) -> PathBuf
pub fn snapshot_path(&self) -> PathBuf
Returns the path where the snapshot file would be stored.
Sourcepub fn snapshot_path_raw(&self) -> PathBuf
pub fn snapshot_path_raw(&self) -> PathBuf
Returns the path where the raw snapshot file would be stored.
Sourcepub fn snapshot_exists(&self) -> bool
pub fn snapshot_exists(&self) -> bool
Checks if a snapshot exists for this test.
Sourcepub fn raw_snapshot_exists(&self) -> bool
pub fn raw_snapshot_exists(&self) -> bool
Checks if a raw snapshot exists for this test.