test-try

Crates.iotest-try
lib.rstest-try
version0.1.0
created_at2025-02-19 14:08:30.069838+00
updated_at2025-02-19 14:08:30.069838+00
descriptionAn alternative to Rust's `#[test]` macro for writing unit tests.
homepagehttps://github.com/plul/test-try
repositoryhttps://github.com/plul/test-try
max_upload_size
id1561434
size9,894
Asger Juul Brunshøj (plul)

documentation

https://docs.rs/test-try

README

#[test_try]

An alternative to Rust's #[test] macro for writing unit tests.

Usage

[dependencies]
test-try = "0.1"

Introduction

Rust unit test typically handle errors by unwrapping or by returning a result with a boxed error:

/// Test using `.unwrap()` for error handling.
#[test]
fn regular_rust_test_with_unwrap() {
    Err::<(), _>(OuterError(InnerError)).unwrap();
}

/// Test returning `Result<(), Box<dyn std::error::Error>>` for error handling.
#[test]
fn regular_rust_test_returning_result() -> Result<(), Box<dyn std::error::Error>> {
    Err(OuterError(InnerError))?;
    Ok(())
}

#[derive(Debug, Display, Error, From)]
#[display("Outer error")]
struct OuterError(InnerError);

#[derive(Debug, Display, Error)]
#[display("Inner error that actually explains what went wrong")]
struct InnerError;

where the cargo test output may look like this

---- regular_rust_test_with_unwrap stdout ----
thread 'regular_rust_test_with_unwrap' panicked at crates/test-try/examples/basic.rs:32:42:
called `Result::unwrap()` on an `Err` value: OuterError(InnerError)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

or this

---- regular_rust_test_returning_result stdout ----
Error: OuterError(InnerError)

Critically, none of them include the source error, and writing .unwrap(), -> Result<(), Box<dyn std::error::Error>> and Ok(()) is tedious.

#[test_try] is an alternative test macro:

#[test_try]
fn test_using_test_try() {
    Err(OuterError(InnerError))?;
}

with the following features:

  • Use ? to return errors without specifying a return type or ending the test with Ok(()).
  • On error, the test prints out a report including the chain of source errors.

The cargo test output includes the full chain of source errors, in this case:

---- test_using_test_try stdout ----
Error: Outer error

Caused by:
      Inner error that actually explains what went wrong
Commit count: 3

cargo fmt