
上QQ阅读APP看书,第一时间看更新
Failing tests
There are also test cases where you will want your API methods to fail based on some input, and you want the test framework to assert this failure. Rust provides an attribute called #[should_panic] for this. Here's a test that panics and uses this attribute:
// panic_test.rs
#[test]
#[should_panic]
fn this_panics() {
assert_eq!(1, 2);
}
The #[should_panic] attribute can be paired with a #[test] attribute to signify that running the this_panics function should cause a non-recoverable failure, which is called a panic in Rust.