Skip to main content


What’s the most idiomatic way of testing that a #Rust function returns the correct error enum variant? I’ve been using this pattern as it doesn’t require the error enum to be PartialEq:

Edit: Thank you everyone for suggesting the matches! macro (https://doc.rust-lang.org/std/macro.matches.html), which I somehow wasn’t aware of 🤦🏻‍♀️

Rust code:

#[test]
fn test_something() {
    match do_something(0) {
        Err(Error::SomethingBad(_)) => (),
        _ => panic!("Expected Error::SomethingBad"),
    }
}
#rust
This entry was edited (9 months ago)

There is also an assert_matches! macro, but that's available on nightly only for now. Really looking forward to the release that brings this to stable.

https://doc.rust-lang.org/std/assert_matches/macro.assert_matches.html