简体   繁体   中英

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. 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.

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. You may look at the source code of SummaryGeneratingListener as a first start. You could change executionFinished(..) to build up the map of test results. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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