Since the test-function aborts on a failure, one cannot simply clean up at the end of the function under test.
From testing frameworks in other languages, there's usually a way to setup a callback that handles cleanup at the end of each test-function.
Since the test-function aborts on a failure, one cannot simply clean up at the end of the function under test.
Use RAII and implement Drop
. It removes the need to call anything:
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...!");
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With