Changing names of parameterized tests Changing names of parameterized tests java java

Changing names of parameterized tests


This feature has made it into JUnit 4.11.

To use change the name of parameterized tests, you say:

@Parameters(name="namestring")

namestring is a string, which can have the following special placeholders:

  • {index} - the index of this set of arguments. The default namestring is {index}.
  • {0} - the first parameter value from this invocation of the test.
  • {1} - the second parameter value
  • and so on

The final name of the test will be the name of the test method, followed by the namestring in brackets, as shown below.

For example (adapted from the unit test for the Parameterized annotation):

@RunWith(Parameterized.class)static public class FibonacciTest {    @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 } });    }    private final int fInput;    private final int fExpected;    public FibonacciTest(int input, int expected) {        fInput= input;        fExpected= expected;    }    @Test    public void testFib() {        assertEquals(fExpected, fib(fInput));    }    private int fib(int x) {        // TODO: actually calculate Fibonacci numbers        return 0;    }}

will give names like testFib[1: fib(1)=1] and testFib[4: fib(4)=3]. (The testFib part of the name is the method name of the @Test).


Looking at JUnit 4.5, its runner clearly doesn't support that, as that logic is buried inside a private class inside the Parameterized class. You could not use the JUnit Parameterized runner, and create your own instead which would understand the concept of names (which leads to the question of how you might set a name ...).

From a JUnit perspective, it would be nice if instead of (or in addition to) just passing an increment, they would pass the comma delimited arguments. TestNG does this. If the feature is important to you, you can comment on the yahoo mailing list referenced at www.junit.org.


I recently came across the same problem when using JUnit 4.3.1. I implemented a new class which extends Parameterized called LabelledParameterized. It has been tested using JUnit 4.3.1, 4.4 and 4.5. It reconstructs the Description instance using the String representation of the first argument of each parameter array from the @Parameters method. You can see the code for this at:

http://code.google.com/p/migen/source/browse/trunk/java/src/.../LabelledParameterized.java?r=3789

and an example of its use at:

http://code.google.com/p/migen/source/browse/trunk/java/src/.../ServerBuilderTest.java?r=3789

The test description formats nicely in Eclipse which is what I wanted since this makes failed tests a lot easier to find! I will probably further refine and document the classes over the next few days/weeks. Drop the '?' part of the URLs if you want the bleeding edge. :-)

To use it, all you have to do is copy that class (GPL v3), and change @RunWith(Parameterized.class) to @RunWith(LabelledParameterized.class) assuming the first element of your parameter list is a sensible label.

I don't know if any later releases of JUnit address this issue but even if they did, I can't update JUnit since all my co-developers would have to update too and we have higher priorities than re-tooling. Hence the work in the class to be compilable by multiple versions of JUnit.


Note: there is some reflection jiggery-pokery so that it runs across the different JUnit versions as listed above. The version specifically for JUnit 4.3.1 can be found here and, for JUnit 4.4 and 4.5, here.