Skip to content

tests with Result could have nicer output #52436

@steveklabnik

Description

@steveklabnik
Member

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.

Activity

scottmcm

scottmcm commented on Jul 17, 2018

@scottmcm
Member

Hmm, the trait is super-unstable, so maybe it's be worth adding a purpose-built method for this? Like

fn assert_success(self) {
    assert_eq!(self.report(), 0);
}

But then the Result impl could override it to show something better...

srijs

srijs commented on Jul 17, 2018

@srijs
Contributor

What if it printed something like this?

---- tests::it_works stdout ----
The test function returned a termination value that indicates a failure. The detailed report follows.
Error: "two plus two does not equal four"
thread 'tests::it_works' panicked at 'non-successful termination value', libtest\lib.rs:326:5

Or simply

---- tests::it_works stdout ----
Error: "two plus two does not equal four"
thread 'tests::it_works' panicked at 'test function returned termination value that indicates a failure', libtest\lib.rs:326:5

EDIT: Nevermind the first suggestion, that seems like it couldn't work because Termination::report sort of couples printing the message with returning a code.

added a commit that references this issue on Aug 11, 2018
6411aef
added a commit that references this issue on Aug 15, 2018
d8815cf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @steveklabnik@estebank@srijs@scottmcm

        Issue actions

          tests with Result could have nicer output · Issue #52436 · rust-lang/rust