test(接口测试): 调整接口覆盖率计算的单元测试

This commit is contained in:
Jianguo-Genius 2024-11-26 14:49:29 +08:00 committed by 刘瑞斌
parent bde39eb0c5
commit f6daf2fe8d
4 changed files with 59 additions and 46 deletions

View File

@ -45,6 +45,6 @@
where scenario.project_id = #{0} where scenario.project_id = #{0}
AND scenario.deleted IS FALSE AND scenario.deleted IS FALSE
AND step.step_type IN ('API', 'API_CASE', 'CUSTOM_REQUEST') AND step.step_type IN ('API', 'API_CASE', 'CUSTOM_REQUEST')
AND JSON_EXTRACT(step.config, '$.protocol') = 'http' AND JSON_UNQUOTE(JSON_EXTRACT(LOWER(step.config), '$.protocol')) = 'http'
</select> </select>
</mapper> </mapper>

View File

@ -2635,7 +2635,6 @@ public class ApiScenarioService extends MoveNodeService {
return Collections.emptyList(); return Collections.emptyList();
} }
// 分批处理配置请求 ID 列表 // 分批处理配置请求 ID 列表
Map<String, Set<String>> methodPathMap = new HashMap<>(); Map<String, Set<String>> methodPathMap = new HashMap<>();
SubListUtils.dealForSubList(requestIdList, 200, batchIds -> { SubListUtils.dealForSubList(requestIdList, 200, batchIds -> {

View File

@ -25,13 +25,9 @@ public class ApiDefinitionUtils {
if (StringUtils.isEmpty(customRequestUrl)) { if (StringUtils.isEmpty(customRequestUrl)) {
continue; continue;
} }
if (customRequestUrl.startsWith("/")) {
customRequestUrl = customRequestUrl.substring(1); customRequestUrl = pretreatmentUrl(customRequestUrl);
}
if (StringUtils.isNotEmpty(customRequestUrl)) {
if (customRequestUrl.contains("?")) {
customRequestUrl = customRequestUrl.substring(0, customRequestUrl.indexOf("?"));
}
String[] customUrlArr = customRequestUrl.split("/"); String[] customUrlArr = customRequestUrl.split("/");
if (StringUtils.startsWithAny(customRequestUrl.toLowerCase(), "https://", "http://") if (StringUtils.startsWithAny(customRequestUrl.toLowerCase(), "https://", "http://")
@ -67,14 +63,23 @@ public class ApiDefinitionUtils {
if (isFetch) { if (isFetch) {
return true; return true;
} }
}
} }
} }
} }
return false; return false;
} }
private static String pretreatmentUrl(String customRequestUrl) {
if (customRequestUrl.startsWith("/")) {
customRequestUrl = customRequestUrl.substring(1);
}
if (customRequestUrl.contains("?")) {
customRequestUrl = customRequestUrl.substring(0, customRequestUrl.indexOf("?"));
}
return customRequestUrl;
}
private static boolean isRestUrlParam(String urlParam) { private static boolean isRestUrlParam(String urlParam) {
return !StringUtils.startsWith(urlParam, "{") || !StringUtils.endsWith(urlParam, "}") || StringUtils.equals(urlParam, "{}"); return !StringUtils.startsWith(urlParam, "{") || !StringUtils.endsWith(urlParam, "}") || StringUtils.equals(urlParam, "{}");
} }

View File

@ -165,6 +165,9 @@ public class ApiCalculateTest extends BaseTest {
stepRequest.setName(apiTestCase.getName() + "_step"); stepRequest.setName(apiTestCase.getName() + "_step");
stepRequest.setRefType(ApiScenarioStepRefType.REF.name()); stepRequest.setRefType(ApiScenarioStepRefType.REF.name());
stepRequest.setProjectId(project.getId()); stepRequest.setProjectId(project.getId());
stepRequest.setConfig(new HashMap<>() {{
this.put("protocol", "http");
}});
steps.add(stepRequest); steps.add(stepRequest);
steptDetailMap.put(stepRequest.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msHttpElement))); steptDetailMap.put(stepRequest.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msHttpElement)));
} }
@ -179,6 +182,9 @@ public class ApiCalculateTest extends BaseTest {
stepRequest.setName("custom_step"); stepRequest.setName("custom_step");
stepRequest.setRefType(ApiScenarioStepRefType.DIRECT.name()); stepRequest.setRefType(ApiScenarioStepRefType.DIRECT.name());
stepRequest.setProjectId(project.getId()); stepRequest.setProjectId(project.getId());
stepRequest.setConfig(new HashMap<>() {{
this.put("protocol", "http");
}});
steps.add(stepRequest); steps.add(stepRequest);
MsHTTPElement customElement = MsHTTPElementTest.getMsHttpElement(); MsHTTPElement customElement = MsHTTPElementTest.getMsHttpElement();
@ -197,6 +203,9 @@ public class ApiCalculateTest extends BaseTest {
stepRequest.setName(resultData.getName() + "_step"); stepRequest.setName(resultData.getName() + "_step");
stepRequest.setRefType(ApiScenarioStepRefType.REF.name()); stepRequest.setRefType(ApiScenarioStepRefType.REF.name());
stepRequest.setProjectId(project.getId()); stepRequest.setProjectId(project.getId());
stepRequest.setConfig(new HashMap<>() {{
this.put("protocol", "http");
}});
steps.add(stepRequest); steps.add(stepRequest);
steptDetailMap.put(stepRequest.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msHttpElement))); steptDetailMap.put(stepRequest.getId(), JSON.parseObject(ApiDataUtils.toJSONString(msHttpElement)));
} }
@ -226,12 +235,12 @@ public class ApiCalculateTest extends BaseTest {
Assertions.assertEquals(16, apiCoverageDTO.getUnCoverWithApiCase()); Assertions.assertEquals(16, apiCoverageDTO.getUnCoverWithApiCase());
Assertions.assertEquals(apiCoverageDTO.getApiCaseCoverage(), CalculateUtils.reportPercentage(apiCoverageDTO.getCoverWithApiCase(), apiCoverageDTO.getAllApiCount())); Assertions.assertEquals(apiCoverageDTO.getApiCaseCoverage(), CalculateUtils.reportPercentage(apiCoverageDTO.getCoverWithApiCase(), apiCoverageDTO.getAllApiCount()));
Assertions.assertEquals(4, apiCoverageDTO.getCoverWithApiScenario()); Assertions.assertEquals(8, apiCoverageDTO.getCoverWithApiScenario());
Assertions.assertEquals(16, apiCoverageDTO.getUnCoverWithApiScenario()); Assertions.assertEquals(12, apiCoverageDTO.getUnCoverWithApiScenario());
Assertions.assertEquals(apiCoverageDTO.getScenarioCoverage(), CalculateUtils.reportPercentage(apiCoverageDTO.getCoverWithApiScenario(), apiCoverageDTO.getAllApiCount())); Assertions.assertEquals(apiCoverageDTO.getScenarioCoverage(), CalculateUtils.reportPercentage(apiCoverageDTO.getCoverWithApiScenario(), apiCoverageDTO.getAllApiCount()));
Assertions.assertEquals(6, apiCoverageDTO.getCoverWithApiDefinition()); Assertions.assertEquals(10, apiCoverageDTO.getCoverWithApiDefinition());
Assertions.assertEquals(14, apiCoverageDTO.getUnCoverWithApiDefinition()); Assertions.assertEquals(10, apiCoverageDTO.getUnCoverWithApiDefinition());
Assertions.assertEquals(apiCoverageDTO.getApiCoverage(), CalculateUtils.reportPercentage(apiCoverageDTO.getCoverWithApiDefinition(), apiCoverageDTO.getAllApiCount())); Assertions.assertEquals(apiCoverageDTO.getApiCoverage(), CalculateUtils.reportPercentage(apiCoverageDTO.getCoverWithApiDefinition(), apiCoverageDTO.getAllApiCount()));
Assertions.assertEquals("0.00%", CalculateUtils.reportPercentage(0, 0)); Assertions.assertEquals("0.00%", CalculateUtils.reportPercentage(0, 0));