javaeclipsejunit4parameterized

How to specify label to a Parameterized junit run


In Eclipse, while using the Parameterized runner in a junit test class, each run is noted by a number (0, 1, etc.)

Is there a way to replace this number with a proper label?

PS: I am using a JUNIT version 4.8 older than 4.11 so the @Parameters does not take any argument

Test Case:

@RunWith(value = Parameterized.class)
public class TestClass {

    @Parameters
    public static Collection<Object[]> getLabels() {
        List<Object[]> labels = new ArrayList<Object[]>();
        labels.add(new Object[] {"Toto"});
        labels.add(new Object[] {"Titi"});
        return labels;
    }

    private final String label;

    public TestClass(String label) {
        this.label = label;
    }

    @Test
    public void test1() {
        assertTrue(true);
    }
}

Result:

enter image description here


Solution

  • There is an easy way to easily identify the individual test cases in a Parameterized test, you may provide a name using the @Parameters annotation.
    This name is allowed to contain placeholders that are replaced at runtime:

    {index}: the current parameter index
    {0}, {1}, …: the first, second, and so on, parameter value

    See example here: https://github.com/junit-team/junit/wiki/Parameterized-tests