I'm writing a conformance test in the form of rust integration tests which reads some data from a file (four fields per test case) and calls into the public API of the lib crate and compares the result.
Now, the problem is that I'm checking all the test case from my source data file in one #[test] function, which results in failing the test on the very first test case failure. I want to simulate a #[test] fn situation for each single test case, so we can start improving the number of failures procedurally, instead of having to fix the failing test case that's ordered first.
I have considered using a macro, but since the data file needs some more-than-obvious text processing, it doesn't look like a suitable solution.
I have searched many of the resources online and couldn't find any documentation on the internals of rust's test platform (have not digged into rust source code, yet, though), and couldn't find anything helpful.
So, is it possible at all? If not, is there any specific reason for it, or just lack of public interest?
Let me add that I've seen code with panic::catch_unwind() calls that handle failures (like this), but that's not what I'm looking for, as after catching the failures (via catch_unwind or just in a loop), I still want to be able to tell the test framework which cases passed and which ones failed.
You could write a non-test function in your test that loads the file and presents a single case, like fn get_case(sourceFile: &Path, case_num: u32);. Sort of a helper function. Then use a macro where the invocation chooses which number of case to get, and maybe adds a suffix for that test case to the test function name.
Thanks, @ryan. So, now that you suggested that, I couldn't figure out how to concat a function name prefix (like test_) to a macro ident. Is that possible already?
Also, do you mean that we don't have access to the internals for test framework?
Hum... let's assume that I don't want to parse the test file on compile time. In that case, there's no way I can use macros to create N test cases on compile time (annotated with #[test]) to be executed by the test framework.
As far as I know, Rust doesn't have compile time evaluation (at least not in stable) yet, so you would have to use a build script to do something like that.
So I'm assuming you would write a test module that looks something like this.
where a macro_invocation would expand to something like this for make_test!(test1, 1);
#[test]
fn test1() {
let data = load_file_and_get_case(1);
/// asserts below!
}
In the link I provided above, it shows how to write the macro to create a function. I imagine this could be pretty slow if you have a lot of tests, because it would open/close the file for every case!
EDIT: If you don't want to hand type all the macro invocations, you can write another macro that does that for you, so you can just type in a list of test case names and numbers.