la10736 / rstest

Fixture-based test framework for Rust
Apache License 2.0
1.21k stars 43 forks source link

'#[once]' cleanup after tests executed #209

Open aesteve-rh opened 1 year ago

aesteve-rh commented 1 year ago

Hi, thanks for this crate!

I am not sure if this can be actually done, but it would be great. Maybe with a new Attribute, something like #[final]?

Let me explain with an example. If I use the once fixture to set up an environment (e.g., create a folder and/or some files), it would be great to have them cleaned up only once, after a set of tests (all tests?) is done. Something like this:

mod test {
     use rstest::*;

     #[fixture]
     #[once]
     fn setup_folder() -> TempDir {
         /// Code to setup temp dir
    }

    #[rstest]
    fn test1(setup_folder: TempDir) {
        /// Do stuff
    }

    #[rstest]
    fn test2(setup_folder: TempDir) {
        /// Do stuff
    }

    #[final]
    fn cleanup() {
        /// Cleanup TempDir
    }
}
la10736 commented 1 year ago

Add I mentioned in the documentation "Take care that the #[once] fixture value will never be dropped." This limitation is due to that I have no idea of how to identify the end of the tests... :cry: rstest works on the top of standard cargo test and cannot have any control on tests lifetime. On rust per 1.0 there was a way to register an on exit hook but it was removed lot of time ago.

If I find a way to register a call back is invocated at the end of all tests that will open a way to a lot of new functionalities.

Anyway I used some work around based on arc+mutex and some hysteresis to start and tear down some docker containers in integration tests... But it's not a general purpose solution.