C-RAD
C-RAD

Reputation: 1029

How to get Cargo Test to Pretty Print Failure Outputs

I have a Rust test that is comparing two structs of a type that implements PartialEq and Debug.

According to the help page for the cargo test output, it should be possible to "pretty print" the left and right arguments to each failed assertion:

$ cargo test_foo --lib -- --help

warning: `google2005` (lib test) generated 4 warnings
    Finished test [unoptimized + debuginfo] target(s) in 0.07s
     Running unittests (target/debug/deps/foo)
Usage: test_foo [OPTIONS] [FILTERS...]

Options:
        --include-ignored 
                        Run ignored and not ignored tests
        --ignored       Run only ignored tests
...
***** snipped *****
...
        --format pretty|terse|json|junit
                        Configure formatting of output:
                        pretty = Print verbose output;
                        terse = Display one character per test;
                        json = Output a json document;
                        junit = Output a JUnit document

...
***** snipped *****

I would expect this would print my structs with proper indentation, like when you use the {:#?} pretty print marker for Debug output. But it doesn't seem to have any effect:

$ cargo test test_foo --lib -- --format pretty
   Finished test [unoptimized + debuginfo] target(s) in 0.07s
     Running unittests (target/debug/deps/foo)

running 1 test
test parser::test::test_walk_scrapes_description ... FAILED

failures:

---- parser::test::test_walk_scrapes_description stdout ----
thread 'parser::test::test_walk_scrapes_description' panicked at 'assertion failed: `(left == right)`
  left: `Cat { name: "Fluffy", age: 8, breed: "Siamese", color: "Brown", weight: 12, chip_number: 10001001 }`,
 right: `Cat { name: "Fluffy", age: 8, breed: "Siamese", color: "Brown", weight: 13, chip_number: 10010001 }`', src/parser.rs:170:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    parser::test::test_walk_scrapes_description

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 13 filtered out; finished in 0.00s 

I thought it was possible to do achieve something like this:

  left: `Cat {
             name: "Fluffy",
             age: 8,
             breed: "Siamese",
             color: "Brown",
             weight: 12,
             chip_number: 10001001
 }`,
 right: `Cat {
             name: "Fluffy",
             age: 8,
             breed: "Siamese",
             color: "Brown",
             weight: 13,
             chip_number: 10010001
 }`',

I've tried both with rustc 1.59.0 (9d1b2106e 2022-02-23) and rustc 1.63.0-nightly (a6b8c6954 2022-06-03).

Upvotes: 2

Views: 2187

Answers (1)

Chayim Friedman
Chayim Friedman

Reputation: 71025

This is quite problematic since assert_eq!() itself prints the contents and cargo test gets already-formatted string.

It is also impossible to feature-gate the macro and under test pretty-print the variables, because std's code cannot be feature-gated (currently).

You can however write your macro to do that, or use a library like pretty-assertions.

Note, however, that there were RFCs to improve the display of the assert!() macros (it is not generally considered a breaking change), so it is possible that this will also be fixed in the future:

Upvotes: 4

Related Questions