Skip to content

Commit c73598f

Browse files
committed
Report span of test when should_panic test failed
Signed-off-by: xizheyin <[email protected]>
1 parent 6e83046 commit c73598f

5 files changed

+108
-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)
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//@ run-fail
2+
//@ check-run-results
3+
//@ compile-flags: --test
4+
//@ exec-env:RUST_BACKTRACE=0
5+
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
6+
//@ run-flags: --test-threads=1
7+
8+
#[test]
9+
#[should_panic]
10+
fn should_panic_with_any_message() {
11+
panic!("Panic!");
12+
}
13+
14+
#[test]
15+
#[should_panic = "message"]
16+
fn should_panic_with_message() {
17+
panic!("message");
18+
}
19+
20+
#[test]
21+
#[should_panic]
22+
fn should_panic_with_any_message_does_not_panic() {
23+
// DON'T PANIC
24+
}
25+
26+
#[test]
27+
#[should_panic = "message"]
28+
fn should_panic_with_message_does_not_panic() {
29+
// DON'T PANIC
30+
}
31+
32+
#[test]
33+
#[should_panic = "message"]
34+
fn should_panic_with_substring_panics_with_incorrect_string() {
35+
panic!("ZOMGWTFBBQ");
36+
}
37+
38+
#[test]
39+
#[should_panic = "message"]
40+
fn should_panic_with_substring_panics_with_non_string_value() {
41+
panic!(123); //~ WARNING panic message is not a string literal
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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:22: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:28: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:35:5:
19+
ZOMGWTFBBQ
20+
note: panic did not contain expected string
21+
panic message: `"ZOMGWTFBBQ"`,
22+
expected substring: `"message"`
23+
---- should_panic_with_substring_panics_with_non_string_value stdout ----
24+
25+
thread 'should_panic_with_substring_panics_with_non_string_value' panicked at $DIR/test-should-panic-failed-show-span.rs:41:5:
26+
Box<dyn Any>
27+
note: expected panic with string value,
28+
found non-string value: `TypeId(0x56ced5e4a15bd89050bb9674fa2df013)`
29+
expected substring: `"message"`
30+
31+
failures:
32+
should_panic_with_any_message_does_not_panic
33+
should_panic_with_message_does_not_panic
34+
should_panic_with_substring_panics_with_incorrect_string
35+
should_panic_with_substring_panics_with_non_string_value
36+
37+
test result: FAILED. 2 passed; 4 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
38+
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:41: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)