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
Caching fixtures #182
Comments
You can use Maybe you need that your tests should modify this fixture too... you can follow the follow pattern #[fixture]
#[once]
fn very_slow() -> A {
// your expensive job
}
#[fixture]
fn dependency(very_slow: &A) -> A {
very_slow.clone()
}
#[rstest]
fn test(dependency: A) {
....
} In this case very_slow is execute just once for all your tests. |
I see. So #[once] has then a permanent memory, and if we were to pass a &mut reference to the test function and somehow changed the object, then those changes would persist to the other tests? I then also assume the tests are not run in parallel (which would create race conditions and weird behaviour)? Is this something that rstest allows in general, parallelized tests? I would maybe then suggest creating a macro that encapsulates the behaviour of the first two fixtures, i.e. something the calculates a variable, but then yields copies of that variable, maybe something like a |
In Rust you cannot have a shared mutable reference. If you need serial tests you can use Anyway you can encapsulate you fixture behind a static reference of a About your proposal maybe I can just introduce a #[fixture]
#[once(clone)]
fn very_slow() -> A {
// your expensive job
}
#[rstest]
fn test(very_slow: A) {
....
} |
Hey, Yeah, Yes, I understand the shared mutable reference issue. But if we don't have these and the tests are truly unit tests, is it possible to run them in parallel? For example |
@la10736 |
Ok, should be quite simple to do.... but Let me some time to try to refactor it before and then I'll give all information of how to implement this feature. |
Ok, I've refactored the code. rstest/rstest_macros/src/parse/mod.rs Line 676 in c16601d
Option<Ident> and contains also this variant is the more explicit way to do it).
To see an example of how implement both Please add both unit and integration tests, documentation on both Feel free to ask every question. THX |
Hi.
I want to implement a fixture that generates an initial state in my program, that will then use for various tests.
However, the code of that fixture can be slow to run (maybe I have to download some data), so I want to cache the output of that fixture so that it is only executed once even though it is used by multiple tests. How can I do this?
Of course, each test should have an independent copy of the output of that fixture, so they are completely independent.
The text was updated successfully, but these errors were encountered: