Crates.io | test-try |
lib.rs | test-try |
version | 0.1.0 |
created_at | 2025-02-19 14:08:30.069838+00 |
updated_at | 2025-02-19 14:08:30.069838+00 |
description | An alternative to Rust's `#[test]` macro for writing unit tests. |
homepage | https://github.com/plul/test-try |
repository | https://github.com/plul/test-try |
max_upload_size | |
id | 1561434 |
size | 9,894 |
#[test_try]
An alternative to Rust's #[test]
macro for writing unit tests.
[dependencies]
test-try = "0.1"
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:
?
to return errors without specifying a return type or ending the test with Ok(())
.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