From 2c7a65a5ec73caf71647f1b036b8b5797b5aeddf Mon Sep 17 00:00:00 2001 From: "song.tianyang" Date: Thu, 21 Jan 2021 18:35:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=AE=A1=E5=88=92=E4=B8=AD=E6=89=A7=E8=A1=8C=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E6=A1=88=E4=BE=8B=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复测试计划中执行场景案例失败的问题 --- .../dto/automation/ApiScenarioRequest.java | 1 + .../dto/automation/RunScenarioRequest.java | 3 + .../api/service/ApiAutomationService.java | 15 ++++- .../api/service/ApiScenarioReportService.java | 56 +++++++++++-------- .../base/mapper/ext/ExtApiScenarioMapper.xml | 8 ++- .../job/sechedule/TestPlanTestJob.java | 11 +--- .../service/TestPlanScenarioCaseService.java | 29 ++++++---- .../track/service/TestPlanService.java | 2 +- 8 files changed, 76 insertions(+), 49 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenarioRequest.java b/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenarioRequest.java index ad1f08d27f..2eb30e0f64 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenarioRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenarioRequest.java @@ -27,4 +27,5 @@ public class ApiScenarioRequest { private boolean isSelectThisWeedData; private long createTime = 0; private String executeStatus; + private boolean notInTestPlan; } diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/RunScenarioRequest.java b/backend/src/main/java/io/metersphere/api/dto/automation/RunScenarioRequest.java index ff4f5128c5..83aff1371a 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/RunScenarioRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/RunScenarioRequest.java @@ -4,6 +4,7 @@ import lombok.Getter; import lombok.Setter; import java.util.List; +import java.util.Map; @Setter @Getter @@ -32,6 +33,8 @@ public class RunScenarioRequest { private List scenarioIds; + private Map scenarioTestPlanIdMap; + /** * isSelectAllDate:选择的数据是否是全部数据(全部数据是不受分页影响的数据) * filters: 数据状态 diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 3b990d587a..807dd9c61f 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -397,8 +397,19 @@ public class ApiAutomationService { scenarios.add(scenario); // 创建场景报告 if (reportIds != null) { - createScenarioReport(group.getName(), item.getId(), item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(), - request.getExecuteType(), item.getProjectId(), request.getReportUserID()); + //如果是测试计划页面触发的执行方式,生成报告时createScenarioReport第二个参数需要特殊处理 + if(StringUtils.equals(request.getRunMode(),ApiRunMode.SCENARIO_PLAN.name())){ + String testPlanScenarioId = item.getId(); + if(request.getScenarioTestPlanIdMap()!=null&&request.getScenarioTestPlanIdMap().containsKey(item.getId())){ + testPlanScenarioId = request.getScenarioTestPlanIdMap().get(item.getId()); + } + createScenarioReport(group.getName(), testPlanScenarioId, item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(), + request.getExecuteType(), item.getProjectId(), request.getReportUserID()); + }else{ + createScenarioReport(group.getName(), item.getId(), item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(), + request.getExecuteType(), item.getProjectId(), request.getReportUserID()); + } + reportIds.add(group.getName()); } group.setHashTree(scenarios); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java index 6f811c32da..f30550e488 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -134,31 +134,41 @@ public class ApiScenarioReportService { } public ApiScenarioReport updatePlanCase(TestResult result) { - TestPlanApiScenario testPlanApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(result.getTestId()); - ScenarioResult scenarioResult = result.getScenarios().get(0); - if (scenarioResult.getError() > 0) { - testPlanApiScenario.setLastResult(ScenarioStatus.Fail.name()); - } else { - testPlanApiScenario.setLastResult(ScenarioStatus.Success.name()); +// TestPlanApiScenario testPlanApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(result.getTestId()); + List scenarioResultList = result.getScenarios(); + ApiScenarioReport returnReport = null; + for (ScenarioResult scenarioResult : + scenarioResultList) { + ApiScenarioReport report = editReport(scenarioResult); + // 报告详情内容 + ApiScenarioReportDetail detail = new ApiScenarioReportDetail(); + TestResult newResult = createTestResult(result.getTestId(), scenarioResult); + scenarioResult.setName(report.getScenarioName()); + newResult.addScenario(scenarioResult); + + detail.setContent(JSON.toJSONString(newResult).getBytes(StandardCharsets.UTF_8)); + detail.setReportId(report.getId()); + detail.setProjectId(report.getProjectId()); + apiScenarioReportDetailMapper.insert(detail); + + TestPlanApiScenario testPlanApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(report.getScenarioId()); + if(testPlanApiScenario!=null){ + report.setScenarioId(testPlanApiScenario.getApiScenarioId()); + apiScenarioReportMapper.updateByPrimaryKeySelective(report); + if (scenarioResult.getError() > 0) { + testPlanApiScenario.setLastResult(ScenarioStatus.Fail.name()); + } else { + testPlanApiScenario.setLastResult(ScenarioStatus.Success.name()); + } + String passRate = new DecimalFormat("0%").format((float) scenarioResult.getSuccess() / (scenarioResult.getSuccess() + scenarioResult.getError())); + testPlanApiScenario.setPassRate(passRate); + testPlanApiScenario.setReportId(report.getId()); + testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario); + } + returnReport = report; } - String passRate = new DecimalFormat("0%").format((float) scenarioResult.getSuccess() / (scenarioResult.getSuccess() + scenarioResult.getError())); - testPlanApiScenario.setPassRate(passRate); - // 存储场景报告 - ApiScenarioReport report = editReport(scenarioResult); - // 报告详情内容 - ApiScenarioReportDetail detail = new ApiScenarioReportDetail(); - TestResult newResult = createTestResult(result.getTestId(), scenarioResult); - scenarioResult.setName(report.getScenarioName()); - newResult.addScenario(scenarioResult); - detail.setContent(JSON.toJSONString(newResult).getBytes(StandardCharsets.UTF_8)); - detail.setReportId(report.getId()); - detail.setProjectId(report.getProjectId()); - apiScenarioReportDetailMapper.insert(detail); - - testPlanApiScenario.setReportId(report.getId()); - testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario); - return report; + return returnReport; } public ApiScenarioReport updateSchedulePlanCase(TestResult result) { diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml index aad982b1f3..c0089a1413 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml @@ -164,7 +164,13 @@ and api_scenario.last_result = 'Success' - + + and api_scenario.id not in ( + select pc.api_scenario_id + from test_plan_api_scenario pc + where pc.test_plan_id = #{request.planId} + ) + order by diff --git a/backend/src/main/java/io/metersphere/job/sechedule/TestPlanTestJob.java b/backend/src/main/java/io/metersphere/job/sechedule/TestPlanTestJob.java index a2f87e9011..4067a6981f 100644 --- a/backend/src/main/java/io/metersphere/job/sechedule/TestPlanTestJob.java +++ b/backend/src/main/java/io/metersphere/job/sechedule/TestPlanTestJob.java @@ -1,12 +1,7 @@ package io.metersphere.job.sechedule; import io.metersphere.api.dto.automation.ExecuteType; -import io.metersphere.api.dto.automation.RunScenarioRequest; import io.metersphere.api.dto.automation.SchedulePlanScenarioExecuteRequest; -import io.metersphere.api.dto.definition.RunCaseRequest; -import io.metersphere.api.dto.definition.RunDefinitionRequest; -import io.metersphere.api.service.ApiAutomationService; -import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiTestCaseService; import io.metersphere.base.domain.*; import io.metersphere.commons.constants.ApiRunMode; @@ -19,13 +14,9 @@ import io.metersphere.track.dto.TestPlanLoadCaseDTO; import io.metersphere.track.request.testplan.LoadCaseRequest; import io.metersphere.track.request.testplan.RunTestPlanRequest; import io.metersphere.track.service.*; -import org.python.antlr.ast.Str; import org.quartz.*; -import javax.annotation.Resource; import java.util.*; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; /** * 情景测试Job @@ -130,7 +121,7 @@ public class TestPlanTestJob extends MsScheduleJob { scenarioRequest.setTestPlanID(this.resourceId); scenarioRequest.setRunMode(ApiRunMode.SCHEDULE_SCENARIO_PLAN.name()); scenarioRequest.setTestPlanReportId(testPlanReport.getId()); - testPlanService.runApiCase(scenarioRequest); + testPlanService.runScenarioCase(scenarioRequest); LogUtil.info("-------------- testplan schedule ---------- scenario case over -----------------"); //执行性能测试任务 diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java index 683fe4cee1..2bd865191b 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java @@ -22,9 +22,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; @Service @@ -51,11 +49,12 @@ public class TestPlanScenarioCaseService { } public List relevanceList(ApiScenarioRequest request) { - List ids = apiAutomationService.selectIdsNotExistsInPlan(request.getProjectId(), request.getPlanId()); - if (CollectionUtils.isEmpty(ids)) { - return new ArrayList<>(); - } - request.setIds(ids); +// List ids = apiAutomationService.selectIdsNotExistsInPlan(request.getProjectId(), request.getPlanId()); +// if (CollectionUtils.isEmpty(ids)) { +// return new ArrayList<>(); +// } +// request.setIds(ids); + request.setNotInTestPlan(true); return apiAutomationService.list(request); } @@ -88,11 +87,17 @@ public class TestPlanScenarioCaseService { public String run(RunScenarioRequest request) { TestPlanApiScenarioExample example = new TestPlanApiScenarioExample(); example.createCriteria().andIdIn(request.getPlanCaseIds()); - List scenarioIds = testPlanApiScenarioMapper.selectByExample(example).stream() - .map(TestPlanApiScenario::getApiScenarioId) - .collect(Collectors.toList()); - scenarioIds.addAll(scenarioIds); + List testPlanApiScenarioList = testPlanApiScenarioMapper.selectByExample(example); + + List scenarioIds = new ArrayList<>(); + Map scenarioIdApiScarionMap = new HashMap<>(); + for (TestPlanApiScenario apiScenario: + testPlanApiScenarioList) { + scenarioIds.add(apiScenario.getApiScenarioId()); + scenarioIdApiScarionMap.put(apiScenario.getApiScenarioId(),apiScenario.getId()); + } request.setScenarioIds(scenarioIds); + request.setScenarioTestPlanIdMap(scenarioIdApiScarionMap); request.setRunMode(ApiRunMode.SCENARIO_PLAN.name()); return apiAutomationService.run(request); } diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java index 76843554ae..69fe39c2c7 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java @@ -751,7 +751,7 @@ public class TestPlanService { * @param request * @return */ - public String runApiCase(SchedulePlanScenarioExecuteRequest request) { + public String runScenarioCase(SchedulePlanScenarioExecuteRequest request) { MsTestPlan testPlan = new MsTestPlan(); testPlan.setHashTree(new LinkedList<>()); HashTree jmeterHashTree = new ListedHashTree();