考虑这个片段:
@ParameterizedTest
@ValueSource(strings = {"a", "b", "c"})
void test(final String line) {
// code here
}
这将是一个实际测试,但为简单起见,假设其目的只是打印:
Line 1: processed "a" successfully.
Line 2: processed "b" successfully.
Line 3: failed to process "c".
换句话说,我希望可以访问测试值的索引 内考试。
根据我的发现,
{index}
可以在测试之外使用以正确命名。
最佳答案
我不确定 JUnit 5 当前是否支持这个。解决方法可能是使用 @MethodSource
并提供 List<Argument>
符合您的需求。
public class MyTest {
@ParameterizedTest
@MethodSource("methodSource")
void test(final String input, final Integer index) {
System.out.println(input + " " + index);
}
static Stream<Arguments> methodSource() {
List<String> params = List.of("a", "b", "c");
return IntStream.range(0, params.size())
.mapToObj(index -> Arguments.arguments(params.get(index), index));
}
}
关于java - JUnit 5 : Accessing index inside ParameterizedTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62418780/