Dynamic test cases at run time

I want to unit test my code with dynamic input from a file.
Each line contains parameters for a test case.
Currently, I just read this file an run the testing of all lines within one single #[test] function.
However, I'd like each line to be a separate test case.
How can I achieve that?

This internals.rust-lang.org thread has some notes on dynamic test generation. In short, #[test] generates code that uses the libtest library whose API is private, so you can't use it to generate test cases dynamically. There are other test runners and libraries which do have exposed APIs that allow you to do this, for example libtest_mimic. The IRO thread mentions datatest. Perhaps a simpler option though would be to create a tool that reads the input file and just generates Rust code with a #[test] function per case.

Personally, I tend to stick with having one #[test] function which runs a dynamically generated set of cases. You lose some affordances that way (eg. filtering tests), but it avoids non-standard test infrastructure.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.