提交 c8e22201 authored 作者: mry's avatar mry

fix(base): 自定义简单的testNg模板.DataBean中的数据到报告中的转换

上级 75a395c4
......@@ -19,6 +19,12 @@ import java.util.List;
@ApiModel(value = "接口信息")
public class InterfaceInformation {
/**
* id
*/
@ApiModelProperty(value = "id")
private Long id;
/**
* ip 以及端口信息
*/
......
package org.matrix.testNg;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.matrix.BaseBootApplication;
import org.matrix.actuators.checkpoint.CheckPointActuator;
import org.matrix.actuators.checkpoint.CheckPointResult;
import org.matrix.actuators.httpclient.HttpClientActuator;
import org.matrix.actuators.usecase.CaseActuator;
import org.matrix.actuators.usecase.TestCaseExecuteResult;
import org.matrix.database.entity.TestCase;
import org.matrix.database.entity.TestCaseBTO;
import org.matrix.database.entity.TestCaseListDataBto;
import org.matrix.database.entity.TestData;
import org.matrix.database.service.ITestCaseService;
import org.matrix.database.service.ITestDataService;
import org.matrix.exception.GlobalException;
import org.matrix.socket.queue.LogQueueRuntime;
import org.matrix.socket.pool.TestCaseExecuteSocketPool;
import org.matrix.socket.enums.TestExecuteType;
import org.matrix.socket.vo.CaseExecuteVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.*;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* @author mry
*/
@Slf4j
@Component
@SpringBootTest(classes = BaseBootApplication.class)
public class TestPigeon extends AbstractTestNGSpringContextTests {
String sql;
......@@ -77,6 +76,7 @@ public class TestPigeon extends AbstractTestNGSpringContextTests {
this.projectId = projectId;
}
/**
* XML中的SQL决定了执行什么用例, 执行多少条用例, SQL的搜索结果为需要测试的测试用例
*/
......@@ -85,37 +85,33 @@ public class TestPigeon extends AbstractTestNGSpringContextTests {
return new DataProviderForDb(driver, url, username, password, sql);
}
public static Map<Long, Map<Long, List<TestCaseExecuteResult>>> userResultMap = new HashMap<>();
public static Map<Long, List<TestCaseExecuteResult>> resultMap = new HashMap<>();
@Test(dataProvider = "testData")
public void test(Map<String, String> data) {
long caseId = Long.parseLong(data.get("id"));
log.info("这是testData");
String id = data.get("id");
long caseId = Long.parseLong(id);
TestCase testCase = java.util.Optional.of(caseService.getById(caseId))
.orElseThrow(() -> new GlobalException(String.format("没有找到id = %d 的TestCase", caseId)));
List<TestData> testDataList = java.util.Optional.of(dataService.list(Wrappers.lambdaQuery(TestData.class)
.eq(TestData::getTestCaseId, caseId)))
.orElseThrow(() -> new GlobalException(String.format("没有找到testCaseId = %d 的TestData", caseId)));
TestCaseBTO testCaseBTO = new TestCaseBTO();
testCaseBTO.setTestCase(testCase);
String unionKey = UUID.randomUUID().toString();
LogQueueRuntime
.initTestCaseLog(null
, 1L
, testCase.getId()
, TestExecuteType.TEST_CASE
, unionKey
CaseExecuteVo caseExecuteVo = new CaseExecuteVo();
caseExecuteVo.setProjectId(testCase.getProjectId());
caseExecuteVo.setEnvId(envId);
caseExecuteVo.setUserId(1L);
caseExecuteVo.getTestCaseListDataBtoList().add(
new TestCaseListDataBto(
testCase,
testDataList
)
);
for (TestData testData : testDataList) {
testCaseBTO.setTestData(testData);
TestCaseExecuteResult testCaseExecuteResult = caseActuator.executeTestCase(testCaseBTO, envId, projectId);
CheckPointResult checkPointResult = testCaseExecuteResult.getCheckPointResult();
Reporter.log(checkPointResult.toString());
}
//将本次产生的数据清除
TestCaseExecuteSocketPool.remove(Thread.currentThread().getId());
List<TestCaseExecuteResult> testCaseExecuteResults = caseActuator.runTestCase(null, caseExecuteVo);
resultMap.put(caseId, testCaseExecuteResults);
userResultMap.put(1L, resultMap);
}
@AfterMethod
public void afterMethod(ITestResult result, Object[] objs) {
System.out.println("正在运行的方法:" + result.getName());
}
}
......@@ -7,6 +7,8 @@ import lombok.NoArgsConstructor;
import java.util.List;
/**
* 定义模板中需要的信息
*
* @author mruny
*/
@Data
......@@ -39,16 +41,6 @@ public class DataBean {
*/
private int allTestsSize;
// /**
// * 全部执行的测试方法
// */
// private ITestNGMethod[] allTestsMethod;
// /**
// * 未执行的测试方法
// */
// private Collection<ITestNGMethod> excludeTestsMethod;
/**
* 测试耗时
*/
......@@ -60,7 +52,7 @@ public class DataBean {
private String passPercent;
/**
* 单个测试周期
* 单个测试时间
*/
private String duration;
......@@ -72,7 +64,7 @@ public class DataBean {
/**
* 测试描述
*/
private String des;
private String description;
/**
* Reporter Output
......@@ -97,7 +89,7 @@ public class DataBean {
/**
* 结果信息
*/
private String resultMessage;
private Object resultMessage;
/**
* 用例名称
......@@ -109,6 +101,11 @@ public class DataBean {
*/
private String type;
/**
* 详情参数
*/
private String detail;
/**
* 前置行动ID组,例如:1,2,3
*/
......@@ -124,40 +121,4 @@ public class DataBean {
*/
private String moveAfterTest;
/**
* 是否进行异常检验,0为否,1为是
*/
private Integer abnormalCheckpoint = 0;
/**
* 是否进行非空检验,0为否,1为是
*/
private Integer noEmptyCheckpoint = 0;
/**
* 包含某字段检验(例如 张三,李四) 则对检查结果中是否包含张三或者李四
*/
private String containCheckpoint;
/**
* 不包含某字段检验(例如 张三,李四) 则对检查结果中是否不包含张三或者李四
*/
private String noContainCheckpoint;
/**
* 数据库检验点,以JSON形式存放
*/
private String databaseCheckpoint;
/**
* jsonpath检验点,以json形式存放
*/
private String jsonpathCheckpoint;
/**
* 详情参数
*/
private String detail;
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import org.matrix.exception.GlobalException;
import org.matrix.testNg.TestPigeon;
import org.testng.*;
import org.testng.xml.XmlSuite;
......@@ -11,7 +12,6 @@ import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -28,8 +28,7 @@ public class GenerateReporter implements IReporter {
private static final String FILE_NAME = STRING + "-testNg.html";
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
String outputDirectory) {
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
try {
//freemaker的配置
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
......@@ -38,31 +37,23 @@ public class GenerateReporter implements IReporter {
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
//freemaker的模板文件
Template temp = cfg.getTemplate("overview.ftl");
Map context = new HashMap();
for (ISuite suite : suites) {
Map<String, ISuiteResult> suiteResults = suite.getResults();
for (ISuiteResult suiteResult : suiteResults.values()) {
ReporterData data = new ReporterData();
ITestContext testContext = suiteResult.getTestContext();
// 把数据填入上下文 测试结果汇总信息
// 这里是summary中的信息.
context.put("overView", data.testContext(testContext));
//所有的测试方法
ITestNGMethod[] allTests = testContext.getAllTestMethods();
//未执行的测试方法
Collection<ITestNGMethod> excludeTests = testContext.getExcludedMethods();
//测试通过的测试方法
IResultMap passedTests = testContext.getPassedTests();
//测试失败的测试方法
IResultMap failedTests = testContext.getFailedTests();
//测试跳过的测试方法
IResultMap skippedTests = testContext.getSkippedTests();
context.put("pass", data.testResults(passedTests, ITestResult.SUCCESS));
context.put("fail", data.testResults(failedTests, ITestResult.FAILURE));
context.put("skip", data.testResults(skippedTests, ITestResult.FAILURE));
context.put("allTest", allTests);
context.put("excludeTests", excludeTests);
}
}
//文件夹不存在的话进行创建
......@@ -78,8 +69,9 @@ public class GenerateReporter implements IReporter {
writer.flush();
} catch (Exception e) {
throw new GlobalException(e.getMessage());
} finally {
TestPigeon.resultMap = new HashMap<>();
TestPigeon.userResultMap = new HashMap<>();
}
}
}
\ No newline at end of file
package org.matrix.testNg.report;
import org.matrix.database.entity.TestCase;
import org.matrix.database.service.ITestCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.Reporter;
......@@ -8,7 +11,16 @@ import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;
/**
* 工具类
*
* @author mruny
*/
public class ReportUnits {
@Autowired
private ITestCaseService testCaseService;
private static final NumberFormat DURATION_FORMAT = new DecimalFormat("#0.000");
private static final NumberFormat PERCENTAGE_FORMAT = new DecimalFormat("#0.00%");
......@@ -108,4 +120,13 @@ public class ReportUnits {
}
return buffer.toString();
}
private String getName(ITestResult result) {
String name = result.getName();
List<TestCase> list = testCaseService.list();
for (TestCase testCase : list) {
name = testCase.getName();
}
return commaSeparate(Collections.singleton(name));
}
}
\ No newline at end of file
package org.matrix.testNg.report;
import lombok.extern.slf4j.Slf4j;
import org.matrix.actuators.checkpoint.CheckPointResult;
import org.matrix.actuators.usecase.TestCaseExecuteResult;
import org.matrix.database.entity.TestCase;
import org.matrix.database.service.ITestCaseService;
import org.matrix.exception.GlobalException;
import org.matrix.testNg.TestPigeon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.testng.*;
import java.util.*;
......@@ -10,15 +16,18 @@ import java.util.*;
/**
* @author mruny
*/
@Slf4j
@Component
public class ReporterData {
// @Autowired
// private ITestCaseService testCaseService;
//
// public TestCase getTestCase(){
// }
private static ITestCaseService testCaseService;
// 测试结果Set<ITestResult>转为list,再按执行时间排序 ,返回list
@Autowired
private void setITestDataService(ITestCaseService testCaseService) {
ReporterData.testCaseService = testCaseService;
}
//测试结果Set<ITestResult>转为list,再按执行时间排序 ,返回list
public List<ITestResult> sortByTime(Set<ITestResult> str) {
List<ITestResult> list = new ArrayList<ITestResult>();
for (ITestResult r : str) {
......@@ -28,10 +37,12 @@ public class ReporterData {
return list;
}
//模板中的公共部分
public DataBean testContext(ITestContext context) {
// 测试结果汇总数据
DataBean data = new DataBean();
ReportUnits units = new ReportUnits();
//通过,失败,跳过的测试用例
IResultMap passedTests = context.getPassedTests();
IResultMap failedTests = context.getFailedTests();
IResultMap skipedTests = context.getSkippedTests();
......@@ -52,20 +63,37 @@ public class ReporterData {
return data;
}
//每条测试用例的细节
public List<DataBean> testResults(IResultMap map, int status) {
// 测试结果详细数据
List<DataBean> list = new ArrayList<DataBean>();
//工具类
ReportUnits units = new ReportUnits();
map.getAllResults().size();
for (ITestResult result : sortByTime(map.getAllResults())) {
DataBean data = new DataBean();
data.setDuration(units.formatDuration(result.getEndMillis()
- result.getStartMillis()));
data.setParams(units.getParams(result));
String params = data.getParams();
//testCase主键id
String id = params.replace("{id=", "").replace("}", "");
log.info("这里是testResults");
long caseId = Long.parseLong(id);
Map<Long, List<TestCaseExecuteResult>> longListMap = TestPigeon.userResultMap.get(1L);
List<TestCaseExecuteResult> testCaseExecuteResults = longListMap.get(caseId);
TestCase testCase = java.util.Optional.of(testCaseService.getById(caseId))
.orElseThrow(() -> new GlobalException(String.format("没有找到id = %d 的TestCase", caseId)));
data.setTestCaseName(testCase.getName());
data.setOutput(Reporter.getOutput(result));
data.setDependMethod(units.getDependMethods(result));
data.setType(String.valueOf(testCase.getType()));
data.setDetail(testCase.getDetail());
data.setMoveBefore(testCase.getMoveBefore());
data.setMoveAfterCase(testCase.getMoveAfterCase());
data.setMoveAfterTest(testCase.getMoveAfterTest());
data.setDescription(testCase.getDes());
data.setResultMessage("message");
data.setThrowable(result.getThrowable());
if (result.getThrowable() != null) {
data.setStackTrace(result.getThrowable().getStackTrace());
}
......
package org.matrix.testNg.report;
import org.testng.ITestResult;
public class TestResultSort implements Comparable<ITestResult> {
private Long order;
@Override
public int compareTo(ITestResult arg0) {
return this.order.compareTo(arg0.getStartMillis());//按test开始时间排序
}
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论