我有一个看起来像这样的简单测试
public void shouldCompareEntities(Person input1, Person input2, String expected) throws Exception {
Javers javers = JaversBuilder.javers()
.withListCompareAlgorithm(ListCompareAlgorithm.LEVENSHTEIN_DISTANCE)
.build();
Diff diff = javers.compare(input1, input2);
String jsonDiff = javers.getJsonConverter().toJson(diff);
JSONAssert.assertEquals(expected, jsonDiff, JSONCompareMode.STRICT);
}
这是使用数据提供程序的参数化测试。我在资源中有一组文件夹,其中包含用于参数化测试的数据。
json/
case1/
expected.json
input-1.json
input-2.json
case2/
expected.json
input-1.json
input-2.json
我有dataProvider
@DataProvider(name = "jsonDiff", parallel = true) // runs in a separate of test pool
public Object[][] jsonDiffProvider() {
Gson gson = new Gson();
List<Path> resourceFolder = listResourceFolder("/json");
Map<Path, List<Path>> testFiles = aggregateCaseFiles(resourceFolder);
assertNotNull(testFiles);
List<Object[]> arrays = testFiles.entrySet()
.stream()
.map(entry -> {
List<Path> caseFiles = entry.getValue(); // sorted -> expected, input-1, input-2
JsonReader input1Reader = fileToReader(caseFiles.get(1));
JsonReader input2Reader = fileToReader(caseFiles.get(2));
String expectedJson = fileToString(caseFiles.get(0));
Person input1 = toObject(gson, input1Reader, Person.class);
Person input2 = toObject(gson, input2Reader, Person.class);
return new Object[] { input1, input2, expectedJson }; })
.collect(Collectors.toList());
Object[][] cases = new Object[arrays.size()][3];
int i = 0;
for (Object[] arr : arrays) {
System.arraycopy(arr, 0, cases[i++], 0, 3);
}
return cases;
}
它基本上读取所有文件,将它们转换,创建数组数组并执行测试。很好。
我的问题是,有没有一种方法可以执行所有案例而无需在数据提供者中汇总它们?
由于case
文件夹并不相互依赖,因此看起来执行效果并不理想,因此可能可以分别对每个文件夹执行测试。但是我不确定。