-1

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

Catanzaro
  • 1,312
  • 2
  • 13
  • 24
  • I’m not sure what you want to do since your explanation is too confusing for me. Maybe it helps you to know that @TestFactory is supposed to create individual tests. Reporting of results and summary is done by Jupiter itself. – johanneslink Mar 23 '20 at 13:52
  • I have a TestFactory (Dynamic Test in Junit5) that run 20 dynamic test i n fuction to a Json. I want get results execute of this 20 tests.. HOW? In summary i have only count success/failed not verbose description with testID - results: – Catanzaro Mar 23 '20 at 13:53
  • i have rewritten my question.. Is possible take all tests/results? i can get only countFailure, countSuccess() and getListFailures but no getListSuccess.. – Catanzaro Mar 23 '20 at 14:02
  • Is there a reason you are using a launcher to run your tests instead of IDE, Gradle or Maven? – johanneslink Mar 23 '20 at 15:08
  • Yes because i must run this Test class in another App.. – Catanzaro Mar 23 '20 at 15:17
  • I use a maven project i need read Test Id with Result.. pls – Catanzaro Mar 23 '20 at 15:35

1 Answers1

0

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.

johanneslink
  • 4,877
  • 1
  • 20
  • 37
  • Thanks what you mean here:"There's probably more you want to do in the listener but it should give you an idea where to start."? – Catanzaro Mar 23 '20 at 23:07
  • You may also want to count the different kinds of test results or track the hierarchy of containers and tests etc. – johanneslink Mar 24 '20 at 05:38