Skip to content

Report line number of test when should_panic test failed #138603

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

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion library/test/src/test_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ pub(crate) fn calc_result(

// The test should have panicked, but didn't panic.
(ShouldPanic::Yes, None) | (ShouldPanic::YesWithMessage(_), None) => {
TestResult::TrFailedMsg("test did not panic as expected".to_string())
let fn_location = if !desc.source_file.is_empty() {
&format!(" at {}:{}:{}", desc.source_file, desc.start_line, desc.start_col)
} else {
""
};
TestResult::TrFailedMsg(format!("test did not panic as expected{}", fn_location))
}

// The test should not have panicked, but did panic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test $DIR/failed-doctest-should-panic.rs - Foo (line 10) - should panic ... FAIL
failures:

---- $DIR/failed-doctest-should-panic.rs - Foo (line 10) stdout ----
note: test did not panic as expected
note: test did not panic as expected at $DIR/failed-doctest-should-panic.rs:10:0

failures:
$DIR/failed-doctest-should-panic.rs - Foo (line 10)
Expand Down
46 changes: 46 additions & 0 deletions tests/ui/test-attrs/test-should-panic-failed-show-span.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//@ compile-flags: --test
//@ run-flags: --test-threads=1 --nocapture
//@ run-fail
//@ check-run-results
//@ exec-env:RUST_BACKTRACE=0
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
//@ normalize-stdout: "TypeId\(0x[0-9a-f]+\)" -> "TypeId($$HEX)"
//@ needs-threads
//@ needs-unwind (panic)

#[test]
#[should_panic]
fn should_panic_with_any_message() {
panic!("Panic!");
}

#[test]
#[should_panic = "message"]
fn should_panic_with_message() {
panic!("message");
}

#[test]
#[should_panic]
fn should_panic_with_any_message_does_not_panic() {
// DON'T PANIC
}

#[test]
#[should_panic = "message"]
fn should_panic_with_message_does_not_panic() {
// DON'T PANIC
}

#[test]
#[should_panic = "message"]
fn should_panic_with_substring_panics_with_incorrect_string() {
panic!("ZOMGWTFBBQ");
}

#[test]
#[should_panic = "message"]
#[expect(non_fmt_panics)]
fn should_panic_with_substring_panics_with_non_string_value() {
panic!(123);
}
13 changes: 13 additions & 0 deletions tests/ui/test-attrs/test-should-panic-failed-show-span.run.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

thread 'should_panic_with_any_message' panicked at $DIR/test-should-panic-failed-show-span.rs:14:5:
Panic!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'should_panic_with_message' panicked at $DIR/test-should-panic-failed-show-span.rs:20:5:
message

thread 'should_panic_with_substring_panics_with_incorrect_string' panicked at $DIR/test-should-panic-failed-show-span.rs:38:5:
ZOMGWTFBBQ

thread 'should_panic_with_substring_panics_with_non_string_value' panicked at $DIR/test-should-panic-failed-show-span.rs:45:5:
Box<dyn Any>
32 changes: 32 additions & 0 deletions tests/ui/test-attrs/test-should-panic-failed-show-span.run.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

running 6 tests
test should_panic_with_any_message - should panic ... ok
test should_panic_with_any_message_does_not_panic - should panic ... FAILED
test should_panic_with_message - should panic ... ok
test should_panic_with_message_does_not_panic - should panic ... FAILED
test should_panic_with_substring_panics_with_incorrect_string - should panic ... FAILED
test should_panic_with_substring_panics_with_non_string_value - should panic ... FAILED

failures:

---- should_panic_with_any_message_does_not_panic stdout ----
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:25:4
---- should_panic_with_message_does_not_panic stdout ----
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:31:4
---- should_panic_with_substring_panics_with_incorrect_string stdout ----
note: panic did not contain expected string
panic message: `"ZOMGWTFBBQ"`,
expected substring: `"message"`
---- should_panic_with_substring_panics_with_non_string_value stdout ----
note: expected panic with string value,
found non-string value: `TypeId($HEX)`
expected substring: `"message"`

failures:
should_panic_with_any_message_does_not_panic
should_panic_with_message_does_not_panic
should_panic_with_substring_panics_with_incorrect_string
should_panic_with_substring_panics_with_non_string_value

test result: FAILED. 2 passed; 4 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME

Loading