-
Notifications
You must be signed in to change notification settings - Fork 43
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
Allow defining fixture session scope #119
Comments
I though a lot in the last years about this feature and I never found any good solution. The real issue here is not to define the scope but find when the scope terminate and we should release the resources ... in practice there is no way hook your teardown. Ok, not all cases need it and lot of times we can don't care about release our fixtures but that's not true in general: for instance your fixture is just a docker instance that you would like to stop when all your tests that need it are done. So when I don't need to drop my static fixture I just use a lazy static reference to it but if I need to destroy I switch to Anyway, maybe it's time to call a stop loss and start to accept this limitation. I can code a basic implementation where you can mark as static your fixture and you know that the drop will never invoked. Maybe later I can think about to implement the the Ok... as I just say before you're my best customer and I can't ignore your requests. |
Any chance I can bug you into taking a look at this one of these days? :) |
Ok, I'll try to find a slot in the next weeks. |
I decided to implement a In this case the fixture will return a static reference to the static instance of your fixture and the fixture will never drop. That's the real limitation of this implementation: you cannot use it for something that you need to release at the end of your tests. We can fine some crates that implement this behavior like use std::sync::Once;
struct MyStruct(u32);
fn get() -> &'static MyStruct {
static mut S: Option<MyStruct> = None;
static CELL: Once = Once::new();
CELL.call_once(|| unsafe { S = Some(MyStruct(rand::random())) });
unsafe { S.as_ref().unwrap() }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn all_threads_should_have_same_state() {
let threads = (0..10000)
.into_iter()
.map(|_| std::thread::spawn(|| get().0))
.collect::<Vec<_>>();
let values = threads
.into_iter()
.map(|t| t.join().unwrap())
.collect::<std::collections::HashSet<_>>();
assert_eq!(1, dbg!(values).len());
}
} SyntaxUse #[fixture]
#[once]
fn my_fixture() -> F {
....
}
#[rstest]
fn my_test(my_fixture: &F) {
....
} |
Ok base test and implementations are in place. What is missed:
|
Is not possible use Also |
Done |
Hey, you continue to output amazing work!
However, I wonder whether it'd be possible to allow fixtures to run within certain scopes such as per session, per module, per test (the default).
You can currently kind of emulate the once per session scope with a global atomic but it'd be great if something like this were built into rstest with an attribute.
Reference from pytest feature: https://docs.pytest.org/en/6.2.x/fixture.html#scope-sharing-fixtures-across-classes-modules-packages-or-session
The text was updated successfully, but these errors were encountered: