fix(接口测试): 存在多个响应码断言,断言结果有误

--bug=1046899 --user=陈建星 [接口测试]github#332222个全局断言成功+1个接口用例本身断言失败 = 最终结果为成功 https://www.tapd.cn/55049933/s/1600346
This commit is contained in:
AgAngle 2024-10-29 15:57:34 +08:00 committed by Craftsman
parent 5272fd1482
commit beecbc886f
3 changed files with 20 additions and 32 deletions

View File

@ -22,9 +22,7 @@ import io.metersphere.project.dto.environment.processors.ApiEnvProcessorConfig;
import io.metersphere.project.dto.environment.processors.ApiEnvRequestProcessorConfig; import io.metersphere.project.dto.environment.processors.ApiEnvRequestProcessorConfig;
import io.metersphere.project.dto.environment.processors.EnvProcessorConfig; import io.metersphere.project.dto.environment.processors.EnvProcessorConfig;
import io.metersphere.project.dto.environment.processors.EnvRequestScriptProcessor; import io.metersphere.project.dto.environment.processors.EnvRequestScriptProcessor;
import io.metersphere.sdk.constants.MsAssertionCondition;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import java.util.ArrayList; import java.util.ArrayList;
@ -81,40 +79,25 @@ public class MsCommonElementConverter extends AbstractJmeterElementConverter<MsC
// 将状态码断言放最前面否则会影响脚本断言的效果即使脚本断言失败总状态还是显示成功 // 将状态码断言放最前面否则会影响脚本断言的效果即使脚本断言失败总状态还是显示成功
List<MsAssertion> sortAssertions = new ArrayList<>(assertions.size()); List<MsAssertion> sortAssertions = new ArrayList<>(assertions.size());
assertions.forEach(item -> { assertions.forEach(item -> {
if (item instanceof MsResponseCodeAssertion) { if (BooleanUtils.isTrue(item.getEnable()) && item instanceof MsResponseCodeAssertion) {
sortAssertions.add(item); sortAssertions.add(item);
} }
}); });
assertions.forEach(item -> { assertions.forEach(item -> {
if (!(item instanceof MsResponseCodeAssertion)) { if (BooleanUtils.isTrue(item.getEnable()) && !(item instanceof MsResponseCodeAssertion)) {
sortAssertions.add(item); sortAssertions.add(item);
} }
}); });
sortAssertions for (int i = 0; i < sortAssertions.size(); i++) {
.forEach(assertion -> { MsAssertion assertion = sortAssertions.get(i);
assertion.setProjectId(element.getProjectId()); assertion.setProjectId(element.getProjectId());
AssertionConverterFactory.getConverter(assertion.getClass()).parse(tree, assertion, config, isIgnoreAssertStatus(assertions)); // 只给第一个响应码断言设置忽略状态
}); boolean isIgnoreStatus = i == 0 && assertion instanceof MsResponseCodeAssertion;
}
/** AssertionConverterFactory.getConverter(assertion.getClass())
* 是否忽略状态码 .parse(tree, assertion, config, isIgnoreStatus);
*
* @param assertions
* @return
*/
public static boolean isIgnoreAssertStatus(List<MsAssertion> assertions) {
boolean isIgnoreStatus = false;
for (MsAssertion assertion : assertions) {
if (assertion instanceof MsResponseCodeAssertion responseCodeAssertion) {
// 如果状态码断言添加了不校验状态码则所有断言忽略状态码
if (StringUtils.equals(responseCodeAssertion.getCondition(), MsAssertionCondition.UNCHECK.name())) {
isIgnoreStatus = true;
} }
} }
}
return isIgnoreStatus;
}
private void addProcessors(HashTree tree, MsCommonElement msCommonElement, ParameterConfig config, private void addProcessors(HashTree tree, MsCommonElement msCommonElement, ParameterConfig config,
EnvironmentInfoDTO envInfo, boolean isPre) { EnvironmentInfoDTO envInfo, boolean isPre) {

View File

@ -18,6 +18,7 @@ import io.metersphere.api.parser.jmeter.processor.assertion.AssertionConverterFa
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.assertion.MsAssertion;
import io.metersphere.project.api.assertion.MsResponseCodeAssertion;
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;
@ -241,10 +242,14 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
.getAssertionConfig() .getAssertionConfig()
.getAssertions(); .getAssertions();
boolean ignoreAssertStatus = MsCommonElementConverter.isIgnoreAssertStatus(assertions); for (int i = 0; i < assertions.size(); i++) {
MsAssertion assertion = assertions.get(i);
assertions.forEach(assertion -> // 只给第一个响应码断言设置忽略状态
AssertionConverterFactory.getConverter(assertion.getClass()).parse(tree, assertion, config, ignoreAssertStatus)); boolean isIgnoreStatus = i == 0 && assertion instanceof MsResponseCodeAssertion;
AssertionConverterFactory.getConverter(assertion.getClass()).parse(tree, assertion, config, isIgnoreStatus);
}
} }
/** /**

View File

@ -17,18 +17,18 @@ public class ResponseCodeAssertionConverter extends AssertionConverter<MsRespons
if (!needParse(msAssertion, config) || !isValid(msAssertion)) { if (!needParse(msAssertion, config) || !isValid(msAssertion)) {
return; return;
} }
hashTree.add(parse2ResponseAssertion(msAssertion)); hashTree.add(parse2ResponseAssertion(msAssertion, isIgnoreStatus));
} }
public boolean isValid(MsResponseCodeAssertion msAssertion) { public boolean isValid(MsResponseCodeAssertion msAssertion) {
return StringUtils.isNotBlank(msAssertion.getCondition()); return StringUtils.isNotBlank(msAssertion.getCondition());
} }
private ResponseAssertion parse2ResponseAssertion(MsResponseCodeAssertion msAssertion) { private ResponseAssertion parse2ResponseAssertion(MsResponseCodeAssertion msAssertion, boolean isIgnoreStatus) {
ResponseAssertion assertion = createResponseAssertion(); ResponseAssertion assertion = createResponseAssertion();
String expectedValue = msAssertion.getExpectedValue(); String expectedValue = msAssertion.getExpectedValue();
assertion.setEnabled(msAssertion.getEnable()); assertion.setEnabled(msAssertion.getEnable());
assertion.setAssumeSuccess(true); assertion.setAssumeSuccess(isIgnoreStatus);
assertion.setEnabled(msAssertion.getEnable()); assertion.setEnabled(msAssertion.getEnable());
String condition = msAssertion.getCondition(); String condition = msAssertion.getCondition();