fix (接口自动化): 修复集合报告 场景后执行状态错误问题#1005605

--bug=1005605 --user=赵勇 【接口自动化】场景列... https://www.tapd.cn/55049933/s/1031760
This commit is contained in:
fit2-zhao 2021-08-03 15:57:08 +08:00 committed by fit2-zhao
parent 1cadd7b4ea
commit a3ac7d5c59
4 changed files with 59 additions and 72 deletions

View File

@ -811,7 +811,7 @@ public class ApiAutomationService {
return null;
}
public APIScenarioReportResult createScenarioReport(String id, String scenarioId, String scenarioName, String triggerMode, String execType, String projectId, String userID, RunModeConfig config) {
public APIScenarioReportResult createScenarioReport(String id, String scenarioId, String scenarioName, String triggerMode, String execType, String projectId, String userID, RunModeConfig config,String desc) {
APIScenarioReportResult report = new APIScenarioReportResult();
if (triggerMode.equals(ApiRunMode.SCENARIO.name()) || triggerMode.equals(ApiRunMode.DEFINITION.name())) {
triggerMode = ReportTriggerMode.MANUAL.name();
@ -844,7 +844,7 @@ public class ApiAutomationService {
report.setProjectId(projectId);
report.setScenarioName(scenarioName);
report.setScenarioId(scenarioId);
report.setDescription(desc);
return report;
}
@ -1027,14 +1027,14 @@ public class ApiAutomationService {
if (request.isTestPlanScheduleJob()) {
String savedScenarioId = testPlanScenarioId + ":" + request.getTestPlanReportId();
report = createScenarioReport(reportId, savedScenarioId, item.getName(), request.getTriggerMode(),
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig());
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId());
} else {
report = createScenarioReport(reportId, testPlanScenarioId, item.getName(), request.getTriggerMode(),
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig());
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId());
}
} else {
report = createScenarioReport(reportId, ExecuteType.Marge.name().equals(request.getExecuteType()) ? serialReportId : item.getId(), item.getName(), request.getTriggerMode(),
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig());
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId());
}
try {
if (request.getConfig() != null && StringUtils.isNotBlank(request.getConfig().getResourcePoolId())) {
@ -1058,7 +1058,7 @@ public class ApiAutomationService {
APIScenarioReportResult report = createScenarioReport(request.getConfig().getReportId(),
JSON.toJSONString(CollectionUtils.isNotEmpty(scenarioIds) && scenarioIds.size() > 50 ? scenarioIds.subList(0, 50) : scenarioIds),
scenarioNames.length() >= 3000 ? scenarioNames.substring(0, 2000) : scenarioNames.deleteCharAt(scenarioNames.toString().length() - 1).toString(),
ReportTriggerMode.MANUAL.name(), ExecuteType.Saved.name(), request.getProjectId(), request.getReportUserID(), request.getConfig());
ReportTriggerMode.MANUAL.name(), ExecuteType.Saved.name(), request.getProjectId(), request.getReportUserID(), request.getConfig(),JSON.toJSONString(scenarioIds));
report.setName(request.getConfig().getReportName());
report.setId(serialReportId);
@ -1249,14 +1249,14 @@ public class ApiAutomationService {
if (request.isTestPlanScheduleJob()) {
String savedScenarioId = testPlanScenarioId + ":" + request.getTestPlanReportId();
report = createScenarioReport(group.getName(), savedScenarioId, item.getName(), request.getTriggerMode(),
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig());
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId());
} else {
report = createScenarioReport(group.getName(), testPlanScenarioId, item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(),
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig());
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId());
}
} else {
report = createScenarioReport(group.getName(), item.getId(), item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(),
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig());
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(),item.getId());
}
batchMapper.insert(report);
reportIds.add(group.getName());
@ -1473,7 +1473,7 @@ public class ApiAutomationService {
}
APIScenarioReportResult report = createScenarioReport(request.getId(), request.getScenarioId(), request.getScenarioName(), ReportTriggerMode.MANUAL.name(), request.getExecuteType(), request.getProjectId(),
SessionUtils.getUserId(), request.getConfig());
SessionUtils.getUserId(), request.getConfig(),request.getId());
apiScenarioReportMapper.insert(report);
uploadBodyFiles(request.getBodyFileRequestIds(), bodyFiles);

View File

@ -254,7 +254,7 @@ public class ApiScenarioReportService {
return returnReport;
}
public ApiScenarioReport updateSchedulePlanCase(TestResult result, String runMode) {
public ApiScenarioReport updateSchedulePlanCase(TestResult result, String runMode) {
ApiScenarioReport lastReport = null;
List<ScenarioResult> scenarioResultList = result.getScenarios();
@ -264,7 +264,7 @@ public class ApiScenarioReportService {
List<String> reportIds = new ArrayList<>();
List<String> scenarioIdList = new ArrayList<>();
Map<String,String> scenarioAndErrorMap = new HashMap<>();
Map<String, String> scenarioAndErrorMap = new HashMap<>();
for (ScenarioResult scenarioResult : scenarioResultList) {
// 存储场景报告
@ -332,10 +332,10 @@ public class ApiScenarioReportService {
}
TestPlanReportService testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class);
testPlanLog.info("TestPlanReportId"+ JSONArray.toJSONString(testPlanReportIdList) +" EXECUTE OVER. SCENARIO STATUS : "+JSONObject.toJSONString(scenarioAndErrorMap));
testPlanLog.info("TestPlanReportId" + JSONArray.toJSONString(testPlanReportIdList) + " EXECUTE OVER. SCENARIO STATUS : " + JSONObject.toJSONString(scenarioAndErrorMap));
for (String planId :testPlanReportIdList) {
testPlanReportService.updateExecuteApis(planId,null,scenarioAndErrorMap,null);
for (String planId : testPlanReportIdList) {
testPlanReportService.updateExecuteApis(planId, null, scenarioAndErrorMap, null);
}
return lastReport;
@ -390,7 +390,8 @@ public class ApiScenarioReportService {
List<ApiScenarioReportDetail> details = apiScenarioReportDetailMapper.selectByExampleWithBLOBs(example);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 记录单场景通过率
Map<String, String> passRateMap = new HashMap<>();
for (ApiScenarioReportDetail detail : details) {
try {
String content = new String(detail.getContent(), StandardCharsets.UTF_8);
@ -409,6 +410,8 @@ public class ApiScenarioReportService {
testResult.setScenarioStepError(scenarioResult.getScenarioStepError() + testResult.getScenarioStepError());
testResult.setScenarioStepSuccess(scenarioResult.getScenarioStepSuccess() + testResult.getScenarioStepSuccess());
testResult.setScenarioStepTotal(scenarioResult.getScenarioStepTotal() + testResult.getScenarioStepTotal());
String passRate = new DecimalFormat("0%").format((float) scenarioResult.getSuccess() / (scenarioResult.getSuccess() + scenarioResult.getError()));
passRateMap.put(detail.getReportId(), passRate);
} catch (Exception e) {
LogUtil.error(e.getMessage());
}
@ -430,32 +433,25 @@ public class ApiScenarioReportService {
apiScenarioReportDetailMapper.insert(detail);
}
// 更新场景状态
if (StringUtils.isNotEmpty(report.getScenarioId())) {
List<String> strings = JSON.parseObject(report.getScenarioId(), List.class);
ApiScenarioExample scenarioExample = new ApiScenarioExample();
scenarioExample.createCriteria().andIdIn(strings);
List<ApiScenario> scenarios = apiScenarioMapper.selectByExample(scenarioExample);
if (CollectionUtils.isNotEmpty(scenarios)) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiScenarioMapper scenarioReportMapper = sqlSession.getMapper(ApiScenarioMapper.class);
scenarios.forEach(scenario -> {
if (testResult.getError() > 0) {
scenario.setLastResult("Fail");
} else {
scenario.setLastResult("Success");
}
String passRate = new DecimalFormat("0%").format((float) testResult.getSuccess() / (testResult.getSuccess() + testResult.getError()));
scenario.setPassRate(passRate);
scenario.setReportId(report.getId());
scenarioReportMapper.updateByPrimaryKey(scenario);
});
sqlSession.flushStatements();
}
if (CollectionUtils.isNotEmpty(reportIds)) {
ApiScenarioReportExample scenarioReportExample = new ApiScenarioReportExample();
scenarioReportExample.createCriteria().andIdIn(reportIds);
List<ApiScenarioReport> reports = apiScenarioReportMapper.selectByExampleWithBLOBs(scenarioReportExample);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiScenarioMapper scenarioReportMapper = sqlSession.getMapper(ApiScenarioMapper.class);
reports.forEach(apiScenarioReport -> {
ApiScenarioWithBLOBs scenario = new ApiScenarioWithBLOBs();
scenario.setId(apiScenarioReport.getDescription());
scenario.setLastResult(StringUtils.equals("Error", apiScenarioReport.getStatus()) ? "Fail" : apiScenarioReport.getStatus());
scenario.setPassRate(passRateMap.get(apiScenarioReport.getId()));
scenario.setReportId(report.getId());
scenarioReportMapper.updateByPrimaryKeySelective(scenario);
});
sqlSession.flushStatements();
}
// 清理其他报告保留一份合并后的报告
this.deleteByIds(reportIds);
passRateMap.clear();
deleteByIds(reportIds);
}
}

View File

@ -23,7 +23,6 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -68,7 +67,7 @@ public class TestResultService {
if (StringUtils.isBlank(debugReportId)) {
apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.DEFINITION.name(), TriggerMode.MANUAL.name());
}
//jenkins单接口执行
//jenkins单接口执行
} else if (StringUtils.equals(runMode, ApiRunMode.JENKINS.name())) {
apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.DEFINITION.name(), TriggerMode.API.name());
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.getInfoJenkins(testResult.getTestId());
@ -90,9 +89,9 @@ public class TestResultService {
} else if (StringUtils.equalsAny(runMode, ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) {
//测试计划定时任务-接口执行逻辑的话需要同步测试计划的报告数据
if (StringUtils.equals(runMode, ApiRunMode.SCHEDULE_API_PLAN.name())) {
apiDefinitionExecResultService.saveApiResultByScheduleTask(testResult,debugReportId, ApiRunMode.SCHEDULE_API_PLAN.name(),ReportTriggerMode.SCHEDULE.name());
apiDefinitionExecResultService.saveApiResultByScheduleTask(testResult, debugReportId, ApiRunMode.SCHEDULE_API_PLAN.name(), ReportTriggerMode.SCHEDULE.name());
} else if (StringUtils.equals(runMode, ApiRunMode.JENKINS_API_PLAN.name())) {
apiDefinitionExecResultService.saveApiResultByScheduleTask(testResult,debugReportId, ApiRunMode.JENKINS_API_PLAN.name(),ReportTriggerMode.API.name());
apiDefinitionExecResultService.saveApiResultByScheduleTask(testResult, debugReportId, ApiRunMode.JENKINS_API_PLAN.name(), ReportTriggerMode.API.name());
} else {
apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.API_PLAN.name(), TriggerMode.MANUAL.name());
}
@ -103,6 +102,10 @@ public class TestResultService {
//环境
ApiScenarioWithBLOBs apiScenario = apiAutomationService.getDto(scenarioReport.getScenarioId());
String name = "";
//执行人
String userName = "";
//负责人
String principal = "";
if (apiScenario != null) {
String executionEnvironment = apiScenario.getScenarioDefinition();
JSONObject json = JSONObject.parseObject(executionEnvironment);
@ -111,11 +114,9 @@ public class TestResultService {
String environmentId = environment.get(apiScenario.getProjectId()).toString();
name = apiAutomationService.get(environmentId).getName();
}
userName = apiAutomationService.getUser(apiScenario.getUserId());
principal = apiAutomationService.getUser(apiScenario.getPrincipal());
}
//执行人
String userName = apiAutomationService.getUser(apiScenario.getUserId());
//负责人
String principal = apiAutomationService.getUser(apiScenario.getPrincipal());
//报告内容
reportTask = new ApiTestReportVariable();
reportTask.setStatus(scenarioReport.getStatus());

View File

@ -37,7 +37,6 @@ import io.metersphere.service.SystemParameterService;
import io.metersphere.track.Factory.ReportComponentFactory;
import io.metersphere.track.domain.ReportComponent;
import io.metersphere.track.dto.*;
import io.metersphere.track.request.report.TestPlanReportSaveRequest;
import io.metersphere.track.request.testcase.PlanCaseRelevanceRequest;
import io.metersphere.track.request.testcase.QueryTestPlanRequest;
import io.metersphere.track.request.testplan.AddTestPlanRequest;
@ -53,7 +52,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
@ -462,7 +460,7 @@ public class TestPlanService {
testCaseExample.createCriteria().andIdIn(testCaseIds);
List<TestCase> testCaseList = testCaseMapper.selectByExample(testCaseExample);
Map<String, String> userMap = testCaseList.stream()
.collect(HashMap::new, (m,v)-> m.put(v.getId(), v.getMaintainer()), HashMap::putAll);
.collect(HashMap::new, (m, v) -> m.put(v.getId(), v.getMaintainer()), HashMap::putAll);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
TestPlanTestCaseMapper batchMapper = sqlSession.getMapper(TestPlanTestCaseMapper.class);
@ -849,7 +847,7 @@ public class TestPlanService {
Comparator.comparing(Issues::getCreateTime, (t1, t2) -> {
if (t1 == null) {
return 1;
} else if (t2 == null) {
} else if (t2 == null) {
return -1;
}
return t2.compareTo(t1);
@ -1001,7 +999,7 @@ public class TestPlanService {
APIScenarioReportResult report = apiAutomationService.createScenarioReport(group.getName(),
planScenarioID + ":" + request.getTestPlanReportId(),
item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(),
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig());
request.getExecuteType(), item.getProjectId(), request.getReportUserID(), request.getConfig(), null);
apiScenarioReportMapper.insert(report);
group.setHashTree(scenarios);
testPlan.getHashTree().add(group);
@ -1011,16 +1009,8 @@ public class TestPlanService {
}
public String run(String testPlanID, String projectID, String userId, String triggerMode, String apiRunConfig) {
if (StringUtils.isEmpty(apiRunConfig)) {
apiRunConfig =
"{\"mode\":\"parallel\"," +
"\"reportType\":\"iddReport\"," +
"\"onSampleError\":true," +
"\"runWithinResourcePool\":true," +
"\"resourcePoolId\":\"29773f4f-55e4-4bce-ad3d-b531b4eb59c2\"}";
}
//创建测试报告然后返回的ID重新赋值为resourceID作为后续的参数
TestPlanScheduleReportInfoDTO reportInfoDTO = testPlanReportService.genTestPlanReportBySchedule(projectID,testPlanID,userId,triggerMode);
TestPlanScheduleReportInfoDTO reportInfoDTO = testPlanReportService.genTestPlanReportBySchedule(projectID, testPlanID, userId, triggerMode);
TestPlanReport testPlanReport = reportInfoDTO.getTestPlanReport();
Map<String, String> planScenarioIdMap = reportInfoDTO.getPlanScenarioIdMap();
@ -1029,12 +1019,12 @@ public class TestPlanService {
String planReportId = testPlanReport.getId();
testPlanLog.info("ReportId["+planReportId+"] created. TestPlanID:["+testPlanID+"]. ");
testPlanLog.info("ReportId[" + planReportId + "] created. TestPlanID:[" + testPlanID + "]. ");
//不同任务的执行ID
Map<String,String> executePerformanceIdMap = new HashMap<>();
Map<String,String> executeApiCaseIdMap = new HashMap<>();
Map<String,String> executeScenarioCaseIdMap = new HashMap<>();
Map<String, String> executePerformanceIdMap = new HashMap<>();
Map<String, String> executeApiCaseIdMap = new HashMap<>();
Map<String, String> executeScenarioCaseIdMap = new HashMap<>();
//执行性能测试任务
List<String> performaneReportIDList = new ArrayList<>();
@ -1070,7 +1060,7 @@ public class TestPlanService {
e.printStackTrace();
}
if (StringUtils.isNotEmpty(reportId)) {
executePerformanceIdMap.put(caseID,TestPlanApiExecuteStatus.RUNNING.name());
executePerformanceIdMap.put(caseID, TestPlanApiExecuteStatus.RUNNING.name());
}
}
if (!performaneReportIDList.isEmpty()) {
@ -1081,13 +1071,13 @@ public class TestPlanService {
for (Map.Entry<String, String> entry : apiTestCaseIdMap.entrySet()) {
String apiCaseID = entry.getKey();
executeApiCaseIdMap.put(apiCaseID,TestPlanApiExecuteStatus.RUNNING.name());
executeApiCaseIdMap.put(apiCaseID, TestPlanApiExecuteStatus.RUNNING.name());
}
for (String id : planScenarioIdMap.keySet()) {
executeScenarioCaseIdMap.put(id,TestPlanApiExecuteStatus.RUNNING.name());
executeScenarioCaseIdMap.put(id, TestPlanApiExecuteStatus.RUNNING.name());
}
testPlanLog.info("ReportId["+planReportId+"] start run. TestPlanID:["+testPlanID+"]. Execute api :"+JSONObject.toJSONString(executeApiCaseIdMap)+"; Execute scenario:"+JSONObject.toJSONString(executeScenarioCaseIdMap)+"; Execute performance:"+JSONObject.toJSONString(executePerformanceIdMap));
testPlanReportService.updateExecuteApis(planReportId,executeApiCaseIdMap,executeScenarioCaseIdMap,executePerformanceIdMap);
testPlanLog.info("ReportId[" + planReportId + "] start run. TestPlanID:[" + testPlanID + "]. Execute api :" + JSONObject.toJSONString(executeApiCaseIdMap) + "; Execute scenario:" + JSONObject.toJSONString(executeScenarioCaseIdMap) + "; Execute performance:" + JSONObject.toJSONString(executePerformanceIdMap));
testPlanReportService.updateExecuteApis(planReportId, executeApiCaseIdMap, executeScenarioCaseIdMap, executePerformanceIdMap);
//执行接口案例任务
@ -1101,7 +1091,7 @@ public class TestPlanService {
} else {
apiTestCaseService.run(blobs, UUID.randomUUID().toString(), planReportId, testPlanID, ApiRunMode.SCHEDULE_API_PLAN.name());
}
executeApiCaseIdMap.put(apiCaseID,TestPlanApiExecuteStatus.RUNNING.name());
executeApiCaseIdMap.put(apiCaseID, TestPlanApiExecuteStatus.RUNNING.name());
}
//执行场景执行任务