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

Declare test cases once, use them multiple times #93

Open
thatbakamono opened this issue Apr 3, 2022 · 3 comments
Open

Declare test cases once, use them multiple times #93

thatbakamono opened this issue Apr 3, 2022 · 3 comments

Comments

@thatbakamono
Copy link

Is there a way to declare test cases only once and use them multiple times across multiple functions in multiple files? I read the wiki but I didn't see anything like that. If there isn't, that might be a feature worth adding.

@luke-biel
Copy link
Collaborator

Sorry for late reply - busy times. How would this look like? Could you provide examples / usages in other test frameworks?

@thatbakamono
Copy link
Author

I'm not sure what is the best way to realise that, some ways are harder but a lot more configurable and some are simpler but less flexible, It's up to debate which one is the best for test-case.

rstest does that in a really simple but at the same time not so powerful way:

You first declare a template as an empty test function marked using #[template] with test cases you want to use multiple times

#[template]
#[rstest]
#[case(2, 2)]
#[case(4/2, 2)]
fn two_simple_cases(#[case] a: u32, #[case] b: u32) {}

and then you can apply it to a bunch of other tests using #[apply]

#[apply(two_simple_cases)]
fn it_works(#[case] a: u32, #[case] b: u32) {
    assert!(a == b);
}

proptest's way is a bit less simple but it's also a lot more powerful:

You first declare a strategy (basically a function generating values):

fn vec_and_index() -> impl Strategy<Value = (Vec<String>, usize)> {
    prop::collection::vec(".*", 1..100)
        .prop_flat_map(|vec| {
            let len = vec.len();
            (Just(vec), 0..len)
        })
}

and then you can use it as an input in your tests

proptest! {
    #[test]
    fn test_some_function((vec, index) in vec_and_index()) {
        some_function(vec, index);
    }
}

fn some_function(stuff: Vec<String>, index: usize) {
    let _ = &stuff[index];
    // Do stuff
}

@luke-biel
Copy link
Collaborator

Okay, now I have a better picture.
I'm more fond of second design especially that a value generator is something that has gone through my mind before. A blocker in this case is how we structured our code - it's a singular proc-macro crate. I'm trying to rewrite it so that we could export other items than just macros, but it's gonna take some time.
After that we can definitelly design this functionality in terms of test-case and add it.

@luke-biel luke-biel added this to To do in 2.1.0 Apr 22, 2022
@luke-biel luke-biel moved this from To do to Backlog in 2.1.0 May 12, 2022
@luke-biel luke-biel added this to To do in 2.2.0 May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
2.1.0
Backlog
2.2.0
To do
Development

No branches or pull requests

2 participants