I’ve a functional test using Junit
which tests delivery of xml file to an end-point. XML file is sent by client, so I copied it to a test folder, read it from there in my test and do asserts.
We have 10-20 such files with different data but same format, my architect wants me to copy all of them into same folder, loop through them in same test, just to make sure that they are delivered successfully. Note, I am not testing any data validations in this test just successful delivery.
I am not sure if its a good idea to loop through multiple files in a single test in my case because if one fails rest will not be executed and also not convinced for writing 20 tests doing exactly same thing. Please advice
How you should write this test is more a matter of style than anything else.
Some say that each test method should have only a single assertion, because that makes the test very focused, and it’s easy to quickly tell exactly what was wrong.
This seems too extreme to me, so you could follow the guidance of your architect but make the error message in your assertion clear as to which file caused the test failure.
Even better, to have all files tested even when one fails, don’t use assertions on a per-file basis. Cache each file’s result in the execution loop and if any failed, output the details and fail the test. For example:
@Test
public void testAllFiles() {
final Set<String> failures = new HashSet<>();
for (File file : getAllFiles()) {
final Boolean success = processFile(file); // or however you determine success/failure
if (!success) failures.add(file.getName());
}
if (!failures.isEmpty()) {
log.error("Failed file(s):");
for (String filename : failures) {
log.error("t"+filename);
}
fail();
}
}
3
If the goal of the test is to determine whether the method under test will deliver all 20 files, then it is a single test, and should be written accordingly.
I’m not sure it would buy you much to split it into 20 separate tests, since you’re testing the same functionality in each one.
For a test to check that all files are delivered use the answer of Robert Harvey.
If you need to run a test with 20 different inputs use a data provider. Don’t create loops in the code but use the loops structure with data providers so you have less code and use a standard solution for the issue.
2