Comments (5)
It could be an options .... but I've some doubts about your example: you can never write something like this in safe rust and you must wrap your shared instance in a Mutex
, RefCell
or something else.
Anyway this can be useful... I'll try to find some time for implement it but I'm little busy now :(
from rstest.
Or maybe fixture_once
. What do you think?
from rstest.
Ok I try to sketch a valid example
#[derive(PartialEq, Debug, Copy, Clone)]
struct Service(u32);
#[fixture]
fn random() -> u32 {
rand::thread_rng().gen()
}
#[fixture]
#[once_test]
fn shared_random(#[from(random)] r: u32) -> u32 {
r
}
#[fixture]
fn service_a(shared_random: &u32) -> Service {
Service(*random)
}
#[fixture]
fn service_b(shared_random: &u32) -> Service {
Service(*random)
}
#[fixture]
fn service_c(random: u32) -> Service {
Service(random)
}
use once_cell::sync::Lazy
use std::sync::Mutex;
static SERVICE: Lazy<Mutex<Option<Service>>> = Lazy::new(|| Mutex::new(None));
fn check_service(s: Service) {
let old = SERVICE.lock().unwrap();
match *old {
Some(old_service) => assert_ne!(old_service, s),
None => { *old = Some(s); }
}
}
#[rstest]
#[case::first_contest]
#[case::other_contest]
fn shared_dep(service_a: Service, service_b: Service, service_c: Service) {
assert_eq!(service_a, service_b);
assert_ne!(service_a, service_c);
check_service(service_a);
}
from rstest.
I'm looking at this issue and trying to see if this is what I need for my use case. I need a fixture that is like #[once]
but is recreated for each test case. In other words, all the fixtures called from a single test case get one instance of this fixture, but later tests get a different instance.
My case is that I am testing a builder pattern framework. I have a mutable Builder that I can add objects to and get a handle back. Later my Builder gets consumed into some repository of objects. In other words, for one test case I need all fixtures to receive a &mut of the same Builder. The test case itself needs to receive the final built Builder and consume it.
A contrived example:
#[oncetest]
fn builder {
Builder::default()
}
#[fixture]
fn thing1(builder: &mut Builder) -> ThingHandle {
builder.add(Thing(1))
}
#[fixture]
fn thing2(builder: &mut Builder) -> ThingHandle {
builder.add(Thing(2))
}
#[rstest]
fn testthings(thing1: ThingHandle, thing2: ThingHandle, builder: Builder) {
repo = builder.build();
repo.get(thing1);
repo.get(thing2);
}
If this is possible now, I'd love to hear how. If not, does your #[once_fixture]
idea solve this?
from rstest.
@jsadusk Use mutable reference in Rust is a pain 😢
I think that you can do it by wrap it around a mutex, use thread_local
and return a guard instead a reference.
from rstest.
Related Issues (20)
- Generate tests from json files HOT 2
- [question] Test with different specializations of a generic type? HOT 4
- '#[once]' cleanup after tests executed HOT 1
- `#[ignore]` at the top only applies to the first case HOT 3
- Consider supporting `wasm-bindgen-test` for async test HOT 3
- Allow customizing the "length" of the test case names from the path HOT 1
- Separating test input data & expected values? HOT 1
- Unable to silence lints HOT 1
- New #[files] feature does not recompile on file change HOT 5
- Consider allowing to rename the crate by passing an attribute `crate = ...` to the proc-macro HOT 9
- vscode/rust-analyzer "debug overlay action" not working anymore HOT 2
- Question: tests failed by memory allocation of x bytes failure` when only running many cases once HOT 2
- Allow `clippy::too_many_arguments` HOT 6
- Please clearly state the MSRV in the README and documentation and use the "rust-version" flag. HOT 2
- Add capability to ignore arguments HOT 3
- [vscode] text wrapping for run/debug test buttons
- Misleading error messaging when defining a test that includes a lifetime argument HOT 1
- Support argument destructuring for `#[values(...)]` HOT 1
- Injecting static types HOT 5
- Create a fixture from `files` attribute HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rstest.