Skip to content

tests with Result could have nicer output #52436

Closed
@steveklabnik

Description

@steveklabnik

Consider this test:

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() -> Result<(), String> {
        if 2 + 2 != 4 {
            Ok(())
        } else {
            Err(String::from("two plus two does not equal four"))
        }
    }
}

(note the !) when this fails, you get

> cargo test
   Compiling termination v0.1.0 (file:///C:/Users/steve/tmp/termination)
    Finished dev [unoptimized + debuginfo] target(s) in 1.44s
     Running target\debug\deps\termination-4b340b40460d3098.exe

running 1 test
test tests::it_works ... FAILED

failures:

---- tests::it_works stdout ----
Error: "two plus two does not equal four"
thread 'tests::it_works' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `0`', libtest\lib.rs:326:5

This is because it triggers this assert.

can we make this better somehow? 1 != 0 is not super helpful at learning why this test failed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-libtestArea: `#[test]` / the `test` library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions