用到的依赖包:testng-6.9.9.jar,velocity-1.7.jar
1.定义一个DataBean,保存需要收集的数据
只定义部分数据,比如suite、testname、groups等好多数据还没,需要用到的时候再加了
1 package com.reporter.main; 2 3 import java.util.Collection; 4 import java.util.List; 5 6 import org.testng.ITestNGMethod; 7 8 public class DataBean { 9 private int excludeTestsSize; //未执行的test数量 10 private int passedTestsSize; //测试通过的数量 11 private int failedTestsSize; //测试失败的数量 12 private int skippedTestsSize; //测试跳过的数量 13 private int allTestsSize; //全部执行的测试的数量 14 private ITestNGMethod[] allTestsMethod; //全部执行的测试方法 15 private Collection<ITestNGMethod> excludeTestsMethod; //未执行的测试方法 16 private String testsTime; //测试耗时 17 private String passPercent; //测试通过率 18 private String testName; //测试方法名 19 private String className; //测试类名 20 private String duration; //单个测试周期 21 private String params; //测试用参数 22 private String description; //测试描述 23 private List<String> output; //Reporter Output 24 private String dependMethod; //测试依赖方法 25 private Throwable throwable; //测试异常原因 26 private StackTraceElement[] stackTrace; // 异常堆栈信息 27 28 public int getExcludeTestsSize() { 29 return excludeTestsSize; 30 } 31 32 public void setExcludeTestsSize(int excludeTestsSize) { 33 this.excludeTestsSize = excludeTestsSize; 34 } 35 36 public int getPassedTestsSize() { 37 return passedTestsSize; 38 } 39 40 public void setPassedTestsSize(int passedTestsSize) { 41 this.passedTestsSize = passedTestsSize; 42 } 43 44 public int getFailedTestsSize() { 45 return failedTestsSize; 46 } 47 48 public void setFailedTestsSize(int failedTestsSize) { 49 this.failedTestsSize = failedTestsSize; 50 } 51 52 public int getSkippedTestsSize() { 53 return skippedTestsSize; 54 } 55 56 public void setSkippedTestsSize(int skippedTestsSize) { 57 this.skippedTestsSize = skippedTestsSize; 58 } 59 60 public int getAllTestsSize() { 61 return allTestsSize; 62 } 63 64 public void setAllTestsSize(int allTestsSize) { 65 this.allTestsSize = allTestsSize; 66 } 67 68 public String getPassPercent() { 69 return passPercent; 70 } 71 72 public void setPassPercent(String passPercent) { 73 this.passPercent = passPercent; 74 } 75 76 public String getTestName() { 77 return testName; 78 } 79 80 public void setTestName(String testName) { 81 this.testName = testName; 82 } 83 84 public String getClassName() { 85 return className; 86 } 87 88 public void setClassName(String className) { 89 this.className = className; 90 } 91 92 public String getDuration() { 93 return duration; 94 } 95 96 public void setDuration(String duration) { 97 this.duration = duration; 98 } 99 100 public String getParams() { 101 return params; 102 } 103 104 public void setParams(String params) { 105 this.params = params; 106 } 107 108 public String getDescription() { 109 return description; 110 } 111 112 public void setDescription(String description) { 113 this.description = description; 114 } 115 116 public List<String> getOutput() { 117 return output; 118 } 119 120 public void setOutput(List<String> output) { 121 this.output = output; 122 } 123 124 public String getDependMethod() { 125 return dependMethod; 126 } 127 128 public void setDependMethod(String dependMethod) { 129 this.dependMethod = dependMethod; 130 } 131 132 public Throwable getThrowable() { 133 return throwable; 134 } 135 136 public void setThrowable(Throwable throwable2) { 137 this.throwable = throwable2; 138 } 139 140 public StackTraceElement[] getStackTrace() { 141 return stackTrace; 142 } 143 144 public void setStackTrace(StackTraceElement[] stackTrace) { 145 this.stackTrace = stackTrace; 146 } 147 148 public void setTestsTime(String testsTime) { 149 this.testsTime = testsTime; 150 } 151 152 public String getTestsTime() { 153 return testsTime; 154 } 155 156 public void setAllTestsMethod(ITestNGMethod[] allTestsMethod) { 157 this.allTestsMethod = allTestsMethod; 158 } 159 160 public ITestNGMethod[] getAllTestsMethod() { 161 return allTestsMethod; 162 } 163 164 public void setExcludeTestsMethod(Collection<ITestNGMethod> excludeTestsMethod) { 165 this.excludeTestsMethod = excludeTestsMethod; 166 } 167 168 public Collection<ITestNGMethod> getExcludeTestsMethod() { 169 return excludeTestsMethod; 170 } 171 172 } |
2.对需要特别处理的报告元素,比如测试周期、通过率
1 package com.reporter.main; 2 3 import java.text.DecimalFormat; 4 import java.text.NumberFormat; 5 import java.util.ArrayList; 6 import java.util.Arrays; 7 import java.util.Collection; 8 import java.util.Iterator; 9 import java.util.List; 10 import org.testng.ITestContext; 11 import org.testng.ITestResult; 12 import org.testng.Reporter; 13 14 public class ReportUnits { 15 private static final NumberFormat DURATION_FORMAT = new DecimalFormat("#0.000"); 16 private static final NumberFormat PERCENTAGE_FORMAT = new DecimalFormat("#0.00%"); 17 /** 18 *测试消耗时长 19 *return 秒,保留3位小数 20 */ 21 public String getTestDuration(ITestContext context){ 22 long duration; 23 duration=context.getEndDate().getTime()-context.getStartDate().getTime(); 24 return formatDuration(duration); 25 } 26 27 public String formatDuration(long elapsed) 28 { 29 double seconds = (double) elapsed / 1000; 30 return DURATION_FORMAT.format(seconds); 31 } 32 /** 33 *测试通过率 34 *return 2.22%,保留2位小数 35 */ 36 public String formatPercentage(int numerator, int denominator) 37 { 38 return PERCENTAGE_FORMAT.format(numerator / (double) denominator); 39 } 40 41 /** 42 * 获取方法参数,以逗号分隔 43 * @param result 44 * @return 45 */ 46 public String getParams(ITestResult result){ 47 Object[] params = result.getParameters(); 48 List<String> list = new ArrayList<String>(params.length); 49 for (Object o:params){ 50 list.add(renderArgument(o)); 51 } 52 return commaSeparate(list); 53 } 54 /** 55 * 获取依赖的方法 56 * @param result 57 * @return 58 */ 59 public String getDependMethods(ITestResult result){ 60 String[] methods=result.getMethod().getMethodsDependedUpon(); 61 return commaSeparate(Arrays.asList(methods)); 62 } 63 /** 64 * 堆栈轨迹,暂不确定怎么做,放着先 65 * @param throwable 66 * @return 67 */ 68 public String getCause(Throwable throwable){ 69 StackTraceElement[] stackTrace=throwable.getStackTrace(); //堆栈轨迹 70 List<String> list = new ArrayList<String>(stackTrace.length); 71 for (Object o:stackTrace){ 72 list.add(renderArgument(o)); 73 } 74 return commaSeparate(list); 75 } 76 /** 77 * 获取全部日志输出信息 78 * @return 79 */ 80 public List<String> getAllOutput(){ 81 return Reporter.getOutput(); 82 } 83 84 /** 85 * 按testresult获取日志输出信息 86 * @param result 87 * @return 88 */ 89 public List<String> getTestOutput(ITestResult result){ 90 return Reporter.getOutput(result); 91 } 92 93 94 /*将object 转换为String*/ 95 private String renderArgument(Object argument) 96 { 97 if (argument == null) 98 { 99 return "null"; 100 } 101 else if (argument instanceof String) 102 { 103 return "\"" + argument + "\""; 104 } 105 else if (argument instanceof Character) 106 { 107 return "\'" + argument + "\'"; 108 } 109 else 110 { 111 return argument.toString(); 112 } 113 } 114 /*将集合转换为以逗号分隔的字符串*/ 115 private String commaSeparate(Collection<String> strings) 116 { 117 StringBuilder buffer = new StringBuilder(); 118 Iterator<String> iterator = strings.iterator(); 119 while (iterator.hasNext()) 120 { 121 String string = iterator.next(); 122 buffer.append(string); 123 if (iterator.hasNext()) 124 { 125 buffer.append(", "); 126 } 127 } 128 return buffer.toString(); 129 } 130 } |
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。