Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aesteve-rh opened this issue Aug 25, 2023 · 1 comment
Open

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

aesteve-rh opened this issue Aug 25, 2023 · 1 comment

Comments

@aesteve-rh
Copy link
Contributor

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
Copy link
Owner

la10736 commented Aug 26, 2023

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... 😢
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants