無意之間在整理testng 報告輸出的文檔時,發現一個美化testng的報告的插件,感覺確實“漂亮”,但是還不確定是否實用,案例來自官方網站自己添了一些內容,更改了存放路徑,本地目前已確定可正常運行,官方網址:http://extentreports.com/documentation/version-2/
1.配置maven依賴
<dependency> <groupId>com.relevantcodes</groupId> <artifactId>extentreports</artifactId> <version>2.40.2</version> </dependency>
2.創建一個測試類,我是mytest
自己添加了5個測試方法,3個正確,2個錯誤
查看展示效果
demo代碼
package com.test.appuimtest;
import org.apache.tools.ant.util.facade.FacadeTaskHelper;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import until.publicmethod;
public class mytest {
public class SingleLogTest extends BaseExample {
@Test
public void passTest() {
test = extent.startTest("passTest");
test.log(LogStatus.PASS, "Pass");
Assert.assertEquals(test.getRunStatus(), LogStatus.PASS);
}
@Test
public void intentionalFailure() throws Exception {
test = extent.startTest("intentionalFailure");
throw new Exception("intentional failure");
}
@Test
public void Mytest1(){
test = extent.startTest("Mytest1");
Assert.assertTrue(true);
}
@Test
public void Mytest2(){
test = extent.startTest("Mytest2");
Assert.assertTrue(true);
}
@Test
public void Mytest3(){
test = extent.startTest("Mytest2");
Assert.assertTrue(false);
}
}
public static class ExtentManager {
private static ExtentReports extent;
public synchronized static ExtentReports getReporter(String filePath) {
if (extent == null) {
extent = new ExtentReports(filePath, true);
extent
.addSystemInfo("Host Name", "Anshoo")
.addSystemInfo("Environment", "QA");
}
return extent;
}
}
public abstract class BaseExample {
protected ExtentReports extent;
protected ExtentTest test;
final String filePath = "d:\\Extent.html"; //這里需要更改創建的存放路徑
@AfterMethod
protected void afterMethod(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
test.log(LogStatus.FAIL, result.getThrowable());
} else if (result.getStatus() == ITestResult.SKIP) {
test.log(LogStatus.SKIP, "Test skipped " + result.getThrowable());
} else {
test.log(LogStatus.PASS, "Test passed");
}
extent.endTest(test);
extent.flush();
}
@BeforeSuite
public void beforeSuite() {
extent = ExtentManager.getReporter(filePath);
}
@AfterSuite
protected void afterSuite() {
extent.close();
}
}
}
在繼續補充,發現在報告下面還可以增加圖片和視頻,添加出來的效果如下





