Giter Site home page Giter Site logo

once with a test context about rstest HOT 5 OPEN

oriontvv avatar oriontvv commented on June 26, 2024
once with a test context

from rstest.

Comments (5)

la10736 avatar la10736 commented on June 26, 2024 1

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.

oriontvv avatar oriontvv commented on June 26, 2024

Or maybe fixture_once. What do you think?

from rstest.

la10736 avatar la10736 commented on June 26, 2024

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.

jsadusk avatar jsadusk commented on June 26, 2024

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.

la10736 avatar la10736 commented on June 26, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.