25 extentreports/allure美化测试报告
2018-08-07 本文已影响9人
博客已迁移I米阳
最近好多在微信问关于测试报告生成问题。其实TestNG执行结果就已经自带了测试报告,只是比较丑。 这里我就顺便推荐下另外的两种Report,但是文档我就不细写了,只大概写下用法,因为官方文档很详细!!!
extentreports
官方文档:http://extentreports.com/docs/versions/3/java/#testng-examples
testerhome 有人翻译:https://testerhome.com/topics/9994
先睹为快
image.png结合TestNG使用
- pom.xml 文件引入相关包
<!-- pom.xml -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.1.5</version>
</dependency>
- 创建TestNG 的Report监听器:
import com.aventstack.extentreports.*;
import com.aventstack.extentreports.model.Log;
import com.aventstack.extentreports.model.TestAttribute;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import com.frame.tools.DateFormat;
import com.frame.tools.MyFile;
import org.testng.*;
import org.testng.xml.XmlSuite;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExtentTestNGIReporterListener implements IReporter {
//生成的路径以及文件名
private static final String OUTPUT_FOLDER = "test-output/";
private static final String FILE_NAME = "index" + DateFormat.format(DateFormat.REPORT_CSV_FORMAT) + ".html";
private ExtentReports extent;
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
init();
boolean createSuiteNode = false;
if (suites.size() > 1) {
createSuiteNode = true;
}
for (ISuite suite : suites) {
Map<String, ISuiteResult> result = suite.getResults();
//如果suite里面没有任何用例,直接跳过,不在报告里生成
if (result.size() == 0) {
continue;
}
//统计suite下的成功、失败、跳过的总用例数
int suiteFailSize = 0;
int suitePassSize = 0;
int suiteSkipSize = 0;
ExtentTest suiteTest = null;
//存在多个suite的情况下,在报告中将同一个一个suite的测试结果归为一类,创建一级节点。
if (createSuiteNode) {
suiteTest = extent.createTest(suite.getName()).assignCategory(suite.getName());
}
boolean createSuiteResultNode = false;
if (result.size() > 1) {
createSuiteResultNode = true;
}
Date suiteStartTime = null, suiteEndTime = new Date();
for (ISuiteResult r : result.values()) {
ExtentTest resultNode;
ITestContext context = r.getTestContext();
if (createSuiteResultNode) {
//没有创建suite的情况下,将在SuiteResult的创建为一级节点,否则创建为suite的一个子节点。
if (null == suiteTest) {
resultNode = extent.createTest(context.getName());
} else {
resultNode = suiteTest.createNode(context.getName());
}
} else {
resultNode = suiteTest;
}
//TestngRetry testngRetry = new TestngRetry();
//System.out.println("retryCount!!!!!!!!!!!!!!!" + testngRetry.retryCount + "!!!!!!!!!!!!!!!!");
//System.out.println("maxRetryCount!!!!!!!!!!!!!!!" + maxRetryCount + "!!!!!!!!!!!!!!!!");
if (resultNode != null) {
resultNode.assignCategory(suite.getName(), context.getName());
if (suiteStartTime == null) {
suiteStartTime = context.getStartDate();
}
suiteEndTime = context.getEndDate();
resultNode.getModel().setStartTime(context.getStartDate());
resultNode.getModel().setEndTime(context.getEndDate());
//统计SuiteResult下的数据
int passSize = context.getPassedTests().size();
int failSize = context.getFailedTests().size();
int skipSize = context.getSkippedTests().size();
suitePassSize += passSize;
suiteFailSize += failSize;
suiteSkipSize += skipSize;
if (failSize > 0) {
resultNode.getModel().setStatus(Status.FAIL);
}
resultNode.getModel().setDescription(String.format("Pass: %s ; Fail: %s ; Skip: %s ;", passSize, failSize, skipSize));
}
System.out.println("suiteFailSize:" + suiteFailSize);
buildTestNodes(resultNode, context.getFailedTests(), Status.FAIL);
//buildTestNodes(resultNode, context.getFailedTests(), Status.FATAL);
//buildTestNodes(resultNode, context.getSkippedTests(), Status.SKIP);
buildTestNodes(resultNode, context.getPassedTests(), Status.PASS);
}
if (suiteTest != null) {
suiteTest.getModel().setDescription(String.format("Pass: %s ; Fail: %s ; Skip: %s ;", suitePassSize, suiteFailSize, suiteSkipSize));
suiteTest.getModel().setStartTime(suiteStartTime == null ? new Date() : suiteStartTime);
suiteTest.getModel().setEndTime(suiteEndTime);
if (suiteFailSize > 0) {
suiteTest.getModel().setStatus(Status.FAIL);
}
}
}
extent.flush();
}
private void init() {
//文件夹不存在的话进行创建
File reportDir = new File(OUTPUT_FOLDER);
if (!reportDir.exists() && !reportDir.isDirectory()) {
reportDir.mkdir();
}
// 设置生成的html
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
htmlReporter.config().setDocumentTitle(ReportUtil.getReportName());
htmlReporter.config().setReportName(ReportUtil.getReportName());
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
//设置点击效果:.node.level-1 ul{ display:none;} .node.level-1.active ul{display:block;}
//设置系统信息样式:.card-panel.environment th:first-child{ width:30%;}
htmlReporter.config().setCSS(".node.level-1 ul{ display:none;} .node.level-1.active ul{display:block;} .card-panel.environment th:first-child{ width:30%;}");
// 移除按键监听事件
htmlReporter.config().setJS("$(window).off(\"keydown\");");
//设置静态文件的DNS 如果cdn.rawgit.com访问不了,可以设置为:ResourceCDN.EXTENTREPORTS 或者ResourceCDN.GITHUB
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setReportUsesManualConfiguration(true);
// 设置系统信息
Properties properties = System.getProperties();
extent.setSystemInfo("os.name", properties.getProperty("os.name", "未知"));
extent.setSystemInfo("os.arch", properties.getProperty("os.arch", "未知"));
extent.setSystemInfo("os.version", properties.getProperty("os.version", "未知"));
extent.setSystemInfo("java.version", properties.getProperty("java.version", "未知"));
extent.setSystemInfo("java.home", properties.getProperty("java.home", "未知"));
extent.setSystemInfo("user.name", properties.getProperty("user.name", "未知"));
extent.setSystemInfo("user.dir", properties.getProperty("user.dir", "未知"));
}
private void buildTestNodes(ExtentTest extenttest, IResultMap tests, Status status) {
//存在父节点时,获取父节点的标签
String[] categories = new String[0];
if (extenttest != null) {
List<TestAttribute> categoryList = extenttest.getModel().getCategoryContext().getAll();
categories = new String[categoryList.size()];
for (int index = 0; index < categoryList.size(); index++) {
categories[index] = categoryList.get(index).getName();
}
}
ExtentTest test;
if (tests.size() > 0) {
//调整用例排序,按时间排序
Set<ITestResult> treeSet = new TreeSet<ITestResult>(new Comparator<ITestResult>() {
@Override
public int compare(ITestResult o1, ITestResult o2) {
return o1.getStartMillis() < o2.getStartMillis() ? -1 : 1;
}
});
treeSet.addAll(tests.getAllResults());
for (ITestResult result : treeSet) {
Object[] parameters = result.getParameters();
String name = "";
//如果有参数,则使用参数的toString组合代替报告中的name
for (Object param : parameters) {
name += param.toString();
}
if (name.length() > 0) {
if (name.length() > 50) {
name = name.substring(0, 49) + "...";
}
} else {
name = result.getMethod().getMethodName();
}
/* // 遇到skip的就不写入report
if ( status.equals(Status.SKIP)) {
continue;
}*/
if (extenttest == null) {
test = extent.createTest(name);
} else {
//作为子节点进行创建时,设置同父节点的标签一致,便于报告检索。
test = extenttest.createNode(name).assignCategory(categories);
}
//test.getModel().setDescription(description.toString());
//test = extent.createTest(result.getMethod().getMethodName());
for (String group : result.getMethod().getGroups())
test.assignCategory(group);
List<String> outputList = Reporter.getOutput(result);
for (String output : outputList) {
//将用例的log输出报告中
test.debug(output.replaceAll("<", "<").replaceAll(">", ">"));
}
if (result.getThrowable() != null) {
test.log(status, result.getThrowable());
} else {
test.log(status, "Test " + status.toString().toLowerCase() + "ed");
}
//设置log的时间,根据ReportUtil.log()的特定格式进行处理获取数据log的时间
Iterator logIterator = test.getModel().getLogContext().getIterator();
while (logIterator.hasNext()) {
Log log = (Log) logIterator.next();
String details = log.getDetails();
if (details.contains(ReportUtil.getSpiltTimeAndMsg())) {
String time = details.split(ReportUtil.getSpiltTimeAndMsg())[0];
log.setTimestamp(getTime(Long.valueOf(time)));
log.setDetails(details.substring(time.length() + ReportUtil.getSpiltTimeAndMsg().length()));
} else {
log.setTimestamp(getTime(result.getEndMillis()));
}
}
test.getModel().setStartTime(getTime(result.getStartMillis()));
test.getModel().setEndTime(getTime(result.getEndMillis()));
//=================== 插入截图 =========================
// 因为我截图是失败自动截图,根据时间格式生成,所以这里插入截图的思路就是找到遍历对应文件夹下,时间在case
// 开始执行到结束执行之前的图片,不过这个思路感觉不太对
String startTimeStr = String.valueOf(getTime(result.getStartMillis()));
String endTimeStr = String.valueOf(getTime(result.getEndMillis()));
Matcher matcher = Pattern.compile("([0-9]\\d:[0-9]\\d:[0-9]\\d)").matcher(startTimeStr);
if (matcher.find()) {
String timeStr1 = matcher.group();
startTimeStr = timeStr1.replaceAll(":", "");
}
Matcher matcher1 = Pattern.compile("([0-9]\\d:[0-9]\\d:[0-9]\\d)").matcher(endTimeStr);
if (matcher1.find()) {
String timeStr1 = matcher1.group();
endTimeStr = timeStr1.replaceAll(":", "");
}
System.out.println("startTimeStr:" + startTimeStr + "|" + endTimeStr);
// 把失败截图加入
try {
if (status.equals(Status.FAIL)) {
String fileDir = DateFormat.format(DateFormat.CHECK_LOG_FORMAT);
String path = System.getProperty("user.dir");
//test.fail("图片", MediaEntityBuilder.createScreenCaptureFromPath(path + "\\error\\20171122\\20171122_144838.jpg").build());
List fileNameList = MyFile.getFileName(path + "\\error\\" + fileDir);
for (int i = 0; i < fileNameList.size(); i++) {
String fileName = (String) fileNameList.get(i);
System.out.println("fileName:" + fileName);
Matcher matcher2 = Pattern.compile("(\\d{6}.jpg)").matcher(fileName);
if (matcher2.find()) {
String fileTime = matcher2.group().replaceAll(".jpg", "");
System.out.println("fileTime" + fileTime);
if (Integer.parseInt(fileTime) >= Integer.parseInt(startTimeStr) && Integer.parseInt(fileTime) <= Integer.parseInt(endTimeStr) + 2) {
test.fail("错误截图").addScreenCaptureFromPath(path + "\\error\\" + fileDir + "\\" + fileName);
break;
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
//=================== 插入截图 =========================
}
}
}
private Date getTime(long millis) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
}
- 添加ReportUnit
import org.testng.Reporter;
import java.util.Calendar;
public class ReportUtil {
private static String reportName = "WebUI自动化测试报告";
private static String splitTimeAndMsg = "===";
public static void log(String msg) {
long timeMillis = Calendar.getInstance().getTimeInMillis();
Reporter.log(timeMillis + splitTimeAndMsg + msg, true);
}
public static String getReportName() {
return reportName;
}
public static String getSpiltTimeAndMsg() {
return splitTimeAndMsg;
}
}
- 执行xml 添加监听
<listeners>
<listener class-name="com.frame.testng.ExtentTestNGIReporterListener"/>
</listeners>
allure 2.0
官方文档:https://docs.qameta.io/allure/2.0/
比较推荐的原因是支持更多的语言,框架,使用也简单,Jenkins也有对应插件可以直接解析。
先睹为快
image.png结合TestNG使用
- pom.xml文件引入
<properties>
<aspectj.version>1.8.10</aspectj.version>
</properties>
<dependencies>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>LAST_VERSION</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
- 运行便可生成测试报告