refactor(接口测试): 接口用例列表总执行通过率计算

https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001048522
This commit is contained in:
Jianguo-Genius 2024-11-07 17:29:40 +08:00 committed by 建国
parent 346267c468
commit a0de41847c
6 changed files with 32 additions and 5 deletions

View File

@ -9,7 +9,6 @@ import io.metersphere.api.dto.ReferenceDTO;
import io.metersphere.api.dto.ReferenceRequest;
import io.metersphere.api.dto.definition.*;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.service.ApiBatchRunBaseService;
import io.metersphere.api.service.ApiFileResourceService;
import io.metersphere.api.service.definition.*;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
@ -37,7 +36,6 @@ import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@ -170,6 +168,13 @@ public class ApiTestCaseController {
return PageUtils.setPageInfo(page, apiTestCaseService.page(request, false, true,null));
}
@PostMapping(value = "/statistics")
@Operation(summary = "接口测试-接口管理-接口用例-统计")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_READ)
public List<ApiTestCaseDTO> calculate(@Validated @RequestBody List<String> ids) {
return apiTestCaseService.calculate(ids);
}
@PostMapping("/batch/delete")
@Operation(summary = "接口测试-接口管理-接口用例-批量删除")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE)

View File

@ -87,7 +87,7 @@ public class ApiTestCaseDTO {
private List<String> tags;
@Schema(description = "用例通过率")
private String passRate;
private String passRate = "NONE";
@Schema(description = "请求内容")
private AbstractMsTestElement request;

View File

@ -29,5 +29,5 @@ public class ApiScenarioDTO extends ApiScenario {
@Schema(description = "脚本错误标识")
private String scriptIdentifier;
@Schema(description = "执行通过率", requiredMode = Schema.RequiredMode.REQUIRED)
private String execPassRate = "0%";
private String execPassRate = "NONE";
}

View File

@ -341,6 +341,20 @@ public class ApiTestCaseService extends MoveNodeService {
return apiCaseLists;
}
public List<ApiTestCaseDTO> calculate(List<String> ids) {
List<ApiTestCaseDTO> returnList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(ids)) {
List<CasePassDTO> passRateList = extApiTestCaseMapper.findPassRateByIds(ids);
Map<String, String> passRates = passRateList.stream().collect(Collectors.toMap(CasePassDTO::getId, CasePassDTO::getValue));
for (String id : ids) {
ApiTestCaseDTO dto = new ApiTestCaseDTO();
dto.setId(id);
dto.setPassRate(passRates.getOrDefault(id, "0%"));
returnList.add(dto);
}
}
return returnList;
}
private void buildApiTestCaseDTO(List<ApiTestCaseDTO> apiCaseLists) {
if (CollectionUtils.isNotEmpty(apiCaseLists)) {
List<String> userIds = new ArrayList<>();

View File

@ -2641,6 +2641,8 @@ public class ApiScenarioService extends MoveNodeService {
}
}
dto.setExecPassRate(CalculateUtils.reportPercentage(passCount, all));
} else {
dto.setExecPassRate("0%");
}
result.add(dto);
}

View File

@ -45,6 +45,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -90,7 +91,7 @@ public class ApiReportControllerTests extends BaseTest {
@Test
@Order(1)
public void testInsert() {
public void testInsert() throws Exception {
List<ApiReport> reports = new ArrayList<>();
List<ApiTestCaseRecord> records = new ArrayList<>();
for (int i = 0; i < 2515; i++) {
@ -130,6 +131,11 @@ public class ApiReportControllerTests extends BaseTest {
steps.add(apiReportStep);
}
apiReportService.insertApiReportStep(steps);
// 顺便查找一下通过率
// 查统计数据
List<String> caseIds = Collections.singletonList("api-resource-id0");
requestPostWithOk("/api/case/statistics", caseIds);
}
private MvcResult responsePost(String url, Object param) throws Exception {