欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java自动化测试系列(ZTestReport测试报告)

程序员文章站 2022-05-28 21:33:53
【附源码】无论是Python里的Beautifu Report还是Java中的ZTestReport,仔细阅读源码发现已经实现了诸多非常便利的方法,虽然源码中很多写法冗余和耦合都很严重,但仍旧可以满足普通的使用者,但是对高级使用者而言完全可以修改源码重新编译...

无论是Python里的Beautifu Report还是Java中的ZTestReport,仔细阅读源码发现已经实现了诸多非常便利的方法,虽然源码中很多写法冗余和耦合都很严重,但仍旧可以满足普通的使用者,但是对高级使用者而言完全可以修改源码重新编译

ZTestReport

package util; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Matcher; import org.testng.IReporter; import org.testng.IResultMap; import org.testng.ISuite; import org.testng.ISuiteResult; import org.testng.ITestContext; import org.testng.ITestResult; import org.testng.Reporter; import org.testng.xml.XmlSuite; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class TestReport_original implements IReporter { // 定义生成测试报告的路径和文件名,为兼容Windows和Linux此处使用File.separator代替分隔符 private String path = System.getProperty("user.dir")+File.separator+"report.html"; // 定义html样式模板所在路径 private String templatePath = System.getProperty("user.dir")+File.separator+"template"; private int testsPass = 0; private int testsFail = 0; private int testsSkip = 0; private String beginTime; private long totalTime; private String name; public TestReport_original(){ SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmssSSS"); name = formatter.format(System.currentTimeMillis()); } public TestReport_original(String name){ this.name = name; if(this.name==null){ SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmssSSS"); this.name = formatter.format(System.currentTimeMillis()); } } @Override public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { List<ITestResult> list = new ArrayList<ITestResult>(); for (ISuite suite : suites) { Map<String, ISuiteResult> suiteResults = suite.getResults(); for (ISuiteResult suiteResult : suiteResults.values()) { ITestContext testContext = suiteResult.getTestContext(); IResultMap passedTests = testContext.getPassedTests(); testsPass = testsPass + passedTests.size(); IResultMap failedTests = testContext.getFailedTests(); testsFail = testsFail + failedTests.size(); IResultMap skippedTests = testContext.getSkippedTests(); testsSkip = testsSkip + skippedTests.size(); IResultMap failedConfig = testContext.getFailedConfigurations(); list.addAll(this.listTestResult(passedTests)); list.addAll(this.listTestResult(failedTests)); list.addAll(this.listTestResult(skippedTests)); list.addAll(this.listTestResult(failedConfig)); } } this.sort(list); this.outputResult(list); } private ArrayList<ITestResult> listTestResult(IResultMap resultMap) { Set<ITestResult> results = resultMap.getAllResults(); return new ArrayList<ITestResult>(results); } private void sort(List<ITestResult> list) { Collections.sort(list, new Comparator<ITestResult>() { @Override public int compare(ITestResult r1, ITestResult r2) { if (r1.getStartMillis() > r2.getStartMillis()) { return 1; } else { return -1; } } }); } private void outputResult(List<ITestResult> list) { try { List<ReportInfo> listInfo = new ArrayList<ReportInfo>(); int index = 0; for (ITestResult result : list) { String tn = result.getTestContext().getCurrentXmlTest().getParameter("testCase"); if(index==0){ SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS"); beginTime = formatter.format(new Date(result.getStartMillis())); index++; } long spendTime = result.getEndMillis() - result.getStartMillis(); totalTime += spendTime; String status = this.getStatus(result.getStatus()); List<String> log = Reporter.getOutput(result); for (int i = 0; i < log.size(); i++) { log.set(i, log.get(i).replaceAll("\"", "\\\\\"")); } Throwable throwable = result.getThrowable(); if(throwable!=null){ log.add(throwable.toString().replaceAll("\"", "\\\\\"")); StackTraceElement[] st = throwable.getStackTrace(); for (StackTraceElement stackTraceElement : st) { log.add(("    " + stackTraceElement).replaceAll("\"", "\\\\\"")); } } ReportInfo info = new ReportInfo(); info.setName(tn); info.setSpendTime(spendTime+"ms"); info.setStatus(status); info.setClassName(result.getInstanceName()); info.setMethodName(result.getName()); info.setDescription(result.getMethod().getDescription()); info.setLog(log); listInfo.add(info); } Map<String, Object> result = new HashMap<String, Object>(); result.put("testName", name); result.put("testPass", testsPass); result.put("testFail", testsFail); result.put("testSkip", testsSkip); result.put("testAll", testsPass+testsFail+testsSkip); result.put("beginTime", beginTime); result.put("totalTime", totalTime+"ms"); result.put("testResult", listInfo); Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create(); String template = this.read(templatePath); BufferedWriter output = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(new File(path)),"UTF-8")); template = template.replaceFirst("\\$\\{resultData\\}", Matcher.quoteReplacement(gson.toJson(result))); output.write(template); output.flush(); output.close(); } catch (IOException e) { e.printStackTrace(); } } private String getStatus(int status) { String statusString = null; switch (status) { case 1: statusString = "成功"; break; case 2: statusString = "失败"; break; case 3: statusString = "跳过"; break; default: break; } return statusString; } public static class ReportInfo { private String name; private String className; private String methodName; private String description; private String spendTime; private String status; private List<String> log; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public String getSpendTime() { return spendTime; } public void setSpendTime(String spendTime) { this.spendTime = spendTime; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public List<String> getLog() { return log; } public void setLog(List<String> log) { this.log = log; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } private String read(String path) { File file = new File(path); InputStream is = null; StringBuffer sb = new StringBuffer(); try { is = new FileInputStream(file); int index = 0; byte[] b = new byte[1024]; while ((index = is.read(b)) != -1) { sb.append(new String(b, 0, index)); } return sb.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } } 

测试代码

package testscript; import java.lang.reflect.Method; import org.testng.Assert; import org.testng.Reporter; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Listeners; import org.testng.annotations.Test; import util.TestReport; @Listeners({TestReport.class}) public class Demo_How_to_use_TestReport { @BeforeMethod(description="测试方法前初始化") public void beforeMethod(Method m){ if("testDemo3".equals(m.getName())){ int a = 1/0; System.out.println(a); } } @Test(description="测试DEMO") public void testDemo(){ Reporter.log("this is demo!"); int a = 1/0; System.out.println(a); Assert.assertEquals("a", "b", "should be equals."); } @Test(description="测试DEMO1") public void testDemo1(){ Reporter.log("this is demo!"); Assert.assertEquals("a", "b", "should be equals."); } @Test(description="测试DEMO2",dataProvider="test") public void testDemo2(int a){ Reporter.log("this is demo!"); Assert.assertEquals(a, 1, "should be equals."); } @Test(description="测试DEMO3") public void testDemo3(){ Reporter.log("this is demo!"); Assert.assertEquals("a", "a", "should be equals."); } @DataProvider(name="test") public Object[][] dataProvider(){ return new Object[][]{{1},{2}}; } } 

本文地址:https://blog.csdn.net/dawei_yang000000/article/details/108019573