java - 如何在 Junit 5 中动态传递多个输入和输出测试文件名?
问题描述
我需要将多个文件一个一个地传递给下面的 BeforeAll 方法。我可以通过重复相同代码的不同测试类来实现它。我想使用单个类对其进行优化。
@BeforeAll
static void setUp() throws IOException {
inputList = readInput(CommonTestConstants.FilePath + "/Input1.json");
expectedList = readExpected(CommonTestConstants.FilePath + "/Expected1.json");
assertEquals("Checking size of both list", inputList.size(), expectedList.size());
}
static Stream<Arguments> Arguments() {
return IntStream.range(0, inputList.size())
.mapToObj(i -> Arguments.of(inputList.get(i), expectedList.get(i)));
}
@ParameterizedTest
@DisplayName("Parameterized Test For First Input")
@MethodSource("Arguments")
void testFact(Object object, ExpectedObject expected) throws Exception {
Outcome outcome = processExpectedJson(object);
assertEquals(expected, outcome);
}
任何人请建议实现这一目标?
解决方案
您可以创建一个Map
文件名,然后按如下方式对其进行迭代,
Map<String, String> files = Map.of("/Input1.json", "/Expected1.json",
"Input1.json", "/Expected2.json");
files.forEach((k,v)->{
inputList = readInput(CommonTestConstants.FilePath + k);
expectedList = readExpected(CommonTestConstants.FilePath + v);
assertEquals("Checking size of both list",
inputList.size(), expectedList.size());
});
推荐阅读
- scala - 如何将 spark scala 数据框列转换为日期时间?
- haskell - 如何对非单子使用`bound`?
- r - CRAN 拒绝不在主流存储库中的 tidyverse 依赖项
- python - Windows 10 错误:FileNotFoundError:系统在执行 .exe 时找不到指定子进程的文件
- c++ - std::mt19937 为相同的第一个浮点数 ex(1.2, 1.5) 给出相同的随机浮点数
- flutter - 我想创建一个密钥库来签署应用程序,但每次我收到这个错误时说什么?
- sparql - 如何在 SPARQL 的语句组中检索限定符单元?
- php - 在 Symfony 5 中异步使用电子邮件
- asp.net - 通过按钮编辑视图模型的成员而不提交
- google-chrome - 未发送 Javascript 跨域 Cookie Websocket