Search code examples
javatestingjunit

How to create JUnit tests for different input files as separate cases?


Right now, I have around 107 test input cases for my interpreter, and I have my JUnit tester set up to manually handle each case independently so as to not lump them all together. That is, if I use a loop to iterate over the test files as such

for (int i = 0; i < NUM_TESTS; i++) {
    String fileName = "file_" + (i + 1) + ".in";
    testFile(fileName);
}

JUnit will create one giant test result for all 107 tests, meaning if one fails, the entire test fails, which I don't want. As I said, right now I have something like

@Test
public static void test001() {
    testFile("file1.in");
}

@Test
public static void test002() {
    testFile("file2.in");
}

While this works, I imagine there's a much better solution to get what I'm after.


Solution

  • You can use @ParameterizedTest with @MethodSource annotations.

    For exemple :

    @ParameterizedTest
    @MethodSource("fileNameSource")
    void test(final String fileName) {
        testFile(fileName);
    }
    
    private static Stream<String> fileNameSource() {
        return IntStream.range(0,NUM_TESTS).mapToObj(i -> "file_" + (i + 1) + ".in");
    }
    

    Check the documentation at https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests

    For each params returned by fileNameSource(), the corresponding test will be considered as a different case.