1

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

3

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();
    }
}
  • +1, especially for the suggestion in the last paragraph. – Doc Brown Nov 20 '13 at 18:23
  • Thanks for your input. Is there a way to have tests created dynamically for each file in a loop – Charu Khurana Nov 20 '13 at 18:23
  • The if test at the end didn't make sense; fixed now. The getAllFiles() method would contain the logic to figure out what files need to be tested. I don't know of a way to create tests dynamically, but I'm sure you could do it with CGLib or similar if you really wanted to. – Mike Partridge Nov 20 '13 at 18:28
1

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.

0

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.

  • Can you elaborate your case of data provider with an example – Charu Khurana Nov 20 '13 at 19:10
  • I work with PHP, in which this is a very clear example: phpunit.de/manual/3.7/en/… So as you see data is separated from the actual test. The data provider calls the test for each set of data. – Luc Franken Nov 21 '13 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.