提交 140784c0 authored 作者: mry's avatar mry

fix(base): 生成简陋报告,完成了基本功能

上级 efbe03c3
...@@ -5,29 +5,28 @@ import freemarker.template.Template; ...@@ -5,29 +5,28 @@ import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler; import freemarker.template.TemplateExceptionHandler;
import org.matrix.exception.GlobalException; import org.matrix.exception.GlobalException;
import org.matrix.testNg.web.entity.DataBean; import org.matrix.testNg.web.entity.DataBean;
import org.matrix.testNg.web.entity.DataMove;
import org.matrix.testNg.web.entity.DataBeans; import org.matrix.testNg.web.entity.DataBeans;
import org.matrix.testNg.web.entity.DataMove;
import org.matrix.testNg.web.entity.ReportMessage; import org.matrix.testNg.web.entity.ReportMessage;
import org.matrix.util.ReporterUtils; import org.matrix.util.ReporterUtils;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
/** /**
* @author mruny * @author mruny
*/ */
public class GenerateReporter { public class GenerateReporter {
private static final LocalDateTime TIME = LocalDateTime.now(); private static final String OUTPUT_FOLDER = System.getProperty("user.dir") + "/htmls";
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"); private static final String FILE_NAME = UUID.randomUUID().toString().replace("-", "") + ".html";
private static final String STRING = DATE_TIME_FORMATTER.format(TIME);
private static final String OUTPUT_FOLDER = System.getProperty("user.dir") + "/"; FileOutputStream out;
private static final String FILE_NAME = STRING + "-testNg.html"; Writer writer;
public ReportMessage generateReport() { public ReportMessage generateReport() {
try { try {
...@@ -35,7 +34,7 @@ public class GenerateReporter { ...@@ -35,7 +34,7 @@ public class GenerateReporter {
cfg.setClassForTemplateLoading(this.getClass(), "/templates"); cfg.setClassForTemplateLoading(this.getClass(), "/templates");
cfg.setDefaultEncoding("UTF-8"); cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
Template temp = cfg.getTemplate("overview.ftl"); Template temp = cfg.getTemplate("overviewBeautiful.ftl");
Map context = new HashMap(); Map context = new HashMap();
ReporterData reporterData = new ReporterData(); ReporterData reporterData = new ReporterData();
DataBean dataBean = reporterData.testDataBean(); DataBean dataBean = reporterData.testDataBean();
...@@ -57,17 +56,27 @@ public class GenerateReporter { ...@@ -57,17 +56,27 @@ public class GenerateReporter {
reportDir.mkdir(); reportDir.mkdir();
} }
// 输出流 // 输出流
FileOutputStream out = new FileOutputStream(OUTPUT_FOLDER + "/" + FILE_NAME); out = new FileOutputStream(OUTPUT_FOLDER + "/" + FILE_NAME);
Writer writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)); writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
// 转换输出 // 转换输出
temp.process(context, writer); temp.process(context, writer);
writer.flush(); writer.flush();
ReportMessage reportMessage = new ReportMessage(); ReportMessage reportMessage = new ReportMessage();
reportMessage.setUrl(OUTPUT_FOLDER + "/" + FILE_NAME); reportMessage.setUrl("/report/" + FILE_NAME);
return reportMessage; return reportMessage;
} catch (Exception e) { } catch (Exception e) {
throw new GlobalException(e.getMessage()); throw new GlobalException(e.getMessage());
} finally { } finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
ReporterUtils.map = new HashMap<>(); ReporterUtils.map = new HashMap<>();
} }
} }
......
...@@ -17,17 +17,18 @@ import java.time.format.DateTimeFormatter; ...@@ -17,17 +17,18 @@ import java.time.format.DateTimeFormatter;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
/** /**
* @author MRY * @author MRY
*/ */
public class GenerateReporterJob { public class GenerateReporterJob {
private static final LocalDateTime TIME = LocalDateTime.now(); private static final String OUTPUT_FOLDER = System.getProperty("user.dir") + "/htmls";
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"); private static final String FILE_NAME = UUID.randomUUID().toString().replace("-", "") + ".html";
private static final String STRING = DATE_TIME_FORMATTER.format(TIME);
private static final String OUTPUT_FOLDER = System.getProperty("user.dir") + "/"; FileOutputStream out;
private static final String FILE_NAME = STRING + "-testNg.html"; Writer writer;
public ReportMessage generateReport() { public ReportMessage generateReport() {
try { try {
...@@ -35,7 +36,7 @@ public class GenerateReporterJob { ...@@ -35,7 +36,7 @@ public class GenerateReporterJob {
cfg.setClassForTemplateLoading(this.getClass(), "/templates"); cfg.setClassForTemplateLoading(this.getClass(), "/templates");
cfg.setDefaultEncoding("UTF-8"); cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
Template temp = cfg.getTemplate("overviews.ftl"); Template temp = cfg.getTemplate("overviewBeautifuls.ftl");
Map context = new HashMap(); Map context = new HashMap();
ReporterDataJob reporterDatajob = new ReporterDataJob(); ReporterDataJob reporterDatajob = new ReporterDataJob();
DataBean dataBean = reporterDatajob.testDataBean(); DataBean dataBean = reporterDatajob.testDataBean();
...@@ -57,17 +58,27 @@ public class GenerateReporterJob { ...@@ -57,17 +58,27 @@ public class GenerateReporterJob {
reportDir.mkdir(); reportDir.mkdir();
} }
// 输出流 // 输出流
FileOutputStream out = new FileOutputStream(OUTPUT_FOLDER + "/" + FILE_NAME); out = new FileOutputStream(OUTPUT_FOLDER + "/" + FILE_NAME);
Writer writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)); writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
// 转换输出 // 转换输出
temp.process(context, writer); temp.process(context, writer);
writer.flush(); writer.flush();
ReportMessage reportMessage = new ReportMessage(); ReportMessage reportMessage = new ReportMessage();
reportMessage.setUrl(OUTPUT_FOLDER + "/" + FILE_NAME); reportMessage.setUrl("/report/" + FILE_NAME);
return reportMessage; return reportMessage;
} catch (Exception e) { } catch (Exception e) {
throw new GlobalException(e.getMessage()); throw new GlobalException(e.getMessage());
} finally { } finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
ReporterUtils.jobMap = new HashMap<>(); ReporterUtils.jobMap = new HashMap<>();
} }
} }
......
...@@ -70,8 +70,8 @@ public class GenerateReporter implements IReporter { ...@@ -70,8 +70,8 @@ public class GenerateReporter implements IReporter {
} catch (Exception e) { } catch (Exception e) {
throw new GlobalException(e.getMessage()); throw new GlobalException(e.getMessage());
} finally { } finally {
ReporterUtils.resultMap = new HashMap<>(); // ReporterUtils.resultMap = new HashMap<>();
ReporterUtils.userResultMap = new HashMap<>(); // ReporterUtils.userResultMap = new HashMap<>();
} }
} }
} }
\ No newline at end of file
...@@ -68,39 +68,39 @@ public class ReporterData { ...@@ -68,39 +68,39 @@ public class ReporterData {
DataBean data = new DataBean(); DataBean data = new DataBean();
// 测试结果详细数据 // 测试结果详细数据
List<DataBean> list = new ArrayList<DataBean>(); List<DataBean> list = new ArrayList<DataBean>();
//工具类 // //工具类
ReportUnits units = new ReportUnits(); // ReportUnits units = new ReportUnits();
for (ITestResult result : sortByTime(map.getAllResults())) { // for (ITestResult result : sortByTime(map.getAllResults())) {
List<TestDataExecuteResult> testDataExecuteResults; // List<TestDataExecuteResult> testDataExecuteResults;
Map<Long, List<TestDataExecuteResult>> longListMap = ReporterUtils.userResultMap.get(1L); // Map<Long, List<TestDataExecuteResult>> longListMap = ReporterUtils.userResultMap.get(1L);
Set<Long> longs = longListMap.keySet(); // Set<Long> longs = longListMap.keySet();
for (Long caseId : longs) { // for (Long caseId : longs) {
TestCase testCase = java.util.Optional.of(testCaseService.getById(caseId)) // TestCase testCase = java.util.Optional.of(testCaseService.getById(caseId))
.orElseThrow(() -> new GlobalException(String.format("没有找到id = %d 的TestCase", caseId))); // .orElseThrow(() -> new GlobalException(String.format("没有找到id = %d 的TestCase", caseId)));
testDataExecuteResults = ReporterUtils.resultMap.get(caseId); // testDataExecuteResults = ReporterUtils.resultMap.get(caseId);
for (TestDataExecuteResult testDataExecuteResult : testDataExecuteResults) { // for (TestDataExecuteResult testDataExecuteResult : testDataExecuteResults) {
data.setDuration(units.formatDuration(result.getEndMillis() // data.setDuration(units.formatDuration(result.getEndMillis()
- result.getStartMillis())); // - result.getStartMillis()));
data.setParams(units.getParams(result)); // data.setParams(units.getParams(result));
CheckPointResult checkPointResult = testDataExecuteResult.getCheckPointResult(); // CheckPointResult checkPointResult = testDataExecuteResult.getCheckPointResult();
data.setTestCaseName(testCase.getName()); // data.setTestCaseName(testCase.getName());
data.setOutput(Reporter.getOutput(result)); // data.setOutput(Reporter.getOutput(result));
data.setDependMethod(units.getDependMethods(result)); // data.setDependMethod(units.getDependMethods(result));
data.setType(String.valueOf(testCase.getType())); // data.setType(String.valueOf(testCase.getType()));
data.setDetail(testCase.getDetail()); // data.setDetail(testCase.getDetail());
data.setMoveBefore(testCase.getMoveBefore()); // data.setMoveBefore(testCase.getMoveBefore());
data.setMoveAfterCase(testCase.getMoveAfterCase()); // data.setMoveAfterCase(testCase.getMoveAfterCase());
data.setMoveAfterTest(testCase.getMoveAfterTest()); // data.setMoveAfterTest(testCase.getMoveAfterTest());
data.setDescription(testCase.getDes()); // data.setDescription(testCase.getDes());
data.setResultMessage(checkPointResult); // data.setResultMessage(checkPointResult);
data.setThrowable(result.getThrowable()); // data.setThrowable(result.getThrowable());
if (result.getThrowable() != null) { // if (result.getThrowable() != null) {
data.setStackTrace(result.getThrowable().getStackTrace()); // data.setStackTrace(result.getThrowable().getStackTrace());
} // }
} // }
} // }
list.add(data); // list.add(data);
} // }
return list; return list;
} }
} }
\ No newline at end of file
package org.matrix.util; package org.matrix.util;
import org.matrix.actuators.usecase.TestCaseExecuteResult; //import org.matrix.actuators.usecase.TestCaseExecuteResult;
import org.matrix.testNg.web.vo.DataBeansJobVo; import org.matrix.testNg.web.vo.DataBeansJobVo;
import org.matrix.testNg.web.vo.DataBeansVo; import org.matrix.testNg.web.vo.DataBeansVo;
...@@ -13,9 +13,9 @@ import java.util.Map; ...@@ -13,9 +13,9 @@ import java.util.Map;
*/ */
public class ReporterUtils { public class ReporterUtils {
public static Map<Long, Map<Long, List<TestCaseExecuteResult>>> userResultMap = new HashMap<>(); // public static Map<Long, Map<Long, List<TestCaseExecuteResult>>> userResultMap = new HashMap<>();
public static Map<Long, List<TestCaseExecuteResult>> resultMap = new HashMap<>(); // public static Map<Long, List<TestCaseExecuteResult>> resultMap = new HashMap<>();
public static Map<String, DataBeansVo> map = new HashMap<>(); public static Map<String, DataBeansVo> map = new HashMap<>();
......
<?xml version="1.0" encoding="utf-8" ?>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta name="description" content="TestNG unit test results."/>
<style type="text/css">
body {
margin: 10px 20px;
font-size: 14px;
font-family: "Arial", "Microsoft YaHei", "黑体", "宋体", sans-serif;
}
.successBtn {
width: 60px;
padding: 3px;
background-color: #58ab48;
border-color: #58ab48;
color: #fff;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
-khtml-border-radius: 10px;
text-align: center;
vertical-align: middle;
border: 1px solid transparent;
font-weight: 500;
}
.failBtn {
width: 60px;
padding: 3px;
background-color: #ab2e2d;
border-color: #ab2e2d;
color: #fff;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px; /* future proofing */
-khtml-border-radius: 10px; /* for old Konqueror browsers */
text-align: center;
vertical-align: middle;
border: 1px solid transparent;
font-weight: 500;
}
</style>
<style>
/* Border styles */
.tabNoBorder thead, .tabNoBorder tr {
border-top-width: 1px;
border-top-style: solid;
border-top-color: rgb(211, 202, 221);
}
.tabNoBorder {
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: rgb(211, 202, 221);
}
/* Padding and font style */
.tabNoBorder td, .tabNoBorder th {
padding: 5px 10px;
font-size: 14px;
font-family: Verdana;
color: rgb(95, 74, 121);
}
/* Alternating background colors */
.tabNoBorder tr:nth-child(even) {
background: rgb(223, 216, 232)
}
.tabNoBorder tr:nth-child(odd) {
background: #FFF
}
</style>
</head>
<body>
<br/>
<h2>Summary</h2>
<table id="summary" class="tabNoBorder">
<tr class="columnHeadings">
<th>用例总数</th>
<th>执行通过</th>
<th>执行失败</th>
<th>执行时间(s)</th>
<th>用例通过率</th>
</tr>
<tr>
<td>${overView.total}</td><#--用例总数-->
<td>${overView.passNum}</td><#--执行通过-->
<td>${overView.failNum}</td><#--执行失败-->
<td>${overView.allTime}</td><#--执行时间(s)-->
<td>${overView.probability}</td><#--用例通过率-->
</tr>
</table>
<br/><br/>
<table id="moveAction" class="tabNoBorder">
<tr class="columnHeadings">
<th>前置动作</th>
<th>中置动作</th>
<th>后置动作</th>
</tr>
<tr>
<td>${move.moveBefore}</td><#--用例总数-->
<td>${move.moveAfterCase}</td><#--未执行用例数-->
<td>${move.moveAfterTest}</td><#--执行通过-->
</tr>
</table>
<br/><br/>
<h2>Detail</h2>
<table class="tabNoBorder">
<tr class="columnHeadings">
<th>编号</th>
<th>用例名称</th>
<th>用例类型</th>
<th>详细参数</th>
<th>用例描述</th>
<th>执行结果</th>
<th>执行时间(s)</th>
<th>结果信息</th>
</tr>
<#assign caseNo = 0>
<#list fail as failCase>
<tr>
<#assign caseNo=caseNo+1>
<td>${caseNo}</td><#--编号-->
<td>${failCase.testCaseName!}</td><#--用例名称-->
<td>${failCase.type!}</td><#--用例类型-->
<td>${failCase.detail!}</td><#--详细参数-->
<td>${failCase.description!}</td><#--用例描述-->
<td>${failCase.result}</td>
<td>${failCase.duration!}</td><#--<th>执行时间(s)</th>-->
<td>${failCase.resultMessage!}</td>
</tr>
</#list>
<#list pass as passCase>
<tr>
<#assign caseNo=caseNo+1>
<td>${caseNo}</td><#--编号-->
<td>${passCase.testCaseName!}</td><#--用例名称-->
<td>${passCase.type!}</td><#--用例类型-->
<td>${passCase.detail!}</td><#--详细参数-->
<td>${passCase.moveBefore!}</td><#--前置动作-->
<td>${passCase.moveAfterCase!}</td><#--中间动作-->
<td>${passCase.moveAfterTest!}</td><#--后置动作-->
<td>${passCase.description}</td><#--用例描述-->
<td>${passCase.result}</td>
<td>${passCase.duration!}</td><#--执行时间(s)-->
<td>${passCase.resultMessage!}</td><#--结果信息-->
</tr>
</#list>
</table>
</body>
</html>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8" ?>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta name="description" content="TestNG unit test results."/>
<style type="text/css">
body {
margin: 10px 20px;
font-size: 14px;
font-family: "Arial", "Microsoft YaHei", "黑体", "宋体", sans-serif;
}
.successBtn {
width: 60px;
padding: 3px;
background-color: #58ab48;
border-color: #58ab48;
color: #fff;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
-khtml-border-radius: 10px;
text-align: center;
vertical-align: middle;
border: 1px solid transparent;
font-weight: 500;
}
.failBtn {
width: 60px;
padding: 3px;
background-color: #ab2e2d;
border-color: #ab2e2d;
color: #fff;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px; /* future proofing */
-khtml-border-radius: 10px; /* for old Konqueror browsers */
text-align: center;
vertical-align: middle;
border: 1px solid transparent;
font-weight: 500;
}
</style>
<style>
/* Border styles */
.tabNoBorder thead, .tabNoBorder tr {
border-top-width: 1px;
border-top-style: solid;
border-top-color: rgb(211, 202, 221);
}
.tabNoBorder {
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: rgb(211, 202, 221);
}
/* Padding and font style */
.tabNoBorder td, .tabNoBorder th {
padding: 5px 10px;
font-size: 14px;
font-family: Verdana;
color: rgb(95, 74, 121);
}
/* Alternating background colors */
.tabNoBorder tr:nth-child(even) {
background: rgb(223, 216, 232)
}
.tabNoBorder tr:nth-child(odd) {
background: #FFF
}
</style>
</head>
<body>
<br/>
<h2>Summary</h2>
<table id="summary" class="tabNoBorder">
<tr class="columnHeadings">
<th>用例总数</th>
<th>执行通过</th>
<th>执行失败</th>
<th>执行时间(s)</th>
<th>用例通过率</th>
</tr>
<tr>
<td>${overView.total!}</td><#--用例总数-->
<td>${overView.passNum!}</td><#--执行通过-->
<td>${overView.failNum!}</td><#--执行失败-->
<td>${overView.allTime!}</td><#--执行时间(s)-->
<td>${overView.probability!}</td><#--用例通过率-->
</tr>
</table>
<br/><br/>
<h2>Move</h2>
<table id="move" class="tabNoBorder">
<tr class="columnHeadings">
<th>前置动作</th>
<th>中置动作</th>
<th>后置动作</th>
</tr>
<tr>
<td>${move.moveBefore!无}</td><#--用例总数-->
<td>${move.moveAfterCase!无}</td><#--未执行用例数-->
<td>${move.moveAfterTest!无}</td><#--执行通过-->
</tr>
</table>
<br/><br/>
<h2>Detail</h2>
<table class="tabNoBorder">
<tr class="columnHeadings">
<th>编号</th>
<th>用例名称</th>
<th>用例类型</th>
<th>详细参数</th>
<th>用例描述</th>
<th>执行结果</th>
<th>执行时间(s)</th>
<th>结果信息</th>
</tr>
<#assign caseNo = 0>
<#if fail ??>
<#list fail as failCase>
<tr>
<#assign caseNo=caseNo+1>
<td>${caseNo}</td><#--编号-->
<td>${failCase.testCaseName!'无'}</td><#--用例名称-->
<td>${failCase.type!'无'}</td><#--用例类型-->
<td>${failCase.detail!'无'}</td><#--详细参数-->
<td>${failCase.description!'无'}</td><#--用例描述-->
<td>${failCase.result?c}</td>
<td>${failCase.duration!'无'}</td><#--<th>执行时间(s)</th>-->
<td>${failCase.resultMessage!'无'}</td>
</tr>
</#list>
<#else>
</#if>
<#if pass ??>
<#list pass as passCase>
<tr>
<#assign caseNo=caseNo+1>
<td>${caseNo}</td><#--编号-->
<td>${passCase.testCaseName!"无"}</td><#--用例名称-->
<td>${passCase.type!'无'}</td><#--用例类型-->
<td>${passCase.detail!'无'}</td><#--详细参数-->
<td>${passCase.description!'无'}</td><#--用例描述-->
<td>${passCase.result?c}</td>
<td>${passCase.duration!'无'}</td><#--执行时间(s)-->
<td>${passCase.resultMessage!'无'}</td><#--结果信息-->
</tr>
</#list>
<#else>
</#if>
</table>
</body>
</html>
\ 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论