Skip to main content

SnapshotTest

Struct SnapshotTest 

Source
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

Source

pub fn new(name: &str) -> Self

Creates a new snapshot test with the given name.

The name is used as the filename for the snapshot (with .txt extension). Snapshots are stored in tests/snapshots/ by default.

§Example
let snap = SnapshotTest::new("error_display");
// Snapshot will be at tests/snapshots/error_display.txt
Source

pub fn with_snapshot_dir(self, dir: impl AsRef<Path>) -> Self

Sets a custom snapshot directory.

§Example
let snap = SnapshotTest::new("test")
    .with_snapshot_dir("/tmp/my_snapshots");
Source

pub fn with_update_mode(self, update: bool) -> Self

Forces update mode, regardless of environment variable.

Use this for programmatic snapshot updates.

Source

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_SNAPSHOTS is 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);
Source

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_SNAPSHOTS is not set
  • If the string doesn’t match the snapshot
Source

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_SNAPSHOTS is not set
  • If the raw output doesn’t match the snapshot
Source

pub fn snapshot_path(&self) -> PathBuf

Returns the path where the snapshot file would be stored.

Source

pub fn snapshot_path_raw(&self) -> PathBuf

Returns the path where the raw snapshot file would be stored.

Source

pub fn snapshot_exists(&self) -> bool

Checks if a snapshot exists for this test.

Source

pub fn raw_snapshot_exists(&self) -> bool

Checks if a raw snapshot exists for this test.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more