热门IT资讯网

testng自定义报告

发表于:2024-11-25 作者:热门IT资讯网编辑
编辑最后更新 2024年11月25日,效果图:代码:package com.rpoter;import java.io.File;import java.io.FileWriter;import java.io.IOException;i

效果图:

代码:

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 xmlSuites, List 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("\nUI自动化测试报告")

.append(HtmlHelper.getCssString("."))

.append("\n")

.append("

").append(title).append("

\n")

.append("

")

.append("

\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 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 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 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 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("

");

sb1.append("

\n");

sb1.append("

手机品牌名称手机系统版本测试包名版本号通过数失败数跳过合计 开始时间 结束时间
"+Mobile_phone+""+MobileSystem+""+versionName+""+package_name+""+Passed+""+Failed+""+Skipped+""+CountNum+""+StartDate+""+EndDate+"

")

.append("

")

.append("

")

.append("

")

.append("

"+"执行用例数:"+CountNum+"
")

.append("

详 情

")

.append("

")

.append("

\n");

/**详细通过数*/

for (ISuite suite : suites) {

Map tests = suite.getResults();

for (ISuiteResult r : tests.values()) {

ITestContext overview = r.getTestContext();

CountNum = overview.getAllTestMethods().length;

Set 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("

");

i++;

}

}

}

sb1.append("

\n");

/**详细失败数*/

for (ISuite suite : suites) {

Map tests = suite.getResults();

for (ISuiteResult r : tests.values()) {

ITestContext overview = r.getTestContext();

CountNum = overview.getAllTestMethods().length;

int i=0;

Set failedSet = overview.getFailedTests().getAllResults();

for(ITestResult f : failedSet){

System.err.println("class: " +f.getTestClass().getName()+ " | method: "+f.getName() + " | error: "+ f.getThrowable());

sb1.append("

");

i++;

}

}

}

sb1.append("

\n");

/**详细跳过数*/

for (ISuite suite : suites) {

Map tests = suite.getResults();

for (ISuiteResult r : tests.values()) {

ITestContext overview = r.getTestContext();

CountNum = overview.getAllTestMethods().length;

Set skippedSet = overview.getSkippedTests().getAllResults();

int i=0;

for(ITestResult s : skippedSet){

System.err.println("---- " +s.getName() );

System.err.println(s.getThrowable());

sb1.append("

");

i++;

}

}

}

sb1.append("

序列号 状 态用例类名详情信息截图
"+i+"通 过"+P***esuTmp+""+"详情信息:"+PassgetName+"通过数无需截图
序列号 状 态用例类名详情信息截图
"+i+"失败"+f.getTestClass().getName()+""+"测试方法名: "+f.getName() + " 失败详情: "+ f.getThrowable()+"截图路径:
序列号 状 态用例类名详情信息截图
"+i+"跳过"+s.getName()+""+"详情信息:"+s.getThrowable()+"该用例跳过没有截图

");


sb1.append("自动化测试部

\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);

}

}


0