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

2

32

In Rust, is there any way to execute a teardown function after all tests have been run (i.e. at the end of cargo test) using the standard testing library?

I'm not looking to run a teardown function after each test, as they've been discussed in these related posts:

These discuss ideas to run:

One workaround is a shell script that wraps around the cargo test call, but I'm still curious if the above is possible.

Peloria answered 27/11, 2019 at 20:19 Comment(6)
I tried that but couldn't get it to work. Something like this, maybe?Peloria
you can make a script I think like build.rs, you must probably configure a profilSacha
You are right, destructors aren't run for static variables – see #48732887 as well.Michaelmichaela
There's an accepted experimental RFC to allow custom test frameworks – see this tracking issue. This would allow a lot more flexibility around tests, but unfortunately there hasn't been much progress on this yet.Michaelmichaela
As @SvenMarnach, I think that there is no solution to this issue unless this RFC is implemented. If you use the nightly compiler, you can do that with the few that was implemented AFAIKEdita
People often use trybuild (docs.rs/trybuild/1.0.18/trybuild) for testing procedural macros, and I'm fairly certain you can actually apply this crate to any use case, even custom testing framework, if you must. Maybe if someone built such thing on top of this, the RFC mentioned above might even move forward?Extraterrestrial
S
4

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
Stuppy answered 12/6, 2023 at 15:1 Comment(1)
It's still unstable, but this would be definitely an upgrade from the status quo.Amateurish
E
3

I'm not sure there's a way to have a global ("session") teardown with Rust's built-in testing features, previous inquiries seem to have yielded little, aside from "maybe a build script". Third-party testing systems (e.g. shiny or stainless) might have that option though, might be worth looking into their exact capabilities

Alternatively, if nightly is suitable there's a custom test frameworks feature being implemented, which you might be able to use for that purpose.

That aside, you may want to look at macro_rules! to cleanup some boilerplate, that's what folks like burntsushi do e.g. in the regex package.

Euridice answered 29/7, 2020 at 9:3 Comment(2)
when linking to "previous inquiries have yielded little" you linked to this question 😅.Stirling
Link is dead...Amateurish

© 2022 - 2024 — McMap. All rights reserved.