简体   繁体   English

如何在 Junit5 的动态测试中获得测试结果?

[英]How get results of test in a Dynamic Test in Junit5?

my function is similar to:我的功能类似于:

@TestFactory
public Stream<DynamicTest> dynamicTest() throws Exception {
    String geocodingAnasJsonTest = properties.getProperty("smart-road.simulator.json.geocoding-it.anas.testSuite.test");
    String endpoint = properties.getProperty("smart-road.simulator.endpoint.anasGeocoding");
    RequestSpecification request = RestAssured.given().header("Authorization", auth);
    request.accept(ContentType.JSON);
    request.contentType(ContentType.JSON);
    JsonNode jsonObjectArray = JsonMappingUtil.getJsonFileFromPath(geocodingAnasJsonTest);
    Stream<JsonNode> elementStream = StreamSupport.stream(Spliterators
            .spliteratorUnknownSize(jsonObjectArray.elements(),
                    Spliterator.ORDERED), false);
    return elementStream.map(jsonNode -> DynamicTest.dynamicTest(String.format("Test ID: %s", jsonNode.get("test_name")),
            () -> {request.body(jsonNode.get("request").toString());
                   Response response = request.post(endpoint);
                   int statusCode = response.getStatusCode();
                   boolean res = false;
                   if (statusCode >= 200 && statusCode < 300) {
                     res = true;
                   }
                   try {
                        assertEquals(true, res, properties.getProperty("smart-road.response.smart-road.message.status.ok"));
                        logger.info(properties.getProperty("smart-road.response.smart-road.message.status.ok"));
                        String responseOK=jsonNode.get("response").toString();
                        assertEquals(responseOK, response.asString(), properties.getProperty("smart-road.response.smart-road.message.status.right-end"));
                        logger.info(properties.getProperty("smart-road.response.smart-road.message.status.right-end"));
                   } catch (AssertionFailedError er) {
                        logger.error(properties.getProperty("smart-road.response.smart-road.message.status.assertion-failed"));
                        fail("Test Fallito");
                        Assertions.assertTrue(true);
                   }
            }
            )//fine dynamicTest
    );//fine map
}//fine metodo

I have 20 children test.我有20个孩子测试。 I run test in main:我在主要运行测试:

SummaryGeneratingListener listener = new SummaryGeneratingListener();
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                .selectors(selectMethod(Test.class,"dynamicTest"))
                .build();

        Launcher launcher = LauncherFactory.create();
        launcher.registerTestExecutionListeners(listener);
        launcher.execute(request);

Now with summary= listener.getSummary() i dont read all tests result but only count Failed or Successfull test.现在使用 summary= listener.getSummary() 我不读取所有测试结果,而只计算失败或成功测试。

How i read all result fail/success for all tests?我如何读取所有测试的所有结果失败/成功? I will want a map like this:我想要这样的地图:

TEST_ID - RESULTS
test0001   Success
test0002   Fail
test0003   Success
test0004   Success
test0005   Fail

How i get this?我怎么得到这个? Is possible?有可能吗? Thanks Regards感谢和问候

One approach is to create your own implementation of org.junit.platform.launcher.TestExecutionListener and register it with the launcher.一种方法是创建自己的org.junit.platform.launcher.TestExecutionListener实现并将其注册到启动器。 You may look at the source code of SummaryGeneratingListener as a first start.您可以首先查看SummaryGeneratingListener的源代码。 You could change executionFinished(..) to build up the map of test results.您可以更改executionFinished(..)以构建测试结果图。 Here's a sketch:这是一个草图:

class MySummaryListener implements TestExecutionListener {
    private Map<String, TestExecutionResult.Status> summary = new HashMap<>();

    @Override
    public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
        summary.put(testIdentifier.getDisplayName(), testExecutionResult.getStatus());
    }
}

There's probably more you want to do in the listener but it should give you an idea where to start.在侦听器中您可能还有更多事情要做,但它应该让您知道从哪里开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM