Skip to content

Can't fail a test with my custom error message without the panic boilerplate #89398

Open
@imsnif

Description

@imsnif

Problem
Hi there. In certain cases when failing a test run with cargo, I need to display a custom error message.
There doesn't seem to be a way to do this without displaying the whole panic boilerplate. For various reasons I'm running with RUST_BACKTRACE=1, so this also displays the full backtrace and makes the custom error very hard to find (especially in CI).

Steps

  1. Write this test:
#[test]
fn foo () -> Result<(), String> {
    Err("aaa!!!".to_owned())
}
  1. Get this output:
running 1 test
test foo ... FAILED

failures:

---- foo stdout ----
Error: "aaa!!!"
thread 'foo' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `0`: the test returned a termination value with a non-zero status code (1) which indicates a failure', /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/test/src/lib.rs:193:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    foo
  1. I would really like to get this output instead:
running 1 test
test foo ... FAILED

failures:

---- foo stdout ----
aaa!!!

failures:
    foo

Output of cargo version:
cargo 1.54.0 (5ae8d74b3 2021-06-22)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-error-handlingArea: Error handlingA-libtestArea: `#[test]` / the `test` libraryC-bugCategory: This is a bug.T-testing-devexRelevant to the testing devex team (testing DX), which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions