Search code examples
javaunit-testingjunitjunit4

Creating multiple unit tests from a loop in Java


I'm writing unit tests for my (small) program, and the test cases are specified in around 30 different files.
To test it, all I need is a loop that goes over all the files, parse them, and execute what's needed.

The problem is that in that case, all of my tests will be considered as one, since it's all in the same function with the @Test notation.
Is it possible to split it somehow without having to have a separate function for each test file?

The problem with all the tests as one test case, is that I can't see which test case fail the process; and if one fails it fail the rest (I'll get 1 test failed instead of 5/30 failed)

I'm currently using JUnit (4.12), but I'm not obligated to keep using it, so I can switch framework if there's a better solution.

Thanks!

Example:

public class MyTests {
    @Test
    public void testFromFiles {
        // loop through all the files
    }
}

output: 1 test run successfully 

Update: The selected answer worked great for me, and I added another solution with JUnit 5 (instead of 4), in case it will help someone.


Solution

  • Try this way:

    @RunWith(Parameterized.class)
    public class EdiTest {
    
        @SuppressWarnings("WeakerAccess")
        @Parameterized.Parameter(value = 0)
        public String model;
    
        @SuppressWarnings("WeakerAccess")
        @Parameterized.Parameter(value = 1)
        public String filename;
    
        @Parameterized.Parameters(name = "{index}: testEDI({0}, {1})")
        public static Collection<Object[]> data() {
            return Arrays.asList(new Object[][]{
                    {"753", "edi753_A.edi"},
                    {"753", "edi753_B.edi"},
                    {"754", "edi754.edi"},
                    {"810", "edi810-withTax.edi"},
                    {"810", "edi810-withoutTax.edi"},
            });
        }
    
        @Before
        public void setUpContext() throws Exception {
            TestContextManager testContextManager = new TestContextManager(getClass());
            testContextManager.prepareTestInstance(this);
        }
    
        @Test
        public void testEDI() throws IOException {
            String edi = IOUtils.toString(ClassLoader.getSystemResource(filename));
            EdiConverter driver = ediConverterProvider.getConverter(model);
    
            // your test code here
        }
    
    }