Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
johanngan opened this issue Jan 10, 2021 · 1 comment · Fixed by #80868
Closed
Labels
C-bug Category: This is a bug.

Comments

@johanngan
Copy link
Contributor

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
@johanngan johanngan added the C-bug Category: This is a bug. label Jan 10, 2021
@johanngan johanngan changed the title Tests with #[should_panic(expected = *)] should print a message on failure Tests with #[should_panic(expected = *)] should print a failure message if no panic occurs Jan 10, 2021
@johanngan
Copy link
Contributor Author

I'll put up a quick PR for this I guess; it seems simple enough.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jan 27, 2021
…ected, r=m-ou-se

Print failure message on all tests that should panic, but don't

Fixes rust-lang#80861. Tests with the `#[should_panic]` attribute should always print a failure message if no panic occurs, regardless of whether or not an `expected` panic message is specified.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jan 27, 2021
…ected, r=m-ou-se

Print failure message on all tests that should panic, but don't

Fixes rust-lang#80861. Tests with the `#[should_panic]` attribute should always print a failure message if no panic occurs, regardless of whether or not an `expected` panic message is specified.
@bors bors closed this as completed in 9822663 Jan 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant