由于测试功能在故障时中止,因此不能简单地在被测功能结束时进行清理.
从其他语言的测试框架来看,通常有一种方法可以设置一个回调来处理每个测试函数末尾的清理.
由于测试功能在故障时中止,因此不能简单地在被测功能结束时进行清理.
使用RAII并实施Drop.它不需要调用任何东西:
struct Noisy;
impl Drop for Noisy {
fn drop(&mut self) {
println!("I'm melting! Meeeelllllttttinnnng!");
}
}
#[test]
fn always_fails() {
let my_setup = Noisy;
assert!(false, "or else...!");
}
Run Code Online (Sandbox Code Playgroud)
running 1 test
test always_fails ... FAILED
failures:
---- always_fails stdout ----
thread 'always_fails' panicked at 'or else...!', main.rs:12
note: Run with `RUST_BACKTRACE=1` for a backtrace.
I'm melting! Meeeelllllttttinnnng!
failures:
always_fails
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
471 次 |
| 最近记录: |