#macro #testing #utility

assert_ok

A macro that asserts a Result is Ok

2 stable releases

1.0.1 Mar 17, 2022

#719 in Rust patterns

Download history 15/week @ 2022-06-05 3/week @ 2022-06-12 7/week @ 2022-06-19 2/week @ 2022-06-26 9/week @ 2022-07-03 8/week @ 2022-07-10 22/week @ 2022-07-17 27/week @ 2022-07-24 10/week @ 2022-07-31 18/week @ 2022-08-07 49/week @ 2022-08-14 83/week @ 2022-08-21 45/week @ 2022-08-28 62/week @ 2022-09-04 51/week @ 2022-09-11 45/week @ 2022-09-18

204 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