更改参数化测试的名称 [英] Changing names of parameterized tests
问题描述
在JUnit4中使用参数化测试时,有没有办法设置我自己的自定义测试用例名称?
Is there a way to set my own custom test case names when using parameterized tests in JUnit4?
我想更改默认值 - [测试类] .runTest [n]
- 有意义的事情。
I'd like to change the default — [Test class].runTest[n]
— to something meaningful.
推荐答案
此功能已进入 JUnit 4.11 。
This feature has made it into JUnit 4.11.
要使用更改参数化测试的名称,您可以说:
To use change the name of parameterized tests, you say:
@Parameters(name="namestring")
namestring
是一个字符串,可以包含以下特殊占位符:
namestring
is a string, which can have the following special placeholders:
-
{index}
- 这组参数的索引。默认的namestring
是{index}
。 -
{0}
- 此测试调用的第一个参数值。 -
{1}
- 第二个参数值 - 依此类推
{index}
- the index of this set of arguments. The defaultnamestring
is{index}
.{0}
- the first parameter value from this invocation of the test.{1}
- the second parameter value- and so on
测试的最终名称将是测试方法的名称,后面是括号中的 namestring
。如下所示。
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;
}
}
将提供类似的名称testFib [1:fib(1)= 1]
和 testFib [4:fib(4)= 3]
。 ( testFib
部分名称是 @Test
的方法名称。)
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
).
这篇关于更改参数化测试的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!