fix(接口测试): 接口用例执行异常

This commit is contained in:
AgAngle 2024-03-13 21:26:32 +08:00 committed by 刘瑞斌
parent 49090a2312
commit 70f1455000
7 changed files with 100 additions and 37 deletions

View File

@ -17,12 +17,12 @@ public class ApiRunModeConfigDTO implements Serializable {
* 是否并行执行
* {@link io.metersphere.sdk.constants.ApiBatchRunMode}
*/
private String runMode;
private String runMode = ApiBatchRunMode.PARALLEL.name();
/**
* 是否是集成报告
*/
private Boolean integratedReport;
private Boolean integratedReport = false;
/**
* 集合报告配置

View File

@ -88,7 +88,7 @@ public class TaskRequestDTO implements Serializable {
private String projectId;
/**
* {@link io.metersphere.sdk.constants.ApiBatchRunMode}
* {@link io.metersphere.sdk.constants.ApiExecuteRunMode}
*/
@NotBlank
private String runMode;
@ -97,7 +97,7 @@ public class TaskRequestDTO implements Serializable {
* 运行配置
*/
@Valid
private ApiRunModeConfigDTO runModeConfig;
private ApiRunModeConfigDTO runModeConfig = new ApiRunModeConfigDTO();
/**
* TODO 要执行的请求总量用于计算执行各种指标

View File

@ -20,6 +20,7 @@ import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.notice.annotation.SendNotice;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.security.CheckOwner;
import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.utils.PageUtils;
import io.metersphere.system.utils.Pager;
import io.metersphere.system.utils.SessionUtils;
@ -254,7 +255,14 @@ public class ApiTestCaseController {
@Operation(summary = "用例执行,获取获取执行结果")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE)
public TaskRequestDTO run(@PathVariable String id, @PathVariable String reportId) {
return apiTestCaseService.run(id, reportId);
return apiTestCaseService.run(id, reportId, SessionUtils.getUserId());
}
@GetMapping("/run/{id}")
@Operation(summary = "用例执行")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE)
public TaskRequestDTO run(@PathVariable String id) {
return apiTestCaseService.run(id, IDGenerator.nextStr(), SessionUtils.getUserId());
}
@PostMapping("/debug")

View File

@ -23,6 +23,7 @@ import io.metersphere.sdk.constants.ApiExecuteRunMode;
import io.metersphere.sdk.constants.ProjectApplicationType;
import io.metersphere.sdk.constants.StorageType;
import io.metersphere.sdk.dto.api.task.ApiExecuteFileInfo;
import io.metersphere.sdk.dto.api.task.ApiRunModeConfigDTO;
import io.metersphere.sdk.dto.api.task.TaskRequestDTO;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.file.FileCenter;
@ -183,7 +184,7 @@ public class ApiExecuteService {
*/
private TaskRequestDTO doExecute(TaskRequestDTO taskRequest) throws Exception {
// 获取资源池
TestResourcePoolReturnDTO testResourcePoolDTO = getGetResourcePoolNodeDTO(taskRequest.getProjectId());
TestResourcePoolReturnDTO testResourcePoolDTO = getGetResourcePoolNodeDTO(taskRequest.getRunModeConfig(), taskRequest.getProjectId());
TestResourceNodeDTO testResourceNodeDTO = getProjectExecuteNode(testResourcePoolDTO);
if (StringUtils.isNotBlank(testResourcePoolDTO.getServerUrl())) {
// 如果资源池配置了当前站点则使用资源池的
@ -217,9 +218,12 @@ public class ApiExecuteService {
}
}
private TestResourcePoolReturnDTO getGetResourcePoolNodeDTO(String projectId) {
String resourcePoolId = getProjectApiResourcePoolId(projectId);
return getAvailableResourcePoolDTO(projectId, resourcePoolId);
private TestResourcePoolReturnDTO getGetResourcePoolNodeDTO(ApiRunModeConfigDTO runModeConfig, String projectId) {
String poolId = runModeConfig.getPoolId();
if (StringUtils.isBlank(poolId)) {
poolId = getProjectApiResourcePoolId(projectId);
}
return getAvailableResourcePoolDTO(projectId, poolId);
}
/**
@ -436,7 +440,7 @@ public class ApiExecuteService {
return testResourcePoolService.getTestResourcePoolDetail(resourcePoolId);
}
private String getProjectApiResourcePoolId(String projectId) {
public String getProjectApiResourcePoolId(String projectId) {
// 查询接口默认资源池
ProjectApplication resourcePoolConfig = projectApplicationService.getByType(projectId, ProjectApplicationType.API.API_RESOURCE_POOL_ID.name());
// 没有配置接口默认资源池

View File

@ -308,6 +308,7 @@ public class ApiTestCaseBatchRunService {
return taskRequest;
}
/**
* 预生成用例的执行报告
*
@ -315,48 +316,37 @@ public class ApiTestCaseBatchRunService {
* @param apiTestCases
* @return
*/
private List<ApiTestCaseRecord> initApiReport(ApiRunModeConfigDTO runModeConfig, List<ApiTestCase> apiTestCases, String userId) {
public List<ApiTestCaseRecord> initApiReport(ApiRunModeConfigDTO runModeConfig, List<ApiTestCase> apiTestCases, String userId) {
List<ApiReport> apiReports = new ArrayList<>();
List<ApiTestCaseRecord> apiTestCaseRecords = new ArrayList<>();
for (ApiTestCase apiTestCase : apiTestCases) {
// 初始化报告
ApiReport apiReport = getApiReport(runModeConfig, apiTestCase, userId);
ApiTestCaseRecord apiTestCaseRecord = getApiTestCaseRecord(apiTestCase, apiReport);
apiReports.add(apiReport);
// 创建报告和用例的关联关系
ApiTestCaseRecord apiTestCaseRecord = apiTestCaseService.getApiTestCaseRecord(apiTestCase, apiReport);
apiTestCaseRecords.add(apiTestCaseRecord);
}
apiReportService.insertApiReport(apiReports, apiTestCaseRecords);
return apiTestCaseRecords;
}
private ApiTestCaseRecord getApiTestCaseRecord(ApiTestCase apiTestCase, ApiReport apiReport) {
ApiTestCaseRecord apiTestCaseRecord = new ApiTestCaseRecord();
apiTestCaseRecord.setApiTestCaseId(apiTestCase.getId());
apiTestCaseRecord.setApiReportId(apiReport.getId());
return apiTestCaseRecord;
}
private ApiReport getApiReport(ApiRunModeConfigDTO runModeConfig, ApiTestCase apiTestCase, String userId) {
ApiReport apiReport = getApiReport(runModeConfig, userId);
apiReport.setEnvironmentId(getEnvId(runModeConfig, apiTestCase));
apiReport.setName(apiTestCase.getName());
apiReport.setProjectId(apiTestCase.getProjectId());
apiReport.setTriggerMode(TaskTriggerMode.BATCH.name());
return apiReport;
}
private ApiReport getApiReport(ApiRunModeConfigDTO runModeConfig, String userId) {
ApiReport apiReport = new ApiReport();
apiReport.setId(IDGenerator.nextStr());
apiReport.setDeleted(false);
apiReport.setIntegrated(false);
public ApiReport getApiReport(ApiRunModeConfigDTO runModeConfig, String userId) {
ApiReport apiReport = apiTestCaseService.getApiReport(userId);
apiReport.setEnvironmentId(runModeConfig.getEnvironmentId());
apiReport.setRunMode(runModeConfig.getRunMode());
apiReport.setStatus(ApiReportStatus.PENDING.name());
apiReport.setStartTime(System.currentTimeMillis());
apiReport.setUpdateTime(System.currentTimeMillis());
apiReport.setTriggerMode(TaskTriggerMode.BATCH.name());
apiReport.setUpdateUser(userId);
apiReport.setCreateUser(userId);
apiReport.setPoolId(runModeConfig.getPoolId());
apiReport.setTriggerMode(TaskTriggerMode.BATCH.name());
return apiReport;
}
@ -369,7 +359,7 @@ public class ApiTestCaseBatchRunService {
* @param apiTestCase
* @return
*/
private String getEnvId(ApiRunModeConfigDTO runModeConfig, ApiTestCase apiTestCase) {
public String getEnvId(ApiRunModeConfigDTO runModeConfig, ApiTestCase apiTestCase) {
return StringUtils.isBlank(runModeConfig.getEnvironmentId()) ? apiTestCase.getEnvironmentId() : runModeConfig.getEnvironmentId();
}

View File

@ -21,9 +21,7 @@ import io.metersphere.project.dto.MoveNodeSortDTO;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.project.service.EnvironmentService;
import io.metersphere.project.service.MoveNodeService;
import io.metersphere.sdk.constants.ApiExecuteRunMode;
import io.metersphere.sdk.constants.ApplicationNumScope;
import io.metersphere.sdk.constants.DefaultRepositoryDir;
import io.metersphere.sdk.constants.*;
import io.metersphere.sdk.domain.Environment;
import io.metersphere.sdk.domain.EnvironmentExample;
import io.metersphere.sdk.dto.api.task.TaskRequestDTO;
@ -99,6 +97,8 @@ public class ApiTestCaseService extends MoveNodeService {
@Resource
private ApiExecuteService apiExecuteService;
@Resource
private ApiReportService apiReportService;
@Resource
private EnvironmentService environmentService;
private static final String CASE_TABLE = "api_test_case";
@ -626,14 +626,19 @@ public class ApiTestCaseService extends MoveNodeService {
return apiFileResourceService.transfer(request, userId, ApiResourceType.API_CASE.name());
}
public TaskRequestDTO run(String id, String reportId) {
public TaskRequestDTO run(String id, String reportId, String userId) {
ApiTestCase apiTestCase = checkResourceExist(id);
ApiTestCaseBlob apiTestCaseBlob = apiTestCaseBlobMapper.selectByPrimaryKey(id);
ApiResourceRunRequest runRequest = new ApiResourceRunRequest();
runRequest.setTestElement(ApiDataUtils.parseObject(new String(apiTestCaseBlob.getRequest()), AbstractMsTestElement.class));
String poolId = apiExecuteService.getProjectApiResourcePoolId(apiTestCase.getProjectId());
// 初始化报告
initApiReport(apiTestCase, reportId, poolId, userId);
TaskRequestDTO taskRequest = getTaskRequest(reportId, apiTestCase.getId(), apiTestCase.getProjectId(), ApiExecuteRunMode.RUN.name());
taskRequest.getRunModeConfig().setPoolId(poolId);
taskRequest.setSaveResult(true);
taskRequest.setRealTime(true);
@ -644,6 +649,56 @@ public class ApiTestCaseService extends MoveNodeService {
return apiExecuteService.apiExecute(runRequest, taskRequest, apiParamConfig);
}
/**
* 预生成用例的执行报告
*
* @param apiTestCase
* @param poolId
* @param userId
* @return
*/
public List<ApiTestCaseRecord> initApiReport(ApiTestCase apiTestCase, String reportId, String poolId, String userId) {
List<ApiReport> apiReports = new ArrayList<>();
List<ApiTestCaseRecord> apiTestCaseRecords = new ArrayList<>();
// 初始化报告
ApiReport apiReport = getApiReport(userId);
apiReport.setId(reportId);
apiReport.setTriggerMode(TaskTriggerMode.MANUAL.name());
apiReports.add(apiReport);
apiReport.setName(apiTestCase.getName());
apiReport.setRunMode(ApiBatchRunMode.PARALLEL.name());
apiReport.setPoolId(poolId);
apiReport.setProjectId(apiTestCase.getProjectId());
// 创建报告和用例的关联关系
ApiTestCaseRecord apiTestCaseRecord = getApiTestCaseRecord(apiTestCase, apiReport);
apiTestCaseRecords.add(apiTestCaseRecord);
apiReportService.insertApiReport(apiReports, apiTestCaseRecords);
return apiTestCaseRecords;
}
public ApiTestCaseRecord getApiTestCaseRecord(ApiTestCase apiTestCase, ApiReport apiReport) {
ApiTestCaseRecord apiTestCaseRecord = new ApiTestCaseRecord();
apiTestCaseRecord.setApiTestCaseId(apiTestCase.getId());
apiTestCaseRecord.setApiReportId(apiReport.getId());
return apiTestCaseRecord;
}
public ApiReport getApiReport(String userId) {
ApiReport apiReport = new ApiReport();
apiReport.setId(IDGenerator.nextStr());
apiReport.setDeleted(false);
apiReport.setIntegrated(false);
apiReport.setStatus(ApiReportStatus.PENDING.name());
apiReport.setStartTime(System.currentTimeMillis());
apiReport.setUpdateTime(System.currentTimeMillis());
apiReport.setUpdateUser(userId);
apiReport.setCreateUser(userId);
return apiReport;
}
public TaskRequestDTO debug(ApiRunRequest request) {
ApiResourceRunRequest runRequest = apiExecuteService.getApiResourceRunRequest(request);

View File

@ -97,7 +97,8 @@ public class ApiTestCaseControllerTests extends BaseTest {
private static final String EXECUTE = "execute/page";
private static final String HISTORY = "operation-history/page";
private static final String DEBUG = "debug";
private static final String RUN = "run/{0}/{1}";
private static final String RUN_REAL_TIME = "run/{0}/{1}";
private static final String RUN = "run/{0}";
private static final String BATCH_RUN = "batch/run";
private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError();
@ -429,10 +430,15 @@ public class ApiTestCaseControllerTests extends BaseTest {
@Test
@Order(3)
public void run() throws Exception {
assertErrorCode(this.requestGet(RUN, apiTestCase.getId(), "111"), ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR);
assertErrorCode(this.requestGet(RUN_REAL_TIME, apiTestCase.getId(), "111"), ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR);
// @@校验权限
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE, RUN, apiTestCase.getId(), "11111");
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE, RUN_REAL_TIME, apiTestCase.getId(), "11111");
assertErrorCode(this.requestGet(RUN, apiTestCase.getId()), ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR);
// @@校验权限
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE, RUN, apiTestCase.getId());
}
@Test