#testing #macro #utility

assert_ok

A macro that asserts a Result is Ok

3 stable releases

1.0.2 Nov 17, 2022
1.0.1 Mar 17, 2022

#2702 in Rust patterns

Download history 133/week @ 2024-03-13 126/week @ 2024-03-20 84/week @ 2024-03-27 172/week @ 2024-04-03 67/week @ 2024-04-10 147/week @ 2024-04-17 313/week @ 2024-04-24 199/week @ 2024-05-01 308/week @ 2024-05-08 326/week @ 2024-05-15 282/week @ 2024-05-22 349/week @ 2024-05-29 278/week @ 2024-06-05 335/week @ 2024-06-12 179/week @ 2024-06-19 287/week @ 2024-06-26

1,135 downloads per month

Apache-2.0 OR MIT

3KB

assert_ok

crates.io docs.rs

This crate contains a macro assert_ok which takes an expression and if the expression evaluates to an Err, panics with a useful message. If in contrast the expression evaluates to Ok(v) then it returns the value v.

This is commonly useful in tests. Instead of

let z = foo(arg1, arg2).unwrap();

or

let z = foo(arg1, arg2).expect("foo failed");

use

let z = assert_ok!(foo(arg1, arg2));

It's easier to understand (IMO) and more importantly provides a much more useful error message in the case that it fails.

There is a similar macro in Tokio, however for libraries or applications that don't use Tokio, pulling it in for a single macro doesn't make sense.


lib.rs:

A macro that asserts that a Result is [Ok]

No runtime deps