feat(接口测试): 处理环境和场景断言

This commit is contained in:
AgAngle 2024-02-05 13:41:01 +08:00 committed by Craftsman
parent fa94637dc7
commit 4872c1827f
9 changed files with 111 additions and 25 deletions

View File

@ -60,4 +60,14 @@ public class ApiParamConfig extends ParameterConfig {
String pluginId = testElementClassPluginIdMap.get(msTestElement.getClass()); String pluginId = testElementClassPluginIdMap.get(msTestElement.getClass());
return pluginConfigMap.get(pluginId); return pluginConfigMap.get(pluginId);
} }
/**
* 获取当前环境配置
* @param projectId
* @return
*/
public EnvironmentInfoDTO getEnvConfig(String projectId) {
return getEnvConfig();
}
} }

View File

@ -40,6 +40,7 @@ public class ApiScenarioParamConfig extends ApiParamConfig {
* @param projectId * @param projectId
* @return * @return
*/ */
@Override
public EnvironmentInfoDTO getEnvConfig(String projectId) { public EnvironmentInfoDTO getEnvConfig(String projectId) {
if (BooleanUtils.isTrue(grouped)) { if (BooleanUtils.isTrue(grouped)) {
return projectEnvMap.get(projectId); return projectEnvMap.get(projectId);

View File

@ -15,9 +15,9 @@ import java.util.List;
public class MsAssertionConfig { public class MsAssertionConfig {
/** /**
* 是否启用全局断言 * 是否启用全局断言
* 默认为 false * 默认为 true
*/ */
private Boolean enableGlobal = false; private Boolean enableGlobal = true;
/** /**
* 断言列表 * 断言列表
*/ */

View File

@ -0,0 +1,20 @@
package io.metersphere.api.dto.assertion;
import io.metersphere.project.api.assertion.MsAssertion;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* 断言设置
* @Author: jianxing
* @CreateTime: 2023-11-23 17:26
*/
@Data
public class MsScenarioAssertionConfig {
/**
* 断言列表
*/
private List<MsAssertion> assertions = new ArrayList<>(0);
}

View File

@ -22,16 +22,16 @@ public class MsCommonElement extends AbstractMsTestElement {
* 前置处理器配置 * 前置处理器配置
*/ */
@Valid @Valid
private MsProcessorConfig preProcessorConfig; private MsProcessorConfig preProcessorConfig = new MsProcessorConfig();
/** /**
* 后置处理器配置 * 后置处理器配置
*/ */
@Valid @Valid
private MsProcessorConfig postProcessorConfig; private MsProcessorConfig postProcessorConfig = new MsProcessorConfig();
/** /**
* 断言配置 * 断言配置
*/ */
@Valid @Valid
private MsAssertionConfig assertionConfig; private MsAssertionConfig assertionConfig = new MsAssertionConfig();
} }

View File

@ -1,6 +1,6 @@
package io.metersphere.api.dto.scenario; package io.metersphere.api.dto.scenario;
import io.metersphere.api.dto.assertion.MsAssertionConfig; import io.metersphere.api.dto.assertion.MsScenarioAssertionConfig;
import io.metersphere.api.dto.request.processors.MsProcessorConfig; import io.metersphere.api.dto.request.processors.MsProcessorConfig;
import lombok.Data; import lombok.Data;
@ -25,7 +25,7 @@ public class ScenarioConfig {
/** /**
* 断言配置 * 断言配置
*/ */
private MsAssertionConfig assertionConfig = new MsAssertionConfig(); private MsScenarioAssertionConfig assertionConfig = new MsScenarioAssertionConfig();
/** /**
* 其他配置 * 其他配置
*/ */

View File

@ -3,7 +3,6 @@ package io.metersphere.api.parser.jmeter;
import io.metersphere.api.constants.ApiConstants; import io.metersphere.api.constants.ApiConstants;
import io.metersphere.api.dto.ApiParamConfig; import io.metersphere.api.dto.ApiParamConfig;
import io.metersphere.api.dto.ApiScenarioParamConfig;
import io.metersphere.api.dto.assertion.MsAssertionConfig; import io.metersphere.api.dto.assertion.MsAssertionConfig;
import io.metersphere.api.dto.request.MsCommonElement; import io.metersphere.api.dto.request.MsCommonElement;
import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.api.dto.request.http.MsHTTPElement;
@ -48,27 +47,47 @@ public class MsCommonElementConverter extends AbstractJmeterElementConverter<MsC
addProcessors(tree, element, config, envInfo, true); addProcessors(tree, element, config, envInfo, true);
// 解析后置处理器包括环境后置 // 解析后置处理器包括环境后置
addProcessors(tree, element, config, envInfo, false); addProcessors(tree, element, config, envInfo, false);
// 处理断言 // 处理断言包括环境断言
handleAssertion(tree, element.getAssertionConfig(), config); addAssertion(tree, element, config);
} }
private EnvironmentInfoDTO getEnvInfo(MsCommonElement element, ParameterConfig config) { private EnvironmentInfoDTO getEnvInfo(MsCommonElement element, ParameterConfig config) {
if (config instanceof ApiParamConfig apiParamConfig) { if (config instanceof ApiParamConfig apiParamConfig) {
return apiParamConfig.getEnvConfig(); return apiParamConfig.getEnvConfig(element.getProjectId());
} else if (config instanceof ApiScenarioParamConfig apiScenarioParamConfig) {
return apiScenarioParamConfig.getEnvConfig(element.getProjectId());
} }
return null; return null;
} }
private void handleAssertion(HashTree tree, MsAssertionConfig assertionConfig, ParameterConfig config) { /**
// todo 开关默认开启关闭则运行该接口时不执行全局前置 * 添加断言
// assertionConfig.getEnableGlobal(); * @param tree
if (assertionConfig == null || assertionConfig.getAssertions() == null) { * @param element
return; * @param config
*/
private void addAssertion(HashTree tree, MsCommonElement element, ParameterConfig config) {
MsAssertionConfig assertionConfig = element.getAssertionConfig();
List<MsAssertion> assertions = assertionConfig.getAssertions();
// 添加环境断言
if (assertionConfig.getEnableGlobal() && config instanceof ApiParamConfig apiParamConfig) {
EnvironmentInfoDTO envConfig = apiParamConfig.getEnvConfig(element.getProjectId());
if (envConfig != null) {
assertions.addAll(envConfig.getConfig().getAssertionConfig().getAssertions());
}
} }
assertionConfig.getAssertions()
.forEach(assertion -> AssertionConverterFactory.getConverter(assertion.getClass()).parse(tree, assertion, config, isIgnoreAssertStatus(assertions)));
}
/**
* 是否忽略状态码
* @param assertions
* @return
*/
public static boolean isIgnoreAssertStatus(List<MsAssertion> assertions) {
boolean isIgnoreStatus = false; boolean isIgnoreStatus = false;
for (MsAssertion assertion : assertionConfig.getAssertions()) { for (MsAssertion assertion : assertions) {
if (assertion instanceof MsResponseCodeAssertion responseCodeAssertion) { if (assertion instanceof MsResponseCodeAssertion responseCodeAssertion) {
// 如果状态码断言添加了不校验状态码则所有断言忽略状态码 // 如果状态码断言添加了不校验状态码则所有断言忽略状态码
if (StringUtils.equals(responseCodeAssertion.getCondition(), MsAssertionCondition.UNCHECK.name())) { if (StringUtils.equals(responseCodeAssertion.getCondition(), MsAssertionCondition.UNCHECK.name())) {
@ -76,9 +95,7 @@ public class MsCommonElementConverter extends AbstractJmeterElementConverter<MsC
} }
} }
} }
boolean finalIsIgnoreStatus = isIgnoreStatus; return isIgnoreStatus;
assertionConfig.getAssertions()
.forEach(assertion -> AssertionConverterFactory.getConverter(assertion.getClass()).parse(tree, assertion, config, finalIsIgnoreStatus));
} }
private void addProcessors(HashTree tree, MsCommonElement msCommonElement, ParameterConfig config, private void addProcessors(HashTree tree, MsCommonElement msCommonElement, ParameterConfig config,

View File

@ -9,8 +9,10 @@ import io.metersphere.api.dto.scenario.ScenarioConfig;
import io.metersphere.api.dto.scenario.ScenarioStepConfig; import io.metersphere.api.dto.scenario.ScenarioStepConfig;
import io.metersphere.api.parser.jmeter.processor.MsProcessorConverter; import io.metersphere.api.parser.jmeter.processor.MsProcessorConverter;
import io.metersphere.api.parser.jmeter.processor.MsProcessorConverterFactory; import io.metersphere.api.parser.jmeter.processor.MsProcessorConverterFactory;
import io.metersphere.api.parser.jmeter.processor.assertion.AssertionConverterFactory;
import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter;
import io.metersphere.project.api.assertion.MsAssertion;
import io.metersphere.project.api.processor.MsProcessor; import io.metersphere.project.api.processor.MsProcessor;
import io.metersphere.project.dto.environment.EnvironmentConfig; import io.metersphere.project.dto.environment.EnvironmentConfig;
import io.metersphere.project.dto.environment.EnvironmentInfoDTO; import io.metersphere.project.dto.environment.EnvironmentInfoDTO;
@ -50,6 +52,30 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
addScenarioProcessor(tree, msScenario, config, false); addScenarioProcessor(tree, msScenario, config, false);
// 添加环境的后置 // 添加环境的后置
addEnvScenarioProcessor(tree, msScenario, config, envInfo, false); addEnvScenarioProcessor(tree, msScenario, config, envInfo, false);
// 添加场景断言
addScenarioAssertions(tree, msScenario, config);
}
/**
* 添加场景断言
* @param tree
* @param msScenario
* @param config
*/
private void addScenarioAssertions(HashTree tree, MsScenario msScenario, ApiScenarioParamConfig config) {
ScenarioConfig scenarioConfig = msScenario.getScenarioConfig();
if (scenarioConfig == null) {
return;
}
List<MsAssertion> assertions = scenarioConfig
.getAssertionConfig()
.getAssertions();
boolean ignoreAssertStatus = MsCommonElementConverter.isIgnoreAssertStatus(assertions);
assertions.forEach(assertion ->
AssertionConverterFactory.getConverter(assertion.getClass()).parse(tree, assertion, config, ignoreAssertStatus));
} }
/** /**

View File

@ -2,10 +2,9 @@ package io.metersphere.api.controller;
import io.metersphere.api.constants.*; import io.metersphere.api.constants.*;
import io.metersphere.api.domain.*; import io.metersphere.api.domain.*;
import io.metersphere.api.dto.assertion.MsAssertionConfig;
import io.metersphere.api.dto.definition.ApiDefinitionAddRequest; import io.metersphere.api.dto.definition.ApiDefinitionAddRequest;
import io.metersphere.api.dto.definition.ApiTestCaseAddRequest; import io.metersphere.api.dto.definition.ApiTestCaseAddRequest;
import io.metersphere.api.dto.assertion.MsAssertionConfig;
import io.metersphere.project.api.assertion.MsScriptAssertion;
import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.dto.response.ApiScenarioBatchOperationResponse; import io.metersphere.api.dto.response.ApiScenarioBatchOperationResponse;
import io.metersphere.api.dto.response.OperationDataInfo; import io.metersphere.api.dto.response.OperationDataInfo;
@ -22,6 +21,8 @@ import io.metersphere.api.utils.JmeterElementConverterRegister;
import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter;
import io.metersphere.plugin.api.spi.JmeterElementConverter; import io.metersphere.plugin.api.spi.JmeterElementConverter;
import io.metersphere.plugin.api.spi.MsTestElement; import io.metersphere.plugin.api.spi.MsTestElement;
import io.metersphere.project.api.assertion.MsResponseCodeAssertion;
import io.metersphere.project.api.assertion.MsScriptAssertion;
import io.metersphere.project.api.processor.MsProcessor; import io.metersphere.project.api.processor.MsProcessor;
import io.metersphere.project.api.processor.SQLProcessor; import io.metersphere.project.api.processor.SQLProcessor;
import io.metersphere.project.dto.environment.EnvironmentConfig; import io.metersphere.project.dto.environment.EnvironmentConfig;
@ -395,7 +396,11 @@ public class ApiScenarioControllerTests extends BaseTest {
scriptAssertion.setScript("{}"); scriptAssertion.setScript("{}");
scriptAssertion.setName("script"); scriptAssertion.setName("script");
msAssertionConfig.setAssertions(List.of(scriptAssertion)); msAssertionConfig.setAssertions(List.of(scriptAssertion));
scenarioConfig.setAssertionConfig(msAssertionConfig); MsResponseCodeAssertion responseCodeAssertion = new MsResponseCodeAssertion();
responseCodeAssertion.setExpectedValue("200");
responseCodeAssertion.setCondition(MsAssertionCondition.EMPTY.name());
responseCodeAssertion.setName("test");
scenarioConfig.getAssertionConfig().getAssertions().add(responseCodeAssertion);
ScenarioOtherConfig scenarioOtherConfig = new ScenarioOtherConfig(); ScenarioOtherConfig scenarioOtherConfig = new ScenarioOtherConfig();
scenarioOtherConfig.setStepWaitTime(1000); scenarioOtherConfig.setStepWaitTime(1000);
scenarioOtherConfig.setFailureStrategy(ScenarioOtherConfig.FailureStrategy.CONTINUE.name()); scenarioOtherConfig.setFailureStrategy(ScenarioOtherConfig.FailureStrategy.CONTINUE.name());
@ -730,6 +735,13 @@ public class ApiScenarioControllerTests extends BaseTest {
List<MsProcessor> postRequestProcessors = postProcessorConfig.getApiProcessorConfig().getRequestProcessorConfig().getProcessors(); List<MsProcessor> postRequestProcessors = postProcessorConfig.getApiProcessorConfig().getRequestProcessorConfig().getProcessors();
postRequestProcessors.add(envRequestScriptProcessor); postRequestProcessors.add(envRequestScriptProcessor);
postRequestProcessors.add(sqlProcessor); postRequestProcessors.add(sqlProcessor);
MsResponseCodeAssertion responseCodeAssertion = new MsResponseCodeAssertion();
responseCodeAssertion.setExpectedValue("200");
responseCodeAssertion.setCondition(MsAssertionCondition.EMPTY.name());
responseCodeAssertion.setName("test");
environmentConfig.getAssertionConfig().getAssertions().add(responseCodeAssertion);
environmentConfig.setPluginConfigMap(pluginConfigMap); environmentConfig.setPluginConfigMap(pluginConfigMap);
envRequest.setConfig(environmentConfig); envRequest.setConfig(environmentConfig);
Environment environment = environmentService.add(envRequest, "admin", null); Environment environment = environmentService.add(envRequest, "admin", null);