效果图:
代码:
package com.rpoter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.reporters.HtmlHelper;
import org.testng.xml.XmlSuite;
public class Reporter implements IReporter {
private String reportPath;
/** 手机型号 */
private String Mobile_phone = “小米手机”;
/** 测试包名 */
private String package_name = “com.systoon.beijingtoon”;
/** 版本号 */
private String versionName = “1.8.1”;
/** 手机系统版本 */
private String MobileSystem = “安卓5.6”;
/** 通过 */
public int Passed = 0;
/** 失败 */
public int Failed = 0;
/** 跳过 */
public int Skipped = 0;
/**用例总共合计*/
public int CountNum = 0;
/**通过结果*/
public String P***esuTmp,PassgetName;
/**开始时间*/
public Date StartDate ;
/**结束时间*/
public Date EndDate ;
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
File htmlReportDir = new File(“test-output/Test-report”);
if (!htmlReportDir.exists()) {
htmlReportDir.mkdirs();
}
String reportName = formateDate() + “.html”;
reportPath = htmlReportDir + “/” + reportName;
File report = new File(htmlReportDir, reportName);
if (report.exists()) {
try {
report.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
StringBuffer sb = new StringBuffer();
String title = “UI自动化测试用例运行结果列表”;
sb.append(“<html>/n<head><title>UI自动化测试报告</title>”)
.append(HtmlHelper.getCssString(“.”))
.append(“</head><body style=/”background-color:#FAEBD7;/”>/n”)
.append(“<h3><p align=’center’>”).append(title).append(“</p></h3>/n”)
.append(“<table cellspacing=’0′ cellpadding=’0′ border=’1′ width=’90%’>”)
.append(“<tr><th>手机品牌名称</th><th>手机系统版本</th><th>测试包名</th><th>版本号</th><th>通过数</th><th>失败数</th><th>跳过</th><th>合计 </th><th>开始时间 </th><th>结束时间 </th></tr>/n”);
String res = sb.toString();
try {
Files.write((Paths.get(reportPath)), res.getBytes(“utf-8”));
} catch (IOException e) {
e.printStackTrace();
}
StringBuilder sb1 = new StringBuilder();
for (ISuite suite : suites) {
Map<String, ISuiteResult> tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
ITestContext overview = r.getTestContext();
System.err.println(“suite: ” + overview.getName());
System.err.println(“Stard Time: “+overview.getStartDate());
StartDate = overview.getStartDate();
System.err.println(“End Time: “+overview.getEndDate());
EndDate = overview.getEndDate();
CountNum = overview.getAllTestMethods().length;
System.err.println(“all methods num : “+overview.getAllTestMethods().length);
//passed
Passed = overview.getPassedTests().size();
System.err.println(“passed: ” + overview.getPassedTests().size());
Set<ITestResult> passedSet = overview.getPassedTests().getAllResults();
for(ITestResult p : passedSet){
System.err.println(“class: ” +p.getTestClass().getName() + ” | method: “+p.getName());
}
//failed
Failed = overview.getFailedTests().size();
System.err.println(“failed: ” + overview.getFailedTests().size());
Set<ITestResult> failedSet = overview.getFailedTests().getAllResults();
for(ITestResult f : failedSet){
System.err.println(“class: ” +f.getTestClass().getName()+ ” | method: “+f.getName() + ” | error: “+ f.getThrowable());
}
//skipped
Skipped = overview.getSkippedTests().size();
System.err.println(“skipped: ” + overview.getSkippedTests().size());
Set<ITestResult> skippedSet = overview.getSkippedTests().getAllResults();
for(ITestResult s : skippedSet){
System.err.println(“—- ” +s.getName() );
System.err.println(s.getThrowable());
}
System.err.println(“==================================”);
}
}
String reportN = “/”+formateDate() +”index”+”.html”;
sb1.append(“<td>”+Mobile_phone+”</td><td>”+MobileSystem+”</td><td>”+versionName+”</td><td>”+package_name+”</td>”);
sb1.append(“<td>”+Passed+”</td><td>”+Failed+”</td><td>”+Skipped+”</td><td>”+CountNum+”</td><td>”+StartDate+”</td><td>”+EndDate+”</td>/n”);
sb1.append(“</table><br/>”)
.append(“<pre></pre>”)
.append(“<table cellspacing=’0′ cellpadding=’0′ border=’0′ width=’90%’>”)
.append(“<tr><th>”+”执行用例数:”+CountNum+”</th></tr>”)
.append(“</table>”)
.append(“<tbody style=/”word-wrap:break-word;font-weight:bold;/” align=/”center/”><h3>详 情</h3>”)
.append(“<table cellspacing=’0′ cellpadding=’0′ border=’1′ width=’90%’>”)
.append(“<tr><th>序列号 </th><th>状 态</th><th>用例类名</th><th>详情信息</th><th>截图</th></tr>/n”);
/**详细通过数*/
for (ISuite suite : suites) {
Map<String, ISuiteResult> tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
ITestContext overview = r.getTestContext();
CountNum = overview.getAllTestMethods().length;
Set<ITestResult> passedSet = overview.getPassedTests().getAllResults();
int i=0;
for(ITestResult p : passedSet){
System.err.println(“class: ” +p.getTestClass().getName() + ” | method: “+p.getName());
P***esuTmp = p.getTestClass().getName();
PassgetName =p.getName();
sb1.append(“<tr><td>”+i+”</td><td><font color=/”#00FF00/”>通 过</font></td><td>”+P***esuTmp+”</td><td>”+”详情信息:”+PassgetName+”</td><td><font color=/”#00FF00/”>通过数无需截图</font></td></tr>”);
i++;
}
}
}
sb1.append(“<tr><th>序列号 </th><th>状 态</th><th>用例类名</th><th>详情信息</th><th>截图</th></tr>/n”);
/**详细失败数*/
for (ISuite suite : suites) {
Map<String, ISuiteResult> tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
ITestContext overview = r.getTestContext();
CountNum = overview.getAllTestMethods().length;
int i=0;
Set<ITestResult> failedSet = overview.getFailedTests().getAllResults();
for(ITestResult f : failedSet){
System.err.println(“class: ” +f.getTestClass().getName()+ ” | method: “+f.getName() + ” | error: “+ f.getThrowable());
sb1.append(“<tr><td>”+i+”</td><td><font color=/”FF0000/”>失败</font></td><td>”+f.getTestClass().getName()+”</td><td>”+”测试方法名: “+f.getName() + ” 失败详情: “+ f.getThrowable()+”</td><td>截图路径:</td></tr>”);
i++;
}
}
}
sb1.append(“<tr><th>序列号 </th><th>状 态</th><th>用例类名</th><th>详情信息</th><th>截图</th></tr>/n”);
/**详细跳过数*/
for (ISuite suite : suites) {
Map<String, ISuiteResult> tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
ITestContext overview = r.getTestContext();
CountNum = overview.getAllTestMethods().length;
Set<ITestResult> skippedSet = overview.getSkippedTests().getAllResults();
int i=0;
for(ITestResult s : skippedSet){
System.err.println(“—- ” +s.getName() );
System.err.println(s.getThrowable());
sb1.append(“<tr><td>”+i+”</td><td><font color=/”FF9966/”>跳过</font></td><td>”+s.getName()+”</td><td>”+”详情信息:”+s.getThrowable()+”</td><td><font color=/”FF9966/”>该用例跳过没有截图</font></td></tr>”);
i++;
}
}
}
sb1.append(“</table><br/>”);
sb1.append(“</tbody></table><a href=/”#top/”>自动化测试部</a></div></body></html>/n”);
String res1 = sb1.toString();
try {
Files.write((Paths.get(reportPath)), res1.getBytes(“utf-8”), StandardOpenOption.APPEND);
FileWriter writer = new FileWriter(“.//test-output//results.txt”, false);
writer.write(Passed+”:”);
writer.write(Failed+”:”);
writer.write(Skipped+””);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String formateDate() {
SimpleDateFormat sf = new SimpleDateFormat(“yyyy-MM-dd-HH-mm-ss”);
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
return sf.format(date);
}
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/191548.html