What is a good way to clean up after unit test in Rust?

Since the test function is interrupted upon failure, you cannot simply clear it at the end of the function being tested.

From testing frameworks in other languages, there is usually a way to set a callback that handles the cleanup at the end of each test function.

+7
unit-testing rust
source share
1 answer

Since the test function is interrupted upon failure, you cannot simply clear it at the end of the function being tested.

Use RAII and implement Drop . This eliminates the need to call something:

 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 
+8
source share

All Articles