How to clear the status after the test process dies in the cargo test?

How do I clean up the status after the test process dies in the cargo test? If it's just a single test case, I can guarantee through Drop。I expected WarpDrop to call, but it didn't. (WarpDrop needs to exist in multiple testcases. If each case is regenerated, the cost is high.)

#[tokio::test]
async fn should_failure_when_without_xx1() {

}

#[tokio::test]
async fn should_failure_when_without_xx2() {

}
..

static INIT: Mutex<Option<WarpDrop>> = Mutex::new(None);

struct WarpDrop(Vec<Child>);

impl Drop for WarpDrop {
    fn drop(&mut self) {
        for chile in &mut self.0 {
            let _ = chile.kill();
        }
    }
}

Statics aren't dropped when the process ends. I don't think there's a good way to guarantee cleanup runs exactly once at the end of all your tests for a target with the built in test harness.

1 Like

I didn't find it either, but I think it's a shortcoming of cargo, like other language test frameworks actually have before and after phases.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.