#expect #macro #assert #test-macro #result

expecting

Rust macros that test conditions without panicking

8 releases (4 breaking)

0.5.0 Apr 9, 2024
0.4.0 Jan 14, 2023
0.3.0 Jan 11, 2023
0.2.0 Jan 10, 2023
0.1.1 Dec 18, 2022

#153 in Testing

Download history 12/week @ 2024-02-20 17/week @ 2024-02-27 1/week @ 2024-03-05 4/week @ 2024-03-12 2/week @ 2024-03-26 10/week @ 2024-04-02 154/week @ 2024-04-09

166 downloads per month
Used in muffin

MIT license

33KB
385 lines

Expecting

Crates.io Documentation Crates.io

Expecting provides macros for testing conditions without panicking. The expect_* family of macros cause an early return of anyhow::Error if the expected condition is not met.

Macro Description
expect!(condition) Expects condition to be true
expect_eq!(a, b) Expects a == b
expect_ne!(a, b) Expects a != b
expect_some(option) Expects option to be Some(x) and returns x
expect_none(option) Expects option to be None
expect_ok(result) Expects result to be Ok(x) and returns x
expect_err(result) Expects result to be Err(e) and returns e
expect_contains(container, element) Expects container (e.g., Vec) to contain element
expect_empty(container) Expects container to have no elements.
expect_contains(string, substr) Expects string to contain substr
expect_empty(string) Expects string to have a length of zero.
expect_some_eq(some_a, a) Expects a to be Some and some_a == Some(a)

This crate is especially helpful in async integration tests that involve provisioning and tearing down resources; rather than struggle to catch panics, you can simply use expect_* instead of assert_* to return Result.

Examples

use expecting::expect_eq;

#[test]
fn passing_test() -> Result<()> {
    expect_eq!(1, 1);
    Ok(())
}

#[test]
fn failing_test() -> Result<()> {
    expect_eq!(1, 2);  // returns early
    Ok(())  // won't be reached
}

The error message for a failed test includes the line number and detailed description of what went wrong.

expect_eq error

expect_contains error

See the docs for usage examples and more info.

Dependencies

~125KB