From 3d77866d425f82ada054889da88f3c61f7b1e337 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Fri, 18 Mar 2022 18:42:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=AF=E7=94=A8=E8=AF=AF=E6=8A=A5=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E5=A4=B1=E8=B4=A5=E5=81=9C=E6=AD=A2=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1011393 --user=宋天阳 【接口测试】场景测试,开启误报库后,不开启失败继续,遇到误报就停止了 https://www.tapd.cn/55049933/s/1121190 --- .../api/dto/ErrorReportLibraryParseDTO.java | 2 + .../request/assertions/MsAssertionRegex.java | 1 + .../request/assertions/MsAssertions.java | 4 ++ .../ApiScenarioReportResultService.java | 5 ++- .../service/ExtErrorReportLibraryService.java | 4 +- .../commons/utils/ErrorReportLibraryUtil.java | 37 ++++++++----------- .../commons/utils/ResponseUtil.java | 6 +-- 7 files changed, 32 insertions(+), 27 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/ErrorReportLibraryParseDTO.java b/backend/src/main/java/io/metersphere/api/dto/ErrorReportLibraryParseDTO.java index ab2cc3e514..4431f9cbf8 100644 --- a/backend/src/main/java/io/metersphere/api/dto/ErrorReportLibraryParseDTO.java +++ b/backend/src/main/java/io/metersphere/api/dto/ErrorReportLibraryParseDTO.java @@ -1,5 +1,6 @@ package io.metersphere.api.dto; +import io.metersphere.dto.RequestResult; import lombok.Getter; import lombok.Setter; import org.apache.commons.collections.CollectionUtils; @@ -12,6 +13,7 @@ import java.util.List; @Setter public class ErrorReportLibraryParseDTO { public List errorCodeList; + public RequestResult result; public ErrorReportLibraryParseDTO() { this.errorCodeList = new ArrayList<>(); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertionRegex.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertionRegex.java index bbd1f465b3..f9f4a5fe94 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertionRegex.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertionRegex.java @@ -11,6 +11,7 @@ public class MsAssertionRegex extends MsAssertionType { private String expression; private String description; private boolean assumeSuccess; + private int testType = 2; public MsAssertionRegex() { setType(MsAssertionType.REGEX); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java index cf2bd13d17..a8579e73f2 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java @@ -112,6 +112,10 @@ public class MsAssertions extends MsTestElement { assertion.setAssumeSuccess(assertionRegex.isAssumeSuccess()); assertion.addTestString(assertionRegex.getExpression()); assertion.setToContainsType(); + if(assertionRegex.getTestType() != 2){ + assertion.setProperty("Assertion.test_type",assertionRegex.getTestType()); + } + switch (assertionRegex.getSubject()) { case "Response Code": assertion.setTestFieldResponseCode(); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportResultService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportResultService.java index ccbb405a6a..ca8331d17f 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportResultService.java @@ -64,10 +64,11 @@ public class ApiScenarioReportResultService { } } - private ApiScenarioReportResult newApiScenarioReportResult(String reportId, RequestResult result) { + private ApiScenarioReportResult newApiScenarioReportResult(String reportId, RequestResult baseResult) { ApiScenarioReportResult report = new ApiScenarioReportResult(); //解析误报内容 - ErrorReportLibraryParseDTO errorCodeDTO = ErrorReportLibraryUtil.parseAssertions(result); + ErrorReportLibraryParseDTO errorCodeDTO = ErrorReportLibraryUtil.parseAssertions(baseResult); + RequestResult result = errorCodeDTO.getResult(); report.setId(UUID.randomUUID().toString()); String resourceId = result.getResourceId(); report.setResourceId(resourceId); diff --git a/backend/src/main/java/io/metersphere/api/service/ExtErrorReportLibraryService.java b/backend/src/main/java/io/metersphere/api/service/ExtErrorReportLibraryService.java index 06a641258e..c19426608d 100644 --- a/backend/src/main/java/io/metersphere/api/service/ExtErrorReportLibraryService.java +++ b/backend/src/main/java/io/metersphere/api/service/ExtErrorReportLibraryService.java @@ -35,7 +35,9 @@ public class ExtErrorReportLibraryService { try { MsAssertions assertions = JSONObject.parseObject(item.getContent(), MsAssertions.class); if (assertions != null && CollectionUtils.isNotEmpty(assertions.getRegex())) { - assertions.getRegex().get(0).setDescription("Error report:" + item.getErrorCode()); + //误报的断言要设置取反 + assertions.getRegex().get(0).setTestType(6); + assertions.getRegex().get(0).setDescription("Check Error report:" + item.getErrorCode()); returnList.add(assertions); } } catch (Exception e) { diff --git a/backend/src/main/java/io/metersphere/commons/utils/ErrorReportLibraryUtil.java b/backend/src/main/java/io/metersphere/commons/utils/ErrorReportLibraryUtil.java index 8691e2f132..ef96801811 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/ErrorReportLibraryUtil.java +++ b/backend/src/main/java/io/metersphere/commons/utils/ErrorReportLibraryUtil.java @@ -14,29 +14,26 @@ import java.util.List; */ public class ErrorReportLibraryUtil { - private static final String ERROR_CODE_START = "Error report:"; + + private static final String NEW_ERROR_CODE_STATE = "Check Error report:"; public static ErrorReportLibraryParseDTO parseAssertions(RequestResult result) { ErrorReportLibraryParseDTO returnDTO = new ErrorReportLibraryParseDTO(); if (result != null && result.getResponseResult() != null && CollectionUtils.isNotEmpty(result.getResponseResult().getAssertions())) { List errorReportAssertionList = new ArrayList<>(); - boolean hasOtherErrorAssertion = false; - int otherAssertionCount = 0; for (ResponseAssertionResult assertion : result.getResponseResult().getAssertions()) { - if (StringUtils.startsWith(assertion.getContent(), ERROR_CODE_START)) { + if (StringUtils.startsWithAny(assertion.getContent(), NEW_ERROR_CODE_STATE)) { errorReportAssertionList.add(assertion); - }else { - otherAssertionCount ++; - if(!assertion.isPass()){ - hasOtherErrorAssertion = true; - } } } if (CollectionUtils.isNotEmpty(errorReportAssertionList)) { List unMatchErrorReportAssertions = new ArrayList<>(); + int machedErrorPortAssertions = 0; for (ResponseAssertionResult assertion : errorReportAssertionList) { - if (assertion.isPass()) { - String errorCode = StringUtils.substring(assertion.getContent(), ERROR_CODE_START.length()); + if (!assertion.isPass()) { + assertion.setPass(true); + machedErrorPortAssertions ++; + String errorCode = StringUtils.substring(assertion.getContent(), NEW_ERROR_CODE_STATE.length()); returnDTO.getErrorCodeList().add(errorCode); } else { unMatchErrorReportAssertions.add(assertion); @@ -45,21 +42,19 @@ public class ErrorReportLibraryUtil { if (CollectionUtils.isNotEmpty(unMatchErrorReportAssertions)) { // 未被误报断言匹配到的结果,清除该请求的误报断言记录,并将断言涉及到的统计结果恢复正常 - if (result.getResponseResult() != null - && StringUtils.equalsIgnoreCase(result.getResponseResult().getResponseCode(), "200") - && result.getError() > 0) { - if(otherAssertionCount == 0 || !hasOtherErrorAssertion){ - result.setError(result.getError() - 1); - } - } result.setTotalAssertions(result.getTotalAssertions() - unMatchErrorReportAssertions.size()); - result.getResponseResult().getAssertions().removeAll(unMatchErrorReportAssertions); - if (result.getError() == 0 && !result.isSuccess()) { - result.setSuccess(true); + int passAssertionCountg = (result.getPassAssertions() - unMatchErrorReportAssertions.size() ) + machedErrorPortAssertions; + if(passAssertionCountg< 0){ + result.setPassAssertions(0); + }else { + result.setPassAssertions(passAssertionCountg); } + + result.getResponseResult().getAssertions().removeAll(unMatchErrorReportAssertions); } } } + returnDTO.setResult(result); return returnDTO; } } diff --git a/backend/src/main/java/io/metersphere/commons/utils/ResponseUtil.java b/backend/src/main/java/io/metersphere/commons/utils/ResponseUtil.java index 50e34c9801..b5e82b32cb 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/ResponseUtil.java +++ b/backend/src/main/java/io/metersphere/commons/utils/ResponseUtil.java @@ -14,10 +14,10 @@ import java.util.Map; */ public class ResponseUtil { - public static RequestResultExpandDTO parseByRequestResult(RequestResult requestResult) { + public static RequestResultExpandDTO parseByRequestResult(RequestResult baseResult) { //解析是否含有误报库信息 - ErrorReportLibraryParseDTO errorCodeDTO = ErrorReportLibraryUtil.parseAssertions(requestResult); - + ErrorReportLibraryParseDTO errorCodeDTO = ErrorReportLibraryUtil.parseAssertions(baseResult); + RequestResult requestResult = errorCodeDTO.getResult(); RequestResultExpandDTO expandDTO = new RequestResultExpandDTO(); BeanUtils.copyBean(expandDTO, requestResult);