Skip to content

Tests with #[should_panic(expected = *)] should print a failure message if no panic occurs #80861

Closed
@johanngan

Description

@johanngan

As of #60790, a #[should_panic] #[test] produces a helpful error message when it fails because there was no panic. However, a #[should_panic(expected = *)] #[test] still fails with no error message in this case.

For example, consider the following two tests:

#[test]
#[should_panic]
fn no_panic() {}

#[test]
#[should_panic(expected = "foo")]
fn no_panic_with_expected() {}

When run with cargo test, no_panic() fails with the message "note: test did not panic as expected", but no_panic_with_expected() still fails with no message:

test tests::no_panic ... FAILED
test tests::no_panic_with_expected ... FAILED

failures:

---- tests::no_panic stdout ----
note: test did not panic as expected

failures:
    tests::no_panic
    tests::no_panic_with_expected

Suggested Fix

I'm not totally confident here (I've only just started learning Rust), but I think this is an oversight with this match arm. The relevant pattern is currently (&ShouldPanic::Yes, Ok(())), which doesn't catch the ShouldPanic::YesWithMessage variant. I believe it should instead be something like (&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())).

Meta

rustc --version --verbose:

rustc 1.49.0 (e1884a8e3 2020-12-29)
binary: rustc
commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
commit-date: 2020-12-29
host: x86_64-apple-darwin
release: 1.49.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions