Parametrized tests best practices


#1

I have quite a lot of pure functions in a crate I’m writing, so they naturally lend themselves to parametrized testing – I generate a table of all valid inputs and expected outputs, and test each combination in a loop. The problem is that if any of the cases fails, the whole test ends. Furthermore, if the error is a panic inside the code under test, I don’t know what the problematic input was.
I’ve searched the forum, but coudn’t find any recent discussions of this. Is there a better way to go about this than creating individual tests with macros? Since most of the input/output pairs are generated by code, macros would still mean a lot of repetition. I could try write the generation code itself as a macro, but I’m not sure that’s much nicer…
What’s considered the best practice nowadays?


#2

Hi, how about generating the table as global variable using lazy_static https://docs.rs/lazy_static/1.0.0/lazy_static/, writing a single function expecting an index as parameter and then enumerating the test-functions, each one calling the function with different index?


#3

That sounds a bit dangerous, because you’re very likely to forget to add the actual test, even if it’s in the table. It’s also a violation of DRY.


#4

Some discussion on improvements to Rust’s testing infrastructure here: https://internals.rust-lang.org/t/past-present-and-future-for-rust-testing/6354


#5

Wow, that’s quite a discussion! On the one hand, I’m happy it’s taking place, but on the other hand, there are so many different concerns and perspectives that I’m kinda resigned to keeping the status quo for the foreseeable future…