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.
Auto Trait Implementations§
impl Freeze for SnapshotTest
impl RefUnwindSafe for SnapshotTest
impl Send for SnapshotTest
impl Sync for SnapshotTest
impl Unpin for SnapshotTest
impl UnsafeUnpin for SnapshotTest
impl UnwindSafe for SnapshotTest
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more