在 JUnit4 中使用参数化测试时,有没有办法设置我自己的自定义测试用例名称?
我想将默认值 - [Test class].runTest[n]
- 更改为有意义的内容。
最佳答案
此功能已进入 JUnit 4.11 .
要使用更改参数化测试的名称,您可以说:
@Parameters(name="namestring")
namestring
是一个字符串,可以有以下特殊占位符:
{index}
- 这组参数的索引。默认名称字符串
为{index}
。{0}
- 此次测试调用的第一个参数值。{1}
- 第二个参数值- 等等
测试的最终名称将是测试方法的名称,后跟括号中的名称字符串
,如下所示。
例如(改编自参数化
注释的单元测试):
@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
的方法名称)。
关于java - 更改参数化测试的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7928574/