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

parameterizing fixtures #93

Closed
asomers opened this issue May 5, 2020 · 8 comments
Closed

parameterizing fixtures #93

asomers opened this issue May 5, 2020 · 8 comments

Comments

@asomers
Copy link

asomers commented May 5, 2020

With rstest, I can parameterize a test case using #[rstest(foo => [...])] or #[rstest(foo, case(...), ...)] syntax. But I don't see any way to parameterize a fixture. What I would like to do would be to define a fixture that can return multiple values, and use that to parameterize several test cases, like this:

#[fixture(foo => [1, 2, 3])]
fn inputs(foo: i32) -> i32 {
    foo
}

#[rstest] 
fn test_a(inputs: i32)
{
    ...
}
#[rstest] 
fn test_b(inputs: i32)
{
    ...
}

I would then expect to see six separate test cases get run, like

running 6 tests
test test_a_inputs_1 ... ok
test test_a_inputs_2 ... ok
test test_a_inputs_3 ... ok
test test_b_inputs_1 ... ok
test test_b_inputs_2 ... ok
test test_b_inputs_3 ... ok

Would such a thing be possible?

@la10736
Copy link
Owner

la10736 commented May 6, 2020

I already thought about that when I started to think on rstest. This's a pytest's feature that I really like and I would like implement in rstest too.... but rust is a static language and any test should be build at compile time and not run time.

But I didn't found any way to implement it preserving the rule 1 case => 1 test case without using any kind of custom test runner.

I can try to give another try to it...

Maybe in a lot of cases we can work around on it by something like described in #80

@asomers
Copy link
Author

asomers commented May 6, 2020

There's another option which would be nearly as useful. In every case I've encountered so far, the list of parameters is constant. So if there were some way to express it as a macro, then I could reuse it. Something a little like this, perhaps:

macro_rules! myparams {
    () => [1, 2, 3];
}
#[rstest inputs => myparams!()]
fn test_a(inputs: i32) {
    ...
}
#[rstest inputs => myparams!()]
fn test_b(inputs: i32) {
    ...
}

@la10736
Copy link
Owner

la10736 commented May 7, 2020

I already tried something like this but when rstest procedural macro is processed myparams!() is not expanded yet and there isn't any way to know how to expand it.

Rust compiler call procedural macro expansion with just the follow syntax tree and standard macro expansion come later....

@asomers
Copy link
Author

asomers commented May 7, 2020

I was afraid you'd say something like that. I guess it'll take a custom test runner then.

@la10736
Copy link
Owner

la10736 commented May 7, 2020

I didn't take in account to build a custom test runner because

  1. They aren't stable yet and AFAIK there isn't any plan to have them
  2. Ask to user to configure a custom test runner to build some test is a hard request: when you should write tests you would like just to write them and not bothering with some kind of fancy project configuration

I think that Rust's guy have did a very good think to make tests simple and very well integrated: my attempt is to preserve this simple approach.

Anyway I would like to write something to reuse test case set. That is a really need to have feature.

@la10736
Copy link
Owner

la10736 commented May 10, 2020

@asomers Have you took a look to #66? In this ticket I provided a way to build reusable set of tests. Maybe this approach can work either in your cases.

@asomers
Copy link
Author

asomers commented May 10, 2020

Yes I saw it. But for now I'm sticking with the galvanic-test crate. It provides a nicer syntax for parameterizing tests. The downside is that parameterizations aren't independent. They all run as a single test case.

@la10736
Copy link
Owner

la10736 commented Jun 21, 2020

I think I can close this issue: I think that rstest_reuse fit all cases explained in this ticket. Ok, it don't provide a parametrized fixture but rstest_reuse's template it's very close to it.

@la10736 la10736 closed this as completed Jun 21, 2020
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