refactor(测试计划): 优化测试计划批量执行方法

This commit is contained in:
Jianguo-Genius 2024-06-13 17:43:21 +08:00 committed by 刘瑞斌
parent 5f0cea3148
commit 672b9fe599
3 changed files with 34 additions and 25 deletions

View File

@ -30,6 +30,8 @@ public class TestPlanExecutionQueue {
private String executionSource; private String executionSource;
//预生成报告ID //预生成报告ID
private String prepareReportId; private String prepareReportId;
// 测试集Json
private String testPlanCollectionJson;
// 是否是队列的最后一个 // 是否是队列的最后一个
private boolean isLastOne = false; private boolean isLastOne = false;

View File

@ -1,5 +1,6 @@
package io.metersphere.plan.service; package io.metersphere.plan.service;
import com.esotericsoftware.minlog.Log;
import io.metersphere.plan.domain.TestPlan; import io.metersphere.plan.domain.TestPlan;
import io.metersphere.plan.domain.TestPlanCollection; import io.metersphere.plan.domain.TestPlanCollection;
import io.metersphere.plan.domain.TestPlanCollectionExample; import io.metersphere.plan.domain.TestPlanCollectionExample;
@ -64,6 +65,10 @@ public class TestPlanExecuteService {
return executeTestPlanOrGroup(executionQueue); return executeTestPlanOrGroup(executionQueue);
} }
private void setRedisForList(String key, List<String> list) {
redisTemplate.opsForList().rightPushAll(key, list);
redisTemplate.expire(key, 1, TimeUnit.DAYS);
}
//批量执行测试计划组 //批量执行测试计划组
public void batchExecuteTestPlan(TestPlanBatchExecuteRequest request, String userId) { public void batchExecuteTestPlan(TestPlanBatchExecuteRequest request, String userId) {
List<String> rightfulIds = testPlanService.selectRightfulIds(request.getExecuteIds()); List<String> rightfulIds = testPlanService.selectRightfulIds(request.getExecuteIds());
@ -94,9 +99,8 @@ public class TestPlanExecuteService {
); );
} }
} }
testPlanExecutionQueues.forEach(testPlanExecutionQueue -> { this.setRedisForList(genQueueKey(queueId, queueType), testPlanExecutionQueues.stream().map(JSON::toJSONString).toList());
redisTemplate.opsForList().rightPush(testPlanExecutionQueue.getQueueType() + testPlanExecutionQueue.getQueueId(), JSON.toJSONString(testPlanExecutionQueue));
});
if (StringUtils.equalsIgnoreCase(request.getRunMode(), ApiBatchRunMode.SERIAL.name())) { if (StringUtils.equalsIgnoreCase(request.getRunMode(), ApiBatchRunMode.SERIAL.name())) {
//串行 //串行
TestPlanExecutionQueue nextQueue = this.getNextQueue(queueId, queueType); TestPlanExecutionQueue nextQueue = this.getNextQueue(queueId, queueType);
@ -143,9 +147,7 @@ public class TestPlanExecuteService {
//本次的测试计划组执行完成 //本次的测试计划组执行完成
this.testPlanGroupQueueFinish(executionQueue.getQueueId(), executionQueue.getQueueType()); this.testPlanGroupQueueFinish(executionQueue.getQueueId(), executionQueue.getQueueType());
} else { } else {
childrenQueue.forEach(childQueue -> { this.setRedisForList(genQueueKey(queueId, queueType), childrenQueue.stream().map(JSON::toJSONString).toList());
redisTemplate.opsForList().rightPush(childQueue.getQueueType() + childQueue.getQueueId(), JSON.toJSONString(childQueue));
});
// todo Song-cc 这里是否要生成测试计划组的集合报告并且记录测试计划里用例的执行信息 // todo Song-cc 这里是否要生成测试计划组的集合报告并且记录测试计划里用例的执行信息
@ -206,10 +208,7 @@ public class TestPlanExecuteService {
//本次的测试计划组执行完成 //本次的测试计划组执行完成
this.testPlanExecuteQueueFinish(executionQueue.getQueueId(), executionQueue.getQueueType()); this.testPlanExecuteQueueFinish(executionQueue.getQueueId(), executionQueue.getQueueType());
} else { } else {
childrenQueue.forEach(childQueue -> { this.setRedisForList(genQueueKey(queueId, queueType), childrenQueue.stream().map(JSON::toJSONString).toList());
redisTemplate.opsForList().rightPush(childQueue.getQueueType() + childQueue.getQueueId(), JSON.toJSONString(childQueue));
});
// todo Song-cc 这里是否要生成测试计划报告并且记录测试计划里用例的执行信息 // todo Song-cc 这里是否要生成测试计划报告并且记录测试计划里用例的执行信息
//开始根据测试计划集合执行测试用例 //开始根据测试计划集合执行测试用例
@ -253,16 +252,16 @@ public class TestPlanExecuteService {
collection.getId(), collection.getId(),
collection.getExecuteMethod(), collection.getExecuteMethod(),
executionQueue.getExecutionSource(), executionQueue.getExecutionSource(),
IDGenerator.nextStr()) IDGenerator.nextStr()) {{
this.setTestPlanCollectionJson(JSON.toJSONString(collection));
}}
); );
} }
if (CollectionUtils.isEmpty(childrenQueue)) { if (CollectionUtils.isEmpty(childrenQueue)) {
//本次的测试集执行完成 //本次的测试集执行完成
this.caseTypeExecuteQueueFinish(executionQueue.getQueueId(), executionQueue.getQueueType()); this.caseTypeExecuteQueueFinish(executionQueue.getQueueId(), executionQueue.getQueueType());
} else { } else {
childrenQueue.forEach(childQueue -> { this.setRedisForList(genQueueKey(queueId, queueType), childrenQueue.stream().map(JSON::toJSONString).toList());
redisTemplate.opsForList().rightPush(childQueue.getQueueType() + childQueue.getQueueId(), JSON.toJSONString(childQueue));
});
if (StringUtils.equalsIgnoreCase(parentCollection.getExecuteMethod(), ApiBatchRunMode.SERIAL.name())) { if (StringUtils.equalsIgnoreCase(parentCollection.getExecuteMethod(), ApiBatchRunMode.SERIAL.name())) {
//串行 //串行
TestPlanExecutionQueue nextQueue = this.getNextQueue(queueId, queueType); TestPlanExecutionQueue nextQueue = this.getNextQueue(queueId, queueType);
@ -281,20 +280,28 @@ public class TestPlanExecuteService {
private void executeCase(TestPlanExecutionQueue testPlanExecutionQueue) { private void executeCase(TestPlanExecutionQueue testPlanExecutionQueue) {
String queueId = testPlanExecutionQueue.getQueueId(); String queueId = testPlanExecutionQueue.getQueueId();
String queueType = testPlanExecutionQueue.getQueueType(); String queueType = testPlanExecutionQueue.getQueueType();
TestPlanCollection collection = testPlanCollectionMapper.selectByPrimaryKey(testPlanExecutionQueue.getSourceID());
try {
TestPlanCollection collection = JSON.parseObject(testPlanExecutionQueue.getTestPlanCollectionJson(), TestPlanCollection.class);
String runMode = collection.getExecuteMethod(); String runMode = collection.getExecuteMethod();
String environmentId = collection.getEnvironmentId(); String environmentId = collection.getEnvironmentId();
if (StringUtils.equalsIgnoreCase(collection.getType(), CaseType.API_CASE.getKey())) { if (StringUtils.equalsIgnoreCase(collection.getType(), CaseType.API_CASE.getKey())) {
// todo 执行API用例 // todo 执行API用例 预生成报告时会将要执行的用例记录在test_plan_report_api_case表中通过它查询
} else if (StringUtils.equalsIgnoreCase(collection.getType(), CaseType.SCENARIO_CASE.getKey())) { } else if (StringUtils.equalsIgnoreCase(collection.getType(), CaseType.SCENARIO_CASE.getKey())) {
// todo 执行场景用例 // todo 执行场景用例 预生成报告时会将要执行的用例记录在test_plan_report_api_scenario表中通过它查询
} }
//执行完成之后需要回调 collectionExecuteQueueFinish //执行完成之后需要回调 collectionExecuteQueueFinish
//如果没有要执行的用例可能会出现空测试集的情况直接调用collectionExecuteQueueFinish(queueId)
} catch (Exception e) {
Log.error("按测试集执行失败!", e);
collectionExecuteQueueFinish(queueId);
}
} }
//测试集执行完成 //测试集执行完成
public void collectionExecuteQueueFinish(String queueID, String queueType) { public void collectionExecuteQueueFinish(String queueID) {
String queueType = QUEUE_PREFIX_TEST_PLAN_COLLECTION;
TestPlanExecutionQueue nextQueue = getNextQueue(queueID, queueType); TestPlanExecutionQueue nextQueue = getNextQueue(queueID, queueType);
if (StringUtils.equalsIgnoreCase(nextQueue.getRunMode(), ApiBatchRunMode.SERIAL.name())) { if (StringUtils.equalsIgnoreCase(nextQueue.getRunMode(), ApiBatchRunMode.SERIAL.name())) {
//串行时由于是先拿出节点再判断执行所以要判断节点的isExecuteFinish //串行时由于是先拿出节点再判断执行所以要判断节点的isExecuteFinish
@ -302,7 +309,7 @@ public class TestPlanExecuteService {
try { try {
this.executeNextNode(nextQueue); this.executeNextNode(nextQueue);
} catch (Exception e) { } catch (Exception e) {
this.collectionExecuteQueueFinish(nextQueue.getQueueId(), nextQueue.getQueueType()); this.collectionExecuteQueueFinish(nextQueue.getQueueId());
} }
} else { } else {
//当前测试集执行完毕 //当前测试集执行完毕

View File

@ -270,7 +270,7 @@ public class TestPlanExecuteTests extends BaseTest {
String collectionFinishQueueIds = collectionQueueIdList.getFirst(); String collectionFinishQueueIds = collectionQueueIdList.getFirst();
//模拟执行完成之后的回调 //模拟执行完成之后的回调
testPlanExecuteService.collectionExecuteQueueFinish(collectionFinishQueueIds, QUEUE_PREFIX_TEST_PLAN_COLLECTION); testPlanExecuteService.collectionExecuteQueueFinish(collectionFinishQueueIds);
allQueueIds = new ArrayList<>(); allQueueIds = new ArrayList<>();
collectionQueueIdList = new ArrayList<>(); collectionQueueIdList = new ArrayList<>();