Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a164290

Browse files
committedMar 18, 2025
Report span of test when should_panic test failed
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent 66678e6 commit a164290

6 files changed

+124
-10
lines changed
 

‎library/test/src/test_result.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ pub(crate) fn calc_result<'a>(
4545
time_opts: Option<&time::TestTimeOptions>,
4646
exec_time: Option<&time::TestExecTime>,
4747
) -> TestResult {
48+
let at_location = if desc.source_file != "" {
49+
format!(" at {}:{}:{}", desc.source_file, desc.start_line, desc.start_col)
50+
} else {
51+
"".to_string()
52+
};
53+
4854
let result = match (&desc.should_panic, task_result) {
4955
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
5056
(&ShouldPanic::YesWithMessage(msg), Err(err)) => {
@@ -53,26 +59,32 @@ pub(crate) fn calc_result<'a>(
5359
.map(|e| &**e)
5460
.or_else(|| err.downcast_ref::<&'static str>().copied());
5561

62+
let shoud_panic_location = if desc.source_file != "" {
63+
format!("should panic with message{}\n ", at_location)
64+
} else {
65+
"".to_string()
66+
};
67+
5668
if maybe_panic_str.map(|e| e.contains(msg)).unwrap_or(false) {
5769
TestResult::TrOk
5870
} else if let Some(panic_str) = maybe_panic_str {
5971
TestResult::TrFailedMsg(format!(
60-
r#"panic did not contain expected string
72+
r#"{shoud_panic_location}panic did not contain expected string
6173
panic message: `{panic_str:?}`,
62-
expected substring: `{msg:?}`"#
74+
expected substring: `{msg:?}`"#
6375
))
6476
} else {
6577
TestResult::TrFailedMsg(format!(
66-
r#"expected panic with string value,
67-
found non-string value: `{:?}`
68-
expected substring: `{:?}`"#,
78+
r#"{shoud_panic_location}expected panic with string value,
79+
found non-string value: `{:?}`
80+
expected substring: `{:?}`"#,
6981
(*err).type_id(),
7082
msg
7183
))
7284
}
7385
}
7486
(&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {
75-
TestResult::TrFailedMsg("test did not panic as expected".to_string())
87+
TestResult::TrFailedMsg(format!("test did not panic as expected{}", at_location))
7688
}
7789
_ => TestResult::TrFailed,
7890
};

‎library/test/src/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn test_should_panic_bad_message() {
201201
let expected = "foobar";
202202
let failed_msg = r#"panic did not contain expected string
203203
panic message: `"an error message"`,
204-
expected substring: `"foobar"`"#;
204+
expected substring: `"foobar"`"#;
205205
let desc = TestDescAndFn {
206206
desc: TestDesc {
207207
name: StaticTestName("whatever"),
@@ -237,8 +237,8 @@ fn test_should_panic_non_string_message_type() {
237237
let expected = "foobar";
238238
let failed_msg = format!(
239239
r#"expected panic with string value,
240-
found non-string value: `{:?}`
241-
expected substring: `"foobar"`"#,
240+
found non-string value: `{:?}`
241+
expected substring: `"foobar"`"#,
242242
TypeId::of::<i32>()
243243
);
244244
let desc = TestDescAndFn {

‎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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//@ run-fail
2+
//@ check-run-results
3+
//@ compile-flags: --test
4+
//@ run-flags: --test-threads=1
5+
6+
#[test]
7+
#[should_panic]
8+
fn should_panic_with_any_message() {
9+
panic!("Panic!");
10+
}
11+
12+
#[test]
13+
#[should_panic = "message"]
14+
fn should_panic_with_message() {
15+
panic!("message");
16+
}
17+
18+
#[test]
19+
#[should_panic]
20+
fn should_panic_with_any_message_does_not_panic() {
21+
// DON'T PANIC
22+
}
23+
24+
25+
#[test]
26+
#[should_panic = "message"]
27+
fn should_panic_with_message_does_not_panic() {
28+
// DON'T PANIC
29+
}
30+
31+
#[test]
32+
#[should_panic = "message"]
33+
fn should_panic_with_substring_panics_with_incorrect_string() {
34+
panic!("ZOMGWTFBBQ");
35+
}
36+
37+
#[test]
38+
#[should_panic = "message"]
39+
fn should_panic_with_substring_panics_with_non_string_value() {
40+
panic!(123); //~ WARNING panic message is not a string literal
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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:20: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:27:4
16+
---- should_panic_with_substring_panics_with_incorrect_string stdout ----
17+
18+
thread 'should_panic_with_substring_panics_with_incorrect_string' panicked at $DIR/test-should-panic-failed-show-span.rs:34:5:
19+
ZOMGWTFBBQ
20+
note: should panic with message at $DIR/test-should-panic-failed-show-span.rs:33:4
21+
panic did not contain expected string
22+
panic message: `"ZOMGWTFBBQ"`,
23+
expected substring: `"message"`
24+
---- should_panic_with_substring_panics_with_non_string_value stdout ----
25+
26+
thread 'should_panic_with_substring_panics_with_non_string_value' panicked at $DIR/test-should-panic-failed-show-span.rs:40:5:
27+
Box<dyn Any>
28+
note: should panic with message at $DIR/test-should-panic-failed-show-span.rs:39:4
29+
expected panic with string value,
30+
found non-string value: `TypeId(0x56ced5e4a15bd89050bb9674fa2df013)`
31+
expected substring: `"message"`
32+
33+
failures:
34+
should_panic_with_any_message_does_not_panic
35+
should_panic_with_message_does_not_panic
36+
should_panic_with_substring_panics_with_incorrect_string
37+
should_panic_with_substring_panics_with_non_string_value
38+
39+
test result: FAILED. 2 passed; 4 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: panic message is not a string literal
2+
--> $DIR/test-should-panic-failed-show-span.rs:40:12
3+
|
4+
LL | panic!(123);
5+
| ^^^
6+
|
7+
= note: this usage of `panic!()` is deprecated; it will be a hard error in Rust 2021
8+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/panic-macro-consistency.html>
9+
= note: `#[warn(non_fmt_panics)]` on by default
10+
help: add a "{}" format string to `Display` the message
11+
|
12+
LL | panic!("{}", 123);
13+
| +++++
14+
help: or use std::panic::panic_any instead
15+
|
16+
LL - panic!(123);
17+
LL + std::panic::panic_any(123);
18+
|
19+
20+
warning: 1 warning emitted
21+

0 commit comments

Comments
 (0)
Failed to load comments.