Suggestions for better test output?


#1

As a newcomer to Rust, I’m happy to see a built-in test runner (cargo test), but I’m used to better assertion output in other languages.

Consider this assertion:

debug_assert_eq!(vec![1, 2, 3], vec![1, 2, 4]);

which produces this output:

thread ‘tests::derp::test_vector_match’ panicked at ‘assertion failed: `(left == right)` (left: `[1, 2, 3]`, right: `[1, 2, 4]`)’, src/lib.rs:10

That’s not great. A few specific problems:

  1. The part I care about is far to the right.
  2. If the expected and actual values were on separate lines they could be aligned horizontally for better visual diffing.
  3. The output is all one color. Cargo uses green and red above for passing/failing tests, so it seems like some color could be used here too, to make the failed assertion stand out.

I’d like to see something like this:

thread ‘tests::derp::test_vector_match’ panicked at ‘assertion failed: (left == right)
expected: [1, 2, 3]
actual:   [1, 2, 4]
in src/lib.rs:10

I’d be happy to use a library for this, the solution doesn’t have to be in Rust itself.

PS: I would have liked to ask this Q on stackoverflow, but it could be construed as a request for library suggestions, which is not allowed there.

PPS: For an unfair comparison to RSpec, see this blog post: https://medium.com/@jaredowenbeck/the-output-of-unit-tests-in-rust-could-be-better-6265e07fc0a7#.em9zrfprs


#2

The problem is that Rust does not support custom test frameworks yet. There is an issue about it (https://github.com/rust-lang/rfcs/issues/816).


#3

Thanks Aleksey. I don’t know if a “custom test framework” is what I need, I just want slightly better output from assert_eq!.


#4

I cant fined it now but there has been some discussion of this in the past. The decision was to leave as is for now, but make it available when custom test runners show up. I think the biggest reason to keep the current behavior is to prevent assertion messages for separate threads from interweaving.


#5

I think the biggest reason to keep the current behavior is to prevent
assertion messages for separate threads from interweaving.

Well that’s important! Thanks Jacob.


#6

So debug_assert_eq! itself is simply a macro that calls panic! if the values aren’t equal. You could rewrite this to include newlines in the panic! message. The code for debug_assert_eq! - https://doc.rust-lang.org/src/core/up/src/libcore/macros.rs.html#98-120.