DEV Community

Cover image for [Rust Guide] 11.5. Using Result<T, E> in Tests
SomeB1oody
SomeB1oody

Posted on

[Rust Guide] 11.5. Using Result<T, E> in Tests

If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.

11.5.1 Test Functions That Return the Result Enum

So far, the reason tests have failed has always been a panic!, but that is not the only way a test can fail.

Tests that use the Result enum are also easy to write. You only need to accept the value returned by the code under test. If it matches expectations, return the Ok variant; otherwise return the Err variant. Since enum variants can carry data, you can also attach error information to Err to help with debugging.

If it is Ok, the test passes; otherwise it fails.

For example:

    #[test]
    fn it_works() -> Result<(), String> {
        let result = add(2, 2);

        if result == 4 {
            Ok(())
        } else {
            Err(String::from("two plus two does not equal four"))
        }
    }
Enter fullscreen mode Exit fullscreen mode

The it_works function has the return type Result<(), String>. When the test passes, it returns Ok(()); when the test fails, it returns Err containing a String with an error message.

This test will definitely pass.

One thing to keep in mind when using Result for tests: do not use the should_panic attribute on tests written with Result<T, E> (as discussed in the previous article), because tests written with Result<T, E> return Err instead of panicking directly.

Top comments (0)