JUnit5-从json文件动态展示测试用例名称

问题描述

我想在使用junit5 ..运行测试用例时动态显示测试用例的名称,所以请帮助我

@ParameterizedTest
@JsonFileSource(resouce="file1.json")
public void abcTest(JsonObje obj){
}

file1.json
[{
"test-case-name" : "test case for infant","ageGroup" : "infant"
},{
"test-case-name" : "test case for adult","ageGroup" : "adult"
},{
"test-case-name" : "test case for mature","ageGroup" : "mature"
}
]

I want to display test case name dynamically on running the test cases using junit5..
For example ... :

test case for infant
test case for adult
test case for mature
...

我想在使用junit5运行测试用例时动态显示测试用例名称。 例如

解决方法

您提供的信息不多,但是我相信您希望使用JUnit's Dynamic Tests而不是参数化测试。

@TestFactory
Collection<DynamicTest> dynamicTestsFromCollection() {
  JsonArray array = ...
  List<DynamicTest> tests = new ArrayList<>();
  for (JsonObject testCase: array) {
    String testCaseName = testCase.get("test-case-name");
    String ageGroup = testCase.get("age-group");
    tests.add(DynamicTest.dynamicTest(testCaseName,() -> testAbc(ageGroup)));
  }
  return tests;
}

这将为您的每个年龄段创建一个单元测试。

,

我尝试遵循@CsvFileSource示例,但是它没有帮助。 https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-writing-parameterized-tests/