Report span of test when should_panic test failed
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
This commit is contained in:
parent
6e83046233
commit
c73598f0fb
5 changed files with 108 additions and 2 deletions
|
@ -77,7 +77,12 @@ pub(crate) fn calc_result(
|
|||
|
||||
// The test should have panicked, but didn't panic.
|
||||
(ShouldPanic::Yes, None) | (ShouldPanic::YesWithMessage(_), None) => {
|
||||
TestResult::TrFailedMsg("test did not panic as expected".to_string())
|
||||
let fn_location = if !desc.source_file.is_empty() {
|
||||
&format!(" at {}:{}:{}", desc.source_file, desc.start_line, desc.start_col)
|
||||
} else {
|
||||
""
|
||||
};
|
||||
TestResult::TrFailedMsg(format!("test did not panic as expected{}", fn_location))
|
||||
}
|
||||
|
||||
// The test should not have panicked, but did panic.
|
||||
|
|
|
@ -5,7 +5,7 @@ test $DIR/failed-doctest-should-panic.rs - Foo (line 10) - should panic ... FAIL
|
|||
failures:
|
||||
|
||||
---- $DIR/failed-doctest-should-panic.rs - Foo (line 10) stdout ----
|
||||
note: test did not panic as expected
|
||||
note: test did not panic as expected at $DIR/failed-doctest-should-panic.rs:10:0
|
||||
|
||||
failures:
|
||||
$DIR/failed-doctest-should-panic.rs - Foo (line 10)
|
||||
|
|
42
tests/ui/test-attrs/test-should-panic-failed-show-span.rs
Normal file
42
tests/ui/test-attrs/test-should-panic-failed-show-span.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
//@ run-fail
|
||||
//@ check-run-results
|
||||
//@ compile-flags: --test
|
||||
//@ exec-env:RUST_BACKTRACE=0
|
||||
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
|
||||
//@ run-flags: --test-threads=1
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn should_panic_with_any_message() {
|
||||
panic!("Panic!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "message"]
|
||||
fn should_panic_with_message() {
|
||||
panic!("message");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn should_panic_with_any_message_does_not_panic() {
|
||||
// DON'T PANIC
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "message"]
|
||||
fn should_panic_with_message_does_not_panic() {
|
||||
// DON'T PANIC
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "message"]
|
||||
fn should_panic_with_substring_panics_with_incorrect_string() {
|
||||
panic!("ZOMGWTFBBQ");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic = "message"]
|
||||
fn should_panic_with_substring_panics_with_non_string_value() {
|
||||
panic!(123); //~ WARNING panic message is not a string literal
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
|
||||
running 6 tests
|
||||
test should_panic_with_any_message - should panic ... ok
|
||||
test should_panic_with_any_message_does_not_panic - should panic ... FAILED
|
||||
test should_panic_with_message - should panic ... ok
|
||||
test should_panic_with_message_does_not_panic - should panic ... FAILED
|
||||
test should_panic_with_substring_panics_with_incorrect_string - should panic ... FAILED
|
||||
test should_panic_with_substring_panics_with_non_string_value - should panic ... FAILED
|
||||
|
||||
failures:
|
||||
|
||||
---- should_panic_with_any_message_does_not_panic stdout ----
|
||||
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:22:4
|
||||
---- should_panic_with_message_does_not_panic stdout ----
|
||||
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:28:4
|
||||
---- should_panic_with_substring_panics_with_incorrect_string stdout ----
|
||||
|
||||
thread 'should_panic_with_substring_panics_with_incorrect_string' panicked at $DIR/test-should-panic-failed-show-span.rs:35:5:
|
||||
ZOMGWTFBBQ
|
||||
note: panic did not contain expected string
|
||||
panic message: `"ZOMGWTFBBQ"`,
|
||||
expected substring: `"message"`
|
||||
---- should_panic_with_substring_panics_with_non_string_value stdout ----
|
||||
|
||||
thread 'should_panic_with_substring_panics_with_non_string_value' panicked at $DIR/test-should-panic-failed-show-span.rs:41:5:
|
||||
Box<dyn Any>
|
||||
note: expected panic with string value,
|
||||
found non-string value: `TypeId(0x56ced5e4a15bd89050bb9674fa2df013)`
|
||||
expected substring: `"message"`
|
||||
|
||||
failures:
|
||||
should_panic_with_any_message_does_not_panic
|
||||
should_panic_with_message_does_not_panic
|
||||
should_panic_with_substring_panics_with_incorrect_string
|
||||
should_panic_with_substring_panics_with_non_string_value
|
||||
|
||||
test result: FAILED. 2 passed; 4 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
warning: panic message is not a string literal
|
||||
--> $DIR/test-should-panic-failed-show-span.rs:41:12
|
||||
|
|
||||
LL | panic!(123);
|
||||
| ^^^
|
||||
|
|
||||
= note: this usage of `panic!()` is deprecated; it will be a hard error in Rust 2021
|
||||
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/panic-macro-consistency.html>
|
||||
= note: `#[warn(non_fmt_panics)]` on by default
|
||||
help: add a "{}" format string to `Display` the message
|
||||
|
|
||||
LL | panic!("{}", 123);
|
||||
| +++++
|
||||
help: or use std::panic::panic_any instead
|
||||
|
|
||||
LL - panic!(123);
|
||||
LL + std::panic::panic_any(123);
|
||||
|
|
||||
|
||||
warning: 1 warning emitted
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue