Skip to content

Commit e57ab34

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

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

library/test/src/test_result.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ pub(crate) fn calc_result<'a>(
7272
}
7373
}
7474
(&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {
75-
TestResult::TrFailedMsg("test did not panic as expected".to_string())
75+
let span_str = format!("{}:{}:{}", desc.source_file, desc.start_line, desc.start_col);
76+
TestResult::TrFailedMsg(format!("test did not panic as expected at {}", span_str))
7677
}
7778
_ => TestResult::TrFailed,
7879
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@ run-fail
2+
//@ check-run-results
3+
//@ compile-flags: --test
4+
5+
6+
#[test]
7+
#[should_panic]
8+
fn do_panic1() {
9+
panic!("Panic!");
10+
}
11+
12+
#[test]
13+
#[should_panic = "SubString"]
14+
fn do_panic2() {
15+
// DON'T PANIC
16+
panic!("SubString");
17+
}
18+
19+
#[test]
20+
#[should_panic]
21+
fn dont_panic1() {
22+
// DON'T PANIC
23+
}
24+
25+
26+
#[test]
27+
#[should_panic = "SubString"]
28+
fn dont_panic2() {
29+
// DON'T PANIC
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
running 4 tests
3+
test dont_panic1 - should panic ... FAILED
4+
test do_panic2 - should panic ... ok
5+
test do_panic1 - should panic ... ok
6+
test dont_panic2 - should panic ... FAILED
7+
8+
failures:
9+
10+
---- dont_panic1 stdout ----
11+
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:21:4
12+
---- dont_panic2 stdout ----
13+
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:28:4
14+
15+
failures:
16+
dont_panic1
17+
dont_panic2
18+
19+
test result: FAILED. 2 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
20+

0 commit comments

Comments
 (0)