fix(接口自动化): 修复测试报告,按照场景展示
This commit is contained in:
parent
c15cd077bc
commit
93d57b7d1f
|
@ -58,6 +58,8 @@ public class MsScenario extends MsTestElement {
|
|||
if (!this.isEnable()) {
|
||||
return;
|
||||
}
|
||||
config.setStep(this.name);
|
||||
|
||||
config.setEnableCookieShare(enableCookieShare);
|
||||
if (StringUtils.isNotEmpty(environmentId)) {
|
||||
ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class);
|
||||
|
|
|
@ -14,5 +14,9 @@ public class ParameterConfig {
|
|||
private List<ScenarioVariable> variables;
|
||||
// 公共Cookie
|
||||
private boolean enableCookieShare;
|
||||
// 步骤
|
||||
private String step;
|
||||
|
||||
private final String stepType = "STEP_GROUP";
|
||||
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import io.metersphere.api.dto.scenario.KeyValue;
|
|||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.jmeter.config.ConfigTestElement;
|
||||
import org.apache.jmeter.save.SaveService;
|
||||
import org.apache.jmeter.testelement.TestElement;
|
||||
|
@ -64,7 +65,7 @@ public class MsDubboSampler extends MsTestElement {
|
|||
this.getRefElement(this);
|
||||
}
|
||||
|
||||
final HashTree testPlanTree = tree.add(dubboSample());
|
||||
final HashTree testPlanTree = tree.add(dubboSample(config));
|
||||
if (CollectionUtils.isNotEmpty(hashTree)) {
|
||||
hashTree.forEach(el -> {
|
||||
el.toHashTree(testPlanTree, el.getHashTree(), config);
|
||||
|
@ -72,9 +73,12 @@ public class MsDubboSampler extends MsTestElement {
|
|||
}
|
||||
}
|
||||
|
||||
private DubboSample dubboSample() {
|
||||
private DubboSample dubboSample(ParameterConfig config) {
|
||||
DubboSample sampler = new DubboSample();
|
||||
sampler.setName(this.getName());
|
||||
if (config != null && StringUtils.isNotEmpty(config.getStep())) {
|
||||
sampler.setName(this.getName() + "<->" + config.getStep());
|
||||
}
|
||||
sampler.setProperty(TestElement.TEST_CLASS, DubboSample.class.getName());
|
||||
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("DubboSampleGui"));
|
||||
|
||||
|
@ -97,19 +101,6 @@ public class MsDubboSampler extends MsTestElement {
|
|||
return sampler;
|
||||
}
|
||||
|
||||
|
||||
private ConfigTestElement dubboConfig() {
|
||||
ConfigTestElement configTestElement = new ConfigTestElement();
|
||||
configTestElement.setEnabled(true);
|
||||
configTestElement.setName(this.getName());
|
||||
configTestElement.setProperty(TestElement.TEST_CLASS, ConfigTestElement.class.getName());
|
||||
configTestElement.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("DubboDefaultConfigGui"));
|
||||
configTestElement.addConfigElement(configCenter(this.getConfigCenter()));
|
||||
configTestElement.addConfigElement(registryCenter(this.getRegistryCenter()));
|
||||
configTestElement.addConfigElement(consumerAndService(this.getConsumerAndService()));
|
||||
return configTestElement;
|
||||
}
|
||||
|
||||
private ConfigTestElement configCenter(MsConfigCenter configCenter) {
|
||||
ConfigTestElement configTestElement = new ConfigTestElement();
|
||||
if (configCenter != null && configCenter.getProtocol() != null && configCenter.getUsername() != null && configCenter.getPassword() != null) {
|
||||
|
|
|
@ -99,6 +99,10 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
|||
HTTPSamplerProxy sampler = new HTTPSamplerProxy();
|
||||
sampler.setEnabled(true);
|
||||
sampler.setName(this.getName());
|
||||
if (config != null && StringUtils.isNotEmpty(config.getStep())) {
|
||||
sampler.setName(this.getName() + "<->" + config.getStep());
|
||||
}
|
||||
|
||||
sampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
|
||||
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HttpTestSampleGui"));
|
||||
sampler.setMethod(this.getMethod());
|
||||
|
|
|
@ -64,7 +64,7 @@ public class MsJDBCSampler extends MsTestElement {
|
|||
if (this.dataSource == null) {
|
||||
MSException.throwException("数据源为空无法执行");
|
||||
}
|
||||
final HashTree samplerHashTree = tree.add(jdbcSampler());
|
||||
final HashTree samplerHashTree = tree.add(jdbcSampler(config));
|
||||
tree.add(jdbcDataSource());
|
||||
tree.add(arguments(this.getName() + " Variables", this.getVariables()));
|
||||
if (CollectionUtils.isNotEmpty(hashTree)) {
|
||||
|
@ -102,9 +102,12 @@ public class MsJDBCSampler extends MsTestElement {
|
|||
return arguments;
|
||||
}
|
||||
|
||||
private JDBCSampler jdbcSampler() {
|
||||
private JDBCSampler jdbcSampler(ParameterConfig config) {
|
||||
JDBCSampler sampler = new JDBCSampler();
|
||||
sampler.setName(this.getName());
|
||||
if (config != null && StringUtils.isNotEmpty(config.getStep())) {
|
||||
sampler.setName(this.getName() + "<->" + config.getStep());
|
||||
}
|
||||
sampler.setProperty(TestElement.TEST_CLASS, JDBCSampler.class.getName());
|
||||
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
|
||||
// request.getDataSource() 是ID,需要转换为Name
|
||||
|
|
|
@ -7,6 +7,7 @@ import io.metersphere.api.dto.definition.request.ParameterConfig;
|
|||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.jmeter.config.ConfigTestElement;
|
||||
import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
|
||||
import org.apache.jmeter.save.SaveService;
|
||||
|
@ -59,7 +60,7 @@ public class MsTCPSampler extends MsTestElement {
|
|||
this.getRefElement(this);
|
||||
}
|
||||
final HashTree samplerHashTree = new ListedHashTree();
|
||||
samplerHashTree.add(tcpConfig());
|
||||
samplerHashTree.add(tcpConfig(config));
|
||||
tree.set(tcpSampler(), samplerHashTree);
|
||||
if (CollectionUtils.isNotEmpty(hashTree)) {
|
||||
hashTree.forEach(el -> {
|
||||
|
@ -89,10 +90,14 @@ public class MsTCPSampler extends MsTestElement {
|
|||
return tcpSampler;
|
||||
}
|
||||
|
||||
private ConfigTestElement tcpConfig() {
|
||||
private ConfigTestElement tcpConfig(ParameterConfig config) {
|
||||
ConfigTestElement configTestElement = new ConfigTestElement();
|
||||
configTestElement.setEnabled(true);
|
||||
configTestElement.setName(this.getName());
|
||||
if (config != null && StringUtils.isNotEmpty(config.getStep())) {
|
||||
configTestElement.setName(this.getName() + "<->" + config.getStep());
|
||||
}
|
||||
|
||||
configTestElement.setProperty(TestElement.TEST_CLASS, ConfigTestElement.class.getName());
|
||||
configTestElement.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TCPConfigGui"));
|
||||
configTestElement.setProperty(TCPSampler.CLASSNAME, this.getClassname());
|
||||
|
|
|
@ -22,7 +22,7 @@ public class ScenarioResult {
|
|||
|
||||
private int passAssertions = 0;
|
||||
|
||||
private final List<RequestResult> requestResults = new ArrayList<>();
|
||||
private List<RequestResult> requestResults = new ArrayList<>();
|
||||
|
||||
public void addResponseTime(long time) {
|
||||
this.responseTime += time;
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package io.metersphere.api.jmeter;
|
||||
|
||||
import io.metersphere.commons.utils.BeanUtils;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@Data
|
||||
public class TestResult {
|
||||
|
@ -38,5 +40,33 @@ public class TestResult {
|
|||
this.passAssertions += count;
|
||||
}
|
||||
|
||||
|
||||
public void addScenario(ScenarioResult result) {
|
||||
Map<String, List<RequestResult>> requestResultMap = new LinkedHashMap<>();
|
||||
if (result != null && CollectionUtils.isNotEmpty(result.getRequestResults())) {
|
||||
result.getRequestResults().forEach(item -> {
|
||||
if (StringUtils.isNotEmpty(item.getName()) && item.getName().indexOf("<->") != -1) {
|
||||
String array[] = item.getName().split("<->");
|
||||
item.setName(array[0]);
|
||||
if (requestResultMap.containsKey(array[1])) {
|
||||
requestResultMap.get(array[1]).add(item);
|
||||
} else {
|
||||
List<RequestResult> requestResults = new LinkedList<>();
|
||||
requestResults.add(item);
|
||||
requestResultMap.put(array[1], requestResults);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!requestResultMap.isEmpty()) {
|
||||
requestResultMap.forEach((k, v) -> {
|
||||
ScenarioResult scenarioResult = new ScenarioResult();
|
||||
BeanUtils.copyBean(scenarioResult, result);
|
||||
scenarioResult.setName(k);
|
||||
scenarioResult.setRequestResults(v);
|
||||
scenarios.add(scenarioResult);
|
||||
});
|
||||
} else {
|
||||
scenarios.add(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -368,14 +368,12 @@ public class ApiAutomationService {
|
|||
// 多态JSON普通转换会丢失内容,需要通过 ObjectMapper 获取
|
||||
if (element != null && StringUtils.isNotEmpty(element.getString("hashTree"))) {
|
||||
LinkedList<MsTestElement> elements = mapper.readValue(element.getString("hashTree"),
|
||||
new TypeReference<LinkedList<MsTestElement>>() {
|
||||
});
|
||||
new TypeReference<LinkedList<MsTestElement>>() {});
|
||||
scenario.setHashTree(elements);
|
||||
}
|
||||
if (StringUtils.isNotEmpty(element.getString("variables"))) {
|
||||
LinkedList<ScenarioVariable> variables = mapper.readValue(element.getString("variables"),
|
||||
new TypeReference<LinkedList<ScenarioVariable>>() {
|
||||
});
|
||||
new TypeReference<LinkedList<ScenarioVariable>>() {});
|
||||
scenario.setVariables(variables);
|
||||
}
|
||||
group.setEnableCookieShare(scenario.isEnableCookieShare());
|
||||
|
|
|
@ -3,7 +3,9 @@ package io.metersphere.api.service;
|
|||
import com.alibaba.fastjson.JSON;
|
||||
import io.metersphere.api.dto.DeleteAPIReportRequest;
|
||||
import io.metersphere.api.dto.QueryAPIReportRequest;
|
||||
import io.metersphere.api.dto.automation.*;
|
||||
import io.metersphere.api.dto.automation.APIScenarioReportResult;
|
||||
import io.metersphere.api.dto.automation.ExecuteType;
|
||||
import io.metersphere.api.dto.automation.ScenarioStatus;
|
||||
import io.metersphere.api.dto.datacount.ApiDataCountResult;
|
||||
import io.metersphere.api.jmeter.ScenarioResult;
|
||||
import io.metersphere.api.jmeter.TestResult;
|
||||
|
@ -26,7 +28,6 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
import javax.annotation.Resource;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -136,10 +137,12 @@ public class ApiScenarioReportService {
|
|||
// 报告详情内容
|
||||
ApiScenarioReportDetail detail = new ApiScenarioReportDetail();
|
||||
TestResult newResult = createTestResult(result.getTestId(), scenarioResult);
|
||||
List<ScenarioResult> scenarioResults = new ArrayList();
|
||||
scenarioResult.setName(report.getScenarioName());
|
||||
scenarioResults.add(scenarioResult);
|
||||
newResult.setScenarios(scenarioResults);
|
||||
// List<ScenarioResult> scenarioResults = new ArrayList();
|
||||
// scenarioResult.setName(report.getScenarioName());
|
||||
// scenarioResults.add(scenarioResult);
|
||||
// newResult.setScenarios(scenarioResults);
|
||||
newResult.addScenario(scenarioResult);
|
||||
|
||||
detail.setContent(JSON.toJSONString(newResult).getBytes(StandardCharsets.UTF_8));
|
||||
detail.setReportId(report.getId());
|
||||
detail.setProjectId(report.getProjectId());
|
||||
|
@ -158,10 +161,11 @@ public class ApiScenarioReportService {
|
|||
// 报告详情内容
|
||||
ApiScenarioReportDetail detail = new ApiScenarioReportDetail();
|
||||
TestResult newResult = createTestResult(result.getTestId(), item);
|
||||
List<ScenarioResult> scenarioResults = new ArrayList();
|
||||
item.setName(report.getScenarioName());
|
||||
scenarioResults.add(item);
|
||||
newResult.setScenarios(scenarioResults);
|
||||
// List<ScenarioResult> scenarioResults = new ArrayList();
|
||||
// item.setName(report.getScenarioName());
|
||||
// scenarioResults.add(item);
|
||||
// newResult.setScenarios(scenarioResults);
|
||||
newResult.addScenario(item);
|
||||
detail.setContent(JSON.toJSONString(newResult).getBytes(StandardCharsets.UTF_8));
|
||||
detail.setReportId(report.getId());
|
||||
detail.setProjectId(report.getProjectId());
|
||||
|
|
Loading…
Reference in New Issue