Parametrized tests best practices

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?

1 Like

Hi, how about generating the table as global variable using 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?

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.

Some discussion on improvements to Rust’s testing infrastructure here:

1 Like

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…