在JUnit4中使用参数化测试时,是否有一种方法可以设置我自己的自定义测试用例名称?

我想改变默认的-[测试类]. runtest [n] -有意义的东西。


当前回答

一种解决方法是捕获所有Throwable并将其嵌套到一个新的Throwable中,并使用包含有关参数的所有信息的自定义消息。消息将出现在堆栈跟踪中。 当所有断言、错误和异常的测试失败时,这种方法都有效,因为它们都是Throwable的子类。

我的代码是这样的:

@RunWith(Parameterized.class)
public class ParameterizedTest {

    int parameter;

    public ParameterizedTest(int parameter) {
        super();
        this.parameter = parameter;
    }

    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] { {1}, {2} });
    }

    @Test
    public void test() throws Throwable {
        try {
            assertTrue(parameter%2==0);
        }
        catch(Throwable thrown) {
            throw new Throwable("parameter="+parameter, thrown);
        }
    }

}

失败测试的堆栈跟踪是:

java.lang.Throwable: parameter=1
    at sample.ParameterizedTest.test(ParameterizedTest.java:34)
Caused by: java.lang.AssertionError
    at org.junit.Assert.fail(Assert.java:92)
    at org.junit.Assert.assertTrue(Assert.java:43)
    at org.junit.Assert.assertTrue(Assert.java:54)
    at sample.ParameterizedTest.test(ParameterizedTest.java:31)
    ... 31 more

其他回答

查看dsaff提到的JUnitParams,它使用ant在html报告中构建参数化测试方法描述。

这是在尝试LabelledParameterized并发现它虽然可以与eclipse一起工作,但就html报告而言,它不能与ant一起工作。

欢呼,

这个特性已经加入到JUnit 4.11中。

若要使用更改参数化测试的名称,请执行以下命令:

@Parameters(name="namestring")

Namestring是一个字符串,可以有以下特殊占位符:

{index} -这组参数的索引。默认名称字符串是{index}。 {0} -测试调用的第一个参数值。 {1} -第二个参数值 等等

测试的最终名称将是测试方法的名称,后面跟着括号中的名称字符串,如下所示。

例如(改编自Parameterized注释的单元测试):

@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;
    }
}

将给出像testFib[1: fib(1)=1]和testFib[4: fib(4)=3]这样的名称。(名称的testFib部分是@Test的方法名)。

在junit4.8.2中,您可以通过简单地复制Parameterized class来创建自己的MyParameterized类。更改TestClassRunnerForParameters中的getName()和testName()方法。

由于所访问的参数(例如“{0}”总是返回toString()表示,一个解决方法是在每种情况下进行匿名实现并覆盖toString()。例如:

public static Iterable<? extends Object> data() {
    return Arrays.asList(
        new MyObject(myParams...) {public String toString(){return "my custom test name";}},
        new MyObject(myParams...) {public String toString(){return "my other custom test name";}},
        //etc...
    );
}

我大量使用静态导入的Assert和朋友,所以我很容易重新定义断言:

private <T> void assertThat(final T actual, final Matcher<T> expected) {
    Assert.assertThat(editThisToDisplaySomethingForYourDatum, actual, expected);
}

例如,您可以向您的测试类添加一个“name”字段,在构造函数中初始化,并在测试失败时显示它。只需将它作为每个测试参数数组的第一个元素传递进来。这也有助于标记数据:

public ExampleTest(final String testLabel, final int one, final int two) {
    this.testLabel = testLabel;
    // ...
}

@Parameters
public static Collection<Object[]> data() {
    return asList(new Object[][]{
        {"first test", 3, 4},
        {"second test", 5, 6}
    });
}