How to specify label to a Parameterized junit run
Asked Answered
D

2

2

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

Drusilladrusus answered 23/1, 2014 at 12:4 Comment(3)
possible duplicate of Changing names of parameterized testsShoeblack
@MartinSchröder Nope. I don't want to change the Test Class name or the test method. I just want to replace the numbered indices with a String. I have already checked the one you pointed at and it's not a duplicate!Drusilladrusus
@MartinSchröder Also, the solution is only available since junti 4.11 while I am using an older version of junit 4Drusilladrusus
H
3

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

Hindmost answered 24/1, 2014 at 5:30 Comment(3)
Hello, the name attribute was added in junit 4.11. I am using 4.8 so it won't workDrusilladrusus
Is there reason that you are sticking to this version and don't upgrade to the latest?Hindmost
Version management is not handled by me nor am i allowed to change itDrusilladrusus
B
0

Parameterized test is calling toString() internally, which does not work for us because some our implementations do not allow toString() and throw an exception.

In this case, the test names will be

TestClass
    testMethod
        [1] Argument1.toString()
        [2] Argument2.toString()
        [3]

I created an object wrapper for my argument that holds the original object, and overrides toString method.

Here is the example.

private static List<LabelArgument> getLabels() {
    List<LabelArgument> labels = new ArrayList<>();
    labels.add(LabelArgument.of(new SimpleLabel("Hi there")));
    labels.add(LabelArgument.of(new LabelExtended2D("Good bye!")));

    // Label toString throws an exception
    labels.add(LabelArgument.of(new Label("Simple"))); 

    return labels;
}

@ParameterizedTest
@MethodSource("getLabels")
void testLabel(LabelArgument labelArgument ) {
    var label = labelArgument.getLabel();
    // Do the test
}

private static class LabelArgument {

    private Label label;

    private LabelArgument(Label label) {
        this.label = label;
    }

    public static LabelArgument of(Label label) {
        return new LabelArgument(label);
    }

    @Override
    public String toString() {
        return label.getClass().getSimpleName();
    }
}

It will produce

TestClass
    testAllLabels
        [1] SimpleLabel
        [2] LabelExtended2D
        [3] Label
Bochum answered 9/4, 2021 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.