java - 如何在 Junit5 的动态测试中获得测试结果?
问题描述
我的功能类似于:
@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
我有20个孩子测试。我在 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);
现在使用 summary= listener.getSummary() 我不读取所有测试结果,而只计算失败或成功测试。
我如何读取所有测试的所有结果失败/成功?我想要这样的地图:
TEST_ID - RESULTS
test0001 Success
test0002 Fail
test0003 Success
test0004 Success
test0005 Fail
我怎么得到这个?有可能吗?感谢和问候
解决方案
一种方法是创建您自己的实现org.junit.platform.launcher.TestExecutionListener
并将其注册到启动器。您可以先查看 的源代码SummaryGeneratingListener
。您可以更改executionFinished(..)
以构建测试结果图。这是一个草图:
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());
}
}
您可能还想在侦听器中做更多事情,但它应该让您知道从哪里开始。
推荐阅读
- regex - 正则表达式匹配单词中不包含的单个非空白字符
- javascript - 用户关闭浏览器php时如何更新数据库表
- stata - 合并具有不同数据的行
- php - PHP Group Array Results Together By Value
- python - python closure/nested function fail when with assignment to outer array
- f# - F# 按运行总数列出组?
- html - 使用 boostrap Grid System 进行堆叠
- javascript - webpack 中缺少 svg 的上下文
- javascript - 同时访问变量
- parsing - 如何使用 fparsec 解析由双空格分隔的单词序列?