在本文中,我將詳細介紹如何將 ExtentReports 測試報告與TestNG集成。
主要特點:
TestNG 原生報告有點丑,信息整理有點亂。ExtentReports 是用于替換TestNG 原生報告。當然也可以使用 ReportNg,個人偏好 ExtentReports 樣式。
官網已經給了很多demo了,大家可以參考練習,這里根據個人經驗進行了配置。
引入pom.xml
<!--引入extentreports相關包-->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.1.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>testng-extentsreport</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
<!--引入testng測試框架-->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>compile</scope>
</dependency>
主要基于以下兩項原因:
下載 ExtentReportes 源碼,找到 ExtentTestNgFormatter 類,Listener 目錄下創建 MyExtentTestNgFormatter.java 類直接繼承 ExtentTestNgFormatter 類。
public class MyExtentTestNgFormatter extends ExtentTestNgFormatter {
構造方法加入
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
具體代碼如下:
ublic MyExtentTestNgFormatter() {
setInstance(this);
testRunnerOutput = new ArrayList<>();
String reportPathStr = System.getProperty("reportPath");
File reportPath;
try {
reportPath = new File(reportPathStr);
} catch (NullPointerException e) {
reportPath = new File(TestNG.DEFAULT_OUTPUTDIR);
}
if (!reportPath.exists()) {
if (!reportPath.mkdirs()) {
throw new RuntimeException("Failed to create output run directory");
}
}
File reportFile = new File(reportPath, "report.html");
File emailReportFile = new File(reportPath, "emailable-report.html");
htmlReporter = new ExtentHtmlReporter(reportFile);
EmailReporter emailReporter = new EmailReporter(emailReportFile);
reporter = new ExtentReports();
// 如果cdn.rawgit.com訪問不了,可以設置為:ResourceCDN.EXTENTREPORTS或者ResourceCDN.GITHUB
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
reporter.attachReporter(htmlReporter, emailReporter);
}
重寫onstart 方法功能。新建一個類名為MyReporter,一個靜態ExtentTest的引用。
Listener 包下 MyReporter.java
public class MyReporter {
public static ExtentTest report;
}
MyExtentTestNgFormatter.java
public void onStart(ITestContext iTestContext) {
ISuite iSuite = iTestContext.getSuite();
ExtentTest suite = (ExtentTest) iSuite.getAttribute(SUITE_ATTR);
ExtentTest testContext = suite.createNode(iTestContext.getName());
// 將MyReporter.report靜態引用賦值為testContext。
// testContext是@Test每個測試用例時需要的。report.log可以跟隨具體的測試用例。另請查閱源碼。
MyReporter.report = testContext;
iTestContext.setAttribute("testContext", testContext);
}
測試報告默認是在工程根目錄下創建 test-output/ 文件夾下,名為report.html、emailable-report.html。可根據各自需求在構造方法中修改。
public MyExtentTestNgFormatter() {
setInstance(this);
testRunnerOutput = new ArrayList<>();
// reportPath報告路徑
String reportPathStr = System.getProperty("reportPath");
File reportPath;
try {
reportPath = new File(reportPathStr);
} catch (NullPointerException e) {
reportPath = new File(TestNG.DEFAULT_OUTPUTDIR);
}
if (!reportPath.exists()) {
if (!reportPath.mkdirs()) {
throw new RuntimeException("Failed to create output run directory");
}
}
// 報告名report.html
File reportFile = new File(reportPath, "report.html");
// 郵件報告名emailable-report.html
File emailReportFile = new File(reportPath, "emailable-report.html");
htmlReporter = new ExtentHtmlReporter(reportFile);
EmailReporter emailReporter = new EmailReporter(emailReportFile);
reporter = new ExtentReports();
reporter.attachReporter(htmlReporter, emailReporter);
}
report.log 支持多種玩法
// 根據狀態不同添加報告。型如警告
MyReporter.report.log(Status.WARNING, "接口耗時(ms):" + String.valueOf(time));
直接從TestClass 中運行時會報 MyReporter.report 的空指針錯誤,需做判空處理。
在測試集合 testng.xml 文件中導入 Listener 監聽類。
<listeners>
<listener class-name="com.zuozewei.extentreportdemo.listener.MyExtentTestNgFormatter"/>
</listeners>
extent reporters支持報告的配置。目前支持的配置內容有title、主題等。 先在src/resources/目錄下添加 config/report/extent-config.xml。
<?xml version="1.0" encoding="UTF-8"?>
<extentreports>
<configuration>
<timeStampFormat>yyyy-MM-dd HH:mm:ss</timeStampFormat>
<!-- report theme -->
<!-- standard, dark 個人喜好暗色 -->
<theme>dark</theme>
<!-- document encoding -->
<!-- defaults to UTF-8 -->
<encoding>UTF-8</encoding>
<!-- protocol for script and stylesheets -->
<!-- defaults to https -->
<protocol>https</protocol>
<!-- title of the document -->
<documentTitle>QA-接口自動化測試報告</documentTitle>
<!-- report name - displayed at top-nav -->
<reportName>QA-接口自動化測試報告</reportName>
<!-- report headline - displayed at top-nav, after reportHeadline -->
<reportHeadline>接口自動化測試報告</reportHeadline>
<!-- global date format override -->
<!-- defaults to yyyy-MM-dd -->
<dateFormat>yyyy-MM-dd</dateFormat>
<!-- global time format override -->
<!-- defaults to HH:mm:ss -->
<timeFormat>HH:mm:ss</timeFormat>
<!-- custom javascript -->
<scripts>
<![CDATA[
$(document).ready(function() {
});
]]>
</scripts>
<!-- custom styles -->
<styles>
<![CDATA[
]]>
</styles>
</configuration>
</extentreports>
config下新建 MySystemInfo類繼承 SystemInfo 接口
public class MySystemInfo implements SystemInfo {
@Override
public Map<String, String> getSystemInfo() {
Map<String, String> systemInfo = new HashMap<>();
systemInfo.put("測試人員", "zuozewei");
return systemInfo;
}
}
可用于添加系統信息,例如:db的配置信息,人員信息,環境信息等。根據項目實際情況添加。 至此,extentreports 美化報告完成。
public class TestMethodsDemo {
@Test
public void test1(){
Assert.assertEquals(1,2);
}
@Test
public void test2(){
Assert.assertEquals(1,1);
}
@Test
public void test3(){
Assert.assertEquals("aaa","aaa");
}
@Test
public void logDemo(){
Reporter.log("這是故意寫入的日志");
throw new RuntimeException("故意運行時異常");
}
}
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="測試demo" verbose="1" preserve-order="true">
<parameter name="report.config" value="src/main/resources/report/extent-config.xml"/>
<parameter name="system.info" value="com.zuozewei.extentreportdemo.config.MySystemInfo"/>
<test name="測試demo" preserve-order="true">
<classes>
<class name="com.zuozewei.extentreportdemo.testCase.TestMethodsDemo"/>
</classes>
</test>
<listeners>
<listener class-name="com.zuozewei.extentreportdemo.listener.MyExtentTestNgFormatter"/>
</listeners>
</suite>
2、Email Report 示例
五、工程目錄
源碼地址:
最新簡約美觀的網址網站引導頁HTML源碼 帶一言 隨機大圖
CSS 代碼,這能使得頁面體積大幅精簡。除此之外減少 HTML 文檔大小還可以采取下面幾種方法:1.刪掉 HTM 文檔對執行結果無影響的空格空行和注釋避免 Table 布局;2.使用 HTML5;3.顯式指定文檔字符集。
13.顯式設置圖片的寬高
14.避免腳本阻塞加載:
當瀏覽器在解析常規的 script 標簽時,它需要等待 script 下載完畢,再解析執行,而后續的 HTML 代碼只能等待。為了避免阻塞加載,應把腳步放到文檔的末尾,如把 script 標簽插入在 body 結束標簽之前。
#挑戰30天在頭條寫日記#
*請認真填寫需求信息,我們會在24小時內與您取得聯系。