官方網(wǎng)站在這里:http://extentreports.relevantcodes.com/
當(dāng)年github上開源,如今pro版本居然收錢了
先說總結(jié):
1. 生成的報(bào)告簡潔美觀,
2.生成的單html方便jenkins集成發(fā)郵件
3.自帶集中展示歷史報(bào)告的服務(wù)端
4.支持java,
5.可定制報(bào)告
6.數(shù)據(jù)入庫
官網(wǎng)提供V2.x版本和V3.x版本,只支持java8。注意
客戶端地址:https://github.com/anshooarora/extentreports-java/commits/master
服務(wù)端地址:https://github.com/anshooarora/extentx
安裝過程,官方文檔上比較消息。
官方說明在這里:http://extentreports.com/docs/versions/3/java/, 提供了3種和testng集成示例:
1.直接在@BeforeSuite和@BeforeClass進(jìn)行初始化
2.自己實(shí)現(xiàn)testng的ITestListener接口,監(jiān)聽的適合你已經(jīng)有測試狂簡了,不想動(dòng)原來的case
3.自己實(shí)現(xiàn)testng的IReporter接口,更加直觀
以上隨便選擇一種都可以,實(shí)現(xiàn)ITestListener接口。內(nèi)容類似下面:
public class ExtentTestNGITestListener implements ITestListener {
private static ExtentReports extent = ExtentManager.getInstance("test-output/extent.html");
private static ThreadLocal test = new ThreadLocal();
public static MacacaClient driver;
@Override
public synchronized void onStart(ITestContext context) {
}
@Override
public synchronized void onFinish(ITestContext context) {
extent.flush();
}
@Override
public synchronized void onTestStart(ITestResult result) {
test.set(extent.createTest(result.getMethod().getMethodName()));
}
@Override
public synchronized void onTestSuccess(ITestResult result) {
((ExtentTest)test.get()).pass("Test passed");
}
@Override
public synchronized void onTestFailure(ITestResult result) {
((ExtentTest)test.get()).fail(result.getThrowable());
File directory = new File("test-output");
try {
String screenPath = directory.getCanonicalPath() + "/";
File file = new File(screenPath);
if (!file.exists()){
file.mkdirs();
}
String fileName = result.getMethod().getMethodName() + ".png";
driver.saveScreenshot(screenPath + fileName);
((ExtentTest)test.get()).addScreenCaptureFromPath(screenPath + fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public synchronized void onTestSkipped(ITestResult result) {
((ExtentTest)test.get()).skip(result.getThrowable());
}
@Override
public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) {
}
}
onTestFailure函數(shù)里面實(shí)現(xiàn)了出錯(cuò)自動(dòng)截圖,調(diào)用的是ExtentTest的addScreenCaptureFromPath方法。
ExtentManager用來做初始化:
public class ExtentManager {
private static ExtentReports extent;
public static ExtentReports getInstance(String filePath) {
if (extent == null)
createInstance(filePath);
return extent;
}
public static void createInstance(String filePath) {
extent = new ExtentReports();
extent.setSystemInfo("os", "Linux");
extent.attachReporter(createHtmlReporter(filePath), createExtentXReporter());
}
public static ExtentHtmlReporter createHtmlReporter(String filePath){
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(filePath);
//報(bào)表位置
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
//使報(bào)表上的圖表可見
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setDocumentTitle(filePath);
htmlReporter.config().setEncoding("utf-8");
htmlReporter.config().setReportName("XXX項(xiàng)目測試報(bào)告");
return htmlReporter;
}
public static ExtentXReporter createExtentXReporter() {
ExtentXReporter extentx = new ExtentXReporter("127.0.0.1",27017);
extentx.config().setProjectName("test1");
extentx.config().setReportName("Build-1224");
extentx.config().setServerUrl("http://localhost:1337");
return extentx;
}
ExtentXReporter構(gòu)造函數(shù)里填的是mongodb的地址和端口。
在res/testng.xml里面注冊這個(gè)監(jiān)聽器,測試類也寫上:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="com.util.extent.ExtentTestNGITestListener"></listener>
</listeners>
<test name="Test">
<classes>
<class name="macaca.client.ExtentTestNGReportBuilder"></class>
</classes>
</test>
</suite>
測試類調(diào)用,隨便寫2個(gè)case演示:macaca是阿里的自動(dòng)化框架,也不錯(cuò)的,可以直接刪除也行,內(nèi)容罷了
public class ExtentTestNGReportBuilder {
MacacaClient driver = new MacacaClient();
@BeforeClass
public synchronized void beforeClass() throws Exception {
JSONObject porps = new JSONObject();
porps.put("platformName", "android");
porps.put("reuse", 1);
porps.put("app", "https://npmcdn.com/android-app-bootstrap@latest/android_app_bootstrap/build/outputs/apk/android_app_bootstrap-debug.apk");
JSONObject desiredCapabilities = new JSONObject();
desiredCapabilities.put("desiredCapabilities", porps);
driver.initDriver(desiredCapabilities);
ExtentTestNGITestListener.driver = driver;
}
@AfterClass
public synchronized void afterClass() throws Exception{
driver.quit();
}
@Test
public void test_1() throws Exception{
assert 1==0;
}
@Test
public void test_2() {
assert 1 == 1;
}
}
運(yùn)行命令
mvn clean test
開始測試
生成的本地報(bào)告在test-output下面,內(nèi)容類似下面:
只查看失敗的case:

Dashboard頁面:

看看服務(wù)端的報(bào)告(運(yùn)行了多次):
匯總頁面:
這里的PROJECT和BUILD是ExtentManager類里面
extentx.config().setProjectName("test1");
extentx.config().setReportName("Build-1224");
這里設(shè)定的,這里實(shí)際使用時(shí)可以用jenkins集成時(shí)直接由jenkins傳進(jìn)來
如果覺得這種報(bào)告太笨重了,可以看下這種
http://www.itdecent.cn/p/ce09d3bd1c37
輕量級(jí)別的
ps:所有酷炫的測試報(bào)告,都不適合發(fā)郵件,因?yàn)閮?nèi)容太多了
適合發(fā)郵件的模板去掉各種渲染




