Skip to content

Commit 07d3fd1

Browse files
committed
Auto merge of rust-lang#138603 - xizheyin:issue-137405, r=chenyukang
Report line number of test when should_panic test failed Closes rust-lang#137405 --- try-job: x86_64-gnu-llvm-19-3 try-job: test-various
2 parents c580c49 + dc3a586 commit 07d3fd1

5 files changed

+98
-2
lines changed

Diff for: library/test/src/test_result.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ pub(crate) fn calc_result(
7777

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

8388
// The test should not have panicked, but did panic.

Diff for: tests/rustdoc-ui/doctest/failed-doctest-should-panic.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test $DIR/failed-doctest-should-panic.rs - Foo (line 10) - should panic ... FAIL
55
failures:
66

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

1010
failures:
1111
$DIR/failed-doctest-should-panic.rs - Foo (line 10)
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//@ compile-flags: --test
2+
//@ run-flags: --test-threads=1 --nocapture
3+
//@ run-fail
4+
//@ check-run-results
5+
//@ exec-env:RUST_BACKTRACE=0
6+
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
7+
//@ normalize-stdout: "TypeId\(0x[0-9a-f]+\)" -> "TypeId($$HEX)"
8+
//@ needs-threads
9+
//@ needs-unwind (panic)
10+
11+
#[test]
12+
#[should_panic]
13+
fn should_panic_with_any_message() {
14+
panic!("Panic!");
15+
}
16+
17+
#[test]
18+
#[should_panic = "message"]
19+
fn should_panic_with_message() {
20+
panic!("message");
21+
}
22+
23+
#[test]
24+
#[should_panic]
25+
fn should_panic_with_any_message_does_not_panic() {
26+
// DON'T PANIC
27+
}
28+
29+
#[test]
30+
#[should_panic = "message"]
31+
fn should_panic_with_message_does_not_panic() {
32+
// DON'T PANIC
33+
}
34+
35+
#[test]
36+
#[should_panic = "message"]
37+
fn should_panic_with_substring_panics_with_incorrect_string() {
38+
panic!("ZOMGWTFBBQ");
39+
}
40+
41+
#[test]
42+
#[should_panic = "message"]
43+
#[expect(non_fmt_panics)]
44+
fn should_panic_with_substring_panics_with_non_string_value() {
45+
panic!(123);
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
thread 'should_panic_with_any_message' panicked at $DIR/test-should-panic-failed-show-span.rs:14:5:
3+
Panic!
4+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5+
6+
thread 'should_panic_with_message' panicked at $DIR/test-should-panic-failed-show-span.rs:20:5:
7+
message
8+
9+
thread 'should_panic_with_substring_panics_with_incorrect_string' panicked at $DIR/test-should-panic-failed-show-span.rs:38:5:
10+
ZOMGWTFBBQ
11+
12+
thread 'should_panic_with_substring_panics_with_non_string_value' panicked at $DIR/test-should-panic-failed-show-span.rs:45:5:
13+
Box<dyn Any>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
running 6 tests
3+
test should_panic_with_any_message - should panic ... ok
4+
test should_panic_with_any_message_does_not_panic - should panic ... FAILED
5+
test should_panic_with_message - should panic ... ok
6+
test should_panic_with_message_does_not_panic - should panic ... FAILED
7+
test should_panic_with_substring_panics_with_incorrect_string - should panic ... FAILED
8+
test should_panic_with_substring_panics_with_non_string_value - should panic ... FAILED
9+
10+
failures:
11+
12+
---- should_panic_with_any_message_does_not_panic stdout ----
13+
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:25:4
14+
---- should_panic_with_message_does_not_panic stdout ----
15+
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:31:4
16+
---- should_panic_with_substring_panics_with_incorrect_string stdout ----
17+
note: panic did not contain expected string
18+
panic message: `"ZOMGWTFBBQ"`,
19+
expected substring: `"message"`
20+
---- should_panic_with_substring_panics_with_non_string_value stdout ----
21+
note: expected panic with string value,
22+
found non-string value: `TypeId($HEX)`
23+
expected substring: `"message"`
24+
25+
failures:
26+
should_panic_with_any_message_does_not_panic
27+
should_panic_with_message_does_not_panic
28+
should_panic_with_substring_panics_with_incorrect_string
29+
should_panic_with_substring_panics_with_non_string_value
30+
31+
test result: FAILED. 2 passed; 4 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
32+

0 commit comments

Comments
 (0)