-
Notifications
You must be signed in to change notification settings - Fork 39
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. |
I've just found this issue after asking about this on the rust forum. Is there a concrete reason why this could not be done with compositional macros. I'm new to rust so I'm most likely aware of something specific in the way macros work. Ideally something along these lines would make the existing framework much more powerful.
Due to reasons that I don't understand, this expands in such a way as to try to invoke |
And here I am, yet another person, with a different idea. My thought is that you should allow putting #[test_case(1)]
#[test_case(2)]
#[test_case(3)]
mod blah {
fn test_one(num: u32) {
// first test
}
fn test_two(num: u32) {
// second test
}
} should generate six tests:
Do note I have near zero experience with proc macros, and don't know how viable this would be. |
I know there is a workaround, of putting all the tests in one function, like below, but it leads to less granular test results. #[test_case(1)]
#[test_case(2)]
#[test_case(3)]
fn blah(num: u32) {
fn test_one(num: u32) {
// first test
}
fn test_two(num: u32) {
// second test
}
test_one(num);
test_two(num);
} |
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: