在JUnit4中使用参数化测试时,是否有一种方法可以设置我自己的自定义测试用例名称?
我想改变默认的-[测试类]. runtest [n] -有意义的东西。
在JUnit4中使用参数化测试时,是否有一种方法可以设置我自己的自定义测试用例名称?
我想改变默认的-[测试类]. runtest [n] -有意义的东西。
当前回答
你可以创建一个方法
@Test
public void name() {
Assert.assertEquals("", inboundFileName);
}
虽然我不会一直用它,但弄清楚143到底是哪个测试数字还是很有用的。
其他回答
您可能还想尝试JUnitParams: https://github.com/Pragmatists/JUnitParams
由于所访问的参数(例如“{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...
);
}
对于一个更复杂的对象,您可以执行以下操作(以JUnit 4为例):
@RunWith(Parameterized.class)
public class MainTest {
private static Object[] makeSample(String[] array, int expectedLength) {
return new Object[]{array, expectedLength, Arrays.toString(array)};
}
@Parameterized.Parameters(name = "for input {2} length should equal {1}")
public static Collection<Object[]> data() {
return Arrays.asList(
makeSample(new String[]{"a"}, 1),
makeSample(new String[]{"a", "b"}, 2)
);
}
private final int expectedLength;
private final String[] array;
public MainTest(String[] array, int expectedLength, String strArray) {
this.array = array;
this.expectedLength = expectedLength;
}
@Test
public void should_have_expected_length() {
assertEquals(expectedLength, array.length);
}
}
这里的技巧是使用一个输入参数作为字符串来描述输入的某些部分或整个测试用例。
在添加第三个参数之前,它是这样的
然后像这样
我大量使用静态导入的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}
});
}
看看JUnit 4.5,它的运行器显然不支持这一点,因为该逻辑隐藏在Parameterized类中的私有类中。您不能使用JUnit Parameterized运行器,而是创建自己的能够理解名称概念的运行器(这导致了如何设置名称的问题……)
从JUnit的角度来看,如果他们传递逗号分隔的参数,而不是(或除了)仅仅传递一个增量,那就更好了。TestNG就是这样做的。如果这个特性对你很重要,你可以在www.junit.org的雅虎邮件列表上发表评论。