Auto merge of #138603 - xizheyin:issue-137405, r=chenyukang

Report line number of test when should_panic test failed

Closes #137405

---

try-job: x86_64-gnu-llvm-19-3
try-job: test-various
This commit is contained in:
bors 2025-04-14 13:33:44 +00:00
commit 07d3fd1d9b
5 changed files with 98 additions and 2 deletions

View file

@ -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.

View file

@ -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)

View file

@ -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);
}

View file

@ -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>

View file

@ -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