java - JUnit 5:在 ParameterizedTest 中访问索引
问题描述
考虑这个片段:
@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));
}
}
推荐阅读
- operating-system - 用 yocto 定义新的图像特征
- visual-studio-code - 如何覆盖特定主题的字体?
- ansible - 条件检查 'user_sdf_exist.rc == 0' 失败
- python - 具有弹出值能力的 Nosql 文件数据库
- python - DROP TABLE IF EXISTS 说“未知表”?[MySQL]
- python-3.x - python starlette中的自定义异常
- botframework - 如何从 microsoft bot 发送电子邮件?
- django - 我如何在 django 中通知用户有关 webhook 的信息
- django - Gunicorn 未在 django 中加载反应前端反应 nginx 配置 digitalocean
- c# - 在将DataTable导出到excel之前将日期时间格式的字符串-“yyyyMMddhhmmssfff”转换为“DD-MM-YYYY HH:MM”