Is there a way to execute a teardown function after all tests have been run?

Here is an example implementation of the custom test framework solution mentioned by Masklinn:

#![feature(custom_test_frameworks)]
#![feature(test)]
#![test_runner(custom_test_runner)]

extern crate test;

use test::{test_main_static, TestDescAndFn};

fn main() {}

pub fn custom_test_runner(tests: &[&TestDescAndFn]) {
    println!("Setup");
    test_main_static(tests);
    println!("Teardown");
}

#[cfg(test)]
mod tests {

    #[test]
    fn test1() {
        println!("Test 1")
    }

    #[test]
    fn test2() {
        println!("Test 2")
    }
}

This will print:

Setup
Test 1
Test 2
Teardown

Leave a Comment