JUnit5 – 从 JSON 文件动态地展示测试用例名称

huangapple go评论110阅读模式
英文:

JUnit5 - Showcase test case name dynamically from json file

问题

Sure, here's the translated code part:

  1. 我想要在使用Junit5运行测试用例时动态显示测试用例名称..所以请在这方面帮助我
  2. @ParameterizedTest
  3. @JsonFileSource(resouce="file1.json")
  4. public void abcTest(JsonObje obj){
  5. }
  6. file1.json
  7. [{
  8. "test-case-name" : "婴儿测试用例",
  9. "ageGroup" : "婴儿"
  10. },
  11. {
  12. "test-case-name" : "成年人测试用例",
  13. "ageGroup" : "成年人"
  14. },
  15. {
  16. "test-case-name" : "成熟人士测试用例",
  17. "ageGroup" : "成熟人士"
  18. }
  19. ]
  20. 我想要在使用Junit5运行测试用例时动态显示测试用例名称..
  21. 例如 ... :
  22. 婴儿测试用例
  23. 成年人测试用例
  24. 成熟人士测试用例
  25. ...
  26. 我想要在使用Junit5运行测试用例时动态显示测试用例名称..
  27. 例如
英文:

I want to display test case name dynamically on running the test cases using Junit5..so please help me on that

  1. @ParameterizedTest
  2. @JsonFileSource(resouce="file1.json")
  3. public void abcTest(JsonObje obj){
  4. }
  5. file1.json
  6. [{
  7. "test-case-name" : "test case for infant",
  8. "ageGroup" : "infant"
  9. },
  10. {
  11. "test-case-name" : "test case for adult",
  12. "ageGroup" : "adult"
  13. },
  14. {
  15. "test-case-name" : "test case for mature",
  16. "ageGroup" : "mature"
  17. }
  18. ]
  19. I want to display test case name dynamically on running the test cases using Junit5..
  20. For example ... :
  21. test case for infant
  22. test case for adult
  23. test case for mature
  24. ...

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

答案1

得分: 1

  1. 你没有提供太多信息,但我认为你想要使用[Junit的动态测试](https://junit.org/junit5/docs/current/user-guide/#writing-tests-dynamic-tests),而不是参数化测试。
  2. @TestFactory
  3. Collection<DynamicTest> dynamicTestsFromCollection() {
  4. JsonArray array = ...
  5. List<DynamicTest> tests = new ArrayList<>();
  6. for (JsonObject testCase: array) {
  7. String testCaseName = testCase.get("test-case-name");
  8. String ageGroup = testCase.get("age-group");
  9. tests.add(DynamicTest.dynamicTest(testCaseName, () -> testAbc(ageGroup)));
  10. }
  11. return tests;
  12. }
  13. 这将为每个年龄组创建一个单元测试。
英文:

You don't give much information, but I believe that you want JUnit's Dynamic Tests, instead of parameterized tests.

  1. @TestFactory
  2. Collection&lt;DynamicTest&gt; dynamicTestsFromCollection() {
  3. JsonArray array = ...
  4. List&lt;DynamicTest&gt; tests = new ArrayList&lt;&gt;();
  5. for (JsonObject testCase: array) {
  6. String testCaseName = testCase.get(&quot;test-case-name&quot;);
  7. String ageGroup = testCase.get(&quot;age-group&quot;);
  8. tests.add(DynamicTest.dynamicTest(testCaseName, () -&gt; testAbc(ageGroup)));
  9. }
  10. return tests;
  11. }

This will create a unit test for each of your age groups.

huangapple
  • 本文由 发表于 2020年8月19日 03:21:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63475346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定