-
Notifications
You must be signed in to change notification settings - Fork 37
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
Comments
Sorry for late reply - busy times. How would this look like? Could you provide examples / usages in other test frameworks? |
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
} |
Okay, now I have a better picture. |
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.
The text was updated successfully, but these errors were encountered: