Search code examples
javaeclipseunit-testingjunitparameterized-unit-test

ParameterizedTest with a name in Eclipse Testrunner


When you run a JUnit 4 ParameterizedTest with the Eclipse TestRunner, the graphical representation is rather dumb: for each test you have a node called [0], [1], etc. Is it possible give the tests [0], [1], etc. explicit names? Implementing a toString method for the tests does not seem to help.

(This is a follow-up question to JUnit test with dynamic number of tests.)


Solution

  • JUnit4 now allows specifying a name attribute to the Parameterized annotation, such that you can specify a naming pattern from the index and toString methods of the arguments. E.g.:

    @Parameters(name = "{index}: fib({0})={1}")
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
                { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
    }