Skip to content

Commit 3a7cf39

Browse files
committed
Report span of test when should_panic test failed
Signed-off-by: xizheyin <[email protected]>
1 parent 8fb32ab commit 3a7cf39

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

library/test/src/test_result.rs

Lines changed: 7 additions & 1 deletion
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 fn_location = if !desc.source_file.is_empty() {
49+
&format!(" at {}:{}:{}", desc.source_file, desc.start_line, desc.start_col)
50+
} else {
51+
""
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)) => {
@@ -72,7 +78,7 @@ pub(crate) fn calc_result<'a>(
7278
}
7379
}
7480
(&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {
75-
TestResult::TrFailedMsg("test did not panic as expected".to_string())
81+
TestResult::TrFailedMsg(format!("test did not panic as expected{}", fn_location))
7682
}
7783
_ => TestResult::TrFailed,
7884
};

tests/rustdoc-ui/doctest/failed-doctest-should-panic.stdout

Lines changed: 1 addition & 1 deletion
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)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ run-fail
2+
//@ check-run-results
3+
//@ compile-flags: --test
4+
//@ exec-env: RUST_BACKTRACE=0
5+
//@ run-flags: --test-threads=1
6+
7+
#[test]
8+
#[should_panic]
9+
fn should_panic_with_any_message_does_not_panic() {
10+
// DON'T PANIC
11+
}
12+
13+
#[test]
14+
#[should_panic = "message"]
15+
fn should_panic_with_message_does_not_panic() {
16+
// DON'T PANIC
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
running 2 tests
3+
test should_panic_with_any_message_does_not_panic - should panic ... FAILED
4+
test should_panic_with_message_does_not_panic - should panic ... FAILED
5+
6+
failures:
7+
8+
---- should_panic_with_any_message_does_not_panic stdout ----
9+
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:9:4
10+
---- should_panic_with_message_does_not_panic stdout ----
11+
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:15:4
12+
13+
failures:
14+
should_panic_with_any_message_does_not_panic
15+
should_panic_with_message_does_not_panic
16+
17+
test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
18+

0 commit comments

Comments
 (0)