更改参数化测试的名称

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

我想将默认设置更改为[Test class].runTest[n]有意义的设置。


江户川乱折腾
浏览 337回答 3
3回答

一只斗牛犬

此功能使其成为JUnit 4.11的一部分。要使用更改参数化测试的名称,请说:@Parameters(name="namestring")namestring 是一个字符串,可以具有以下特殊占位符:{index}-这组参数的索引。默认namestring值为{index}。{0} -此测试调用的第一个参数值。{1} -第二个参数值等等测试的最终名称将是测试方法的名称,后跟namestring方括号,如下所示。例如(从单元测试改编为Parameterized注释):@RunWith(Parameterized.class)static public class FibonacciTest {&nbsp; &nbsp; @Parameters( name = "{index}: fib({0})={1}" )&nbsp; &nbsp; public static Iterable<Object[]> data() {&nbsp; &nbsp; &nbsp; &nbsp; return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });&nbsp; &nbsp; }&nbsp; &nbsp; private final int fInput;&nbsp; &nbsp; private final int fExpected;&nbsp; &nbsp; public FibonacciTest(int input, int expected) {&nbsp; &nbsp; &nbsp; &nbsp; fInput= input;&nbsp; &nbsp; &nbsp; &nbsp; fExpected= expected;&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; public void testFib() {&nbsp; &nbsp; &nbsp; &nbsp; assertEquals(fExpected, fib(fInput));&nbsp; &nbsp; }&nbsp; &nbsp; private int fib(int x) {&nbsp; &nbsp; &nbsp; &nbsp; // TODO: actually calculate Fibonacci numbers&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }}将命名为testFib[1: fib(1)=1]和testFib[4: fib(4)=3]。(testFib名称的一部分是的方法名称@Test)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java