客戶端地址:https://github.com/anshooarora/extentreports-java/commits/master
服務端地址:https://github.com/anshooarora/extentx
官方DOC:http://extentreports.com/docs/versions/3/java/
實現方式(3種):
- 方式一:直接在@BeforeSuite和@BeforeClass進行初始化
- 方式二:自己實現testng的ITestListener接口
- 方式三:自己實現testng的IReporter接口
以方式三爲例:
POM引用:
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
3.0.7版本可能會存在樣式問題,降了版本之後,樣式就好了。同時還需應用freemarker
實現testng的IReporter接口
初始化 Report
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
htmlReporter.config().setDocumentTitle("ExtentReports - Created by TestNG Listener");
htmlReporter.config().setReportName("ExtentReports - Created by TestNG Listener");
htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
htmlReporter.config().setTheme(Theme.STANDARD);
ExtentReports extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setReportUsesManualConfiguration(true);
生成報告
extent.flush();
執行用例之後,直接瀏覽器打開/test-out/index.html
服務端配置
下載Extentx
下載地址:http://extentreports.com/community/,選擇Extentx下載,並解壓
安裝
前提:mongo以及端口配置正確
MongoDB和NodeJS不在同一環境
1. 運行mongo
2. 進入extentx目錄,並執行:
npm install
sails lift
最後,打開http://localhost:1337/,就可以看到歷史報告了
現在Extentx已經被廢棄了,用klov代替,但klov只支持3.1.0以上版本的extent
下載klov
下載地址:http://extentreports.com/community/,選擇Klov下載,並解壓
安裝
前提:mongo以及端口配置正確
只要啓動jar包就行
java -jar klov-0.0.1.jar
或後臺運行:
nohup java -jar klov-0.0.1.jar &
打開http://localhost:80/就行了。
在執行測試用例之前,還需要配置TestNG的監聽器:
這樣html文件就是由extentReports生成的。
HTML報告:
服務端報告:
監聽器代碼(網上找的):
public class ExtentTestNGIReporterListener implements IReporter {
// 生成的路徑以及文件名
private static final String OUTPUT_FOLDER = "test-output/";
private static final String FILE_NAME = "index.html";
private ExtentReports extent = ExtentManager.getInstance(OUTPUT_FOLDER + FILE_NAME);
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
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;
}
for (ISuiteResult r : result.values()) {
ExtentTest resultNode;
ITestContext context = r.getTestContext();
if (createSuiteResultNode) {
// 沒有創建suite的情況下,將在SuiteResult的創建爲一級節點,否則創建爲suite的一個子節點。
if (null == suiteTest) {
resultNode = extent.createTest(r.getTestContext().getName());
} else {
resultNode = suiteTest.createNode(r.getTestContext().getName());
}
} else {
resultNode = suiteTest;
}
if (resultNode != null) {
resultNode.getModel().setName(suite.getName() + " : " + r.getTestContext().getName());
if (resultNode.getModel().hasCategory()) {
resultNode.assignCategory(r.getTestContext().getName());
} else {
resultNode.assignCategory(suite.getName(), r.getTestContext().getName());
}
resultNode.getModel().setStartTime(r.getTestContext().getStartDate());
resultNode.getModel().setEndTime(r.getTestContext().getEndDate());
// 統計SuiteResult下的數據
int passSize = r.getTestContext().getPassedTests().size();
int failSize = r.getTestContext().getFailedTests().size();
int skipSize = r.getTestContext().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));
}
buildTestNodes(resultNode, context.getFailedTests(), Status.FAIL);
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));
if (suiteFailSize > 0) {
suiteTest.getModel().setStatus(Status.FAIL);
}
}
}
for (String s : Reporter.getOutput()) {
extent.setTestRunnerOutput(s);
}
extent.flush();
}
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();
}*/
// name = result.getTestName();
if (name.length() > 0) {
if (name.length() > 50) {
name = name.substring(0, 49) + "...";
}
} else {
name = result.getMethod().getMethodName();
}
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);
if(!outputList.isEmpty())
for (String output : outputList) {
// 將用例的log輸出報告中
// test.debug(output);
// test.info("output:"+output);
if (result.getThrowable() != null) {
test.log(status, result.getThrowable());
} else {
Markup m = MarkupHelper.createLabel(output, status==Status.PASS?ExtentColor.BLUE:ExtentColor.RED);
// test.log(status, "Test "+ status.toString().toLowerCase() + "ed "+output);
test.log(status, m);
}
}
else
if (result.getThrowable() != null) {
test.log(status, result.getThrowable());
} else {
test.log(status, "Test "+ status.toString().toLowerCase() + "ed");
}
test.getModel().setStartTime(getTime(result.getStartMillis()));
test.getModel().setEndTime(getTime(result.getEndMillis()));
}
}
}
private Date getTime(long millis) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
}
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");
//關聯extentx服務端
<!--extent.attachReporter(createHtmlReporter(filePath), createExtentXReporter());-->
//關聯klov服務端
extent.attachReporter(createHtmlReporter(filePath), createKlovReporter());
}
public static ExtentHtmlReporter createHtmlReporter(String filePath){
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(filePath);
//報表位置
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
//使報表上的圖表可見
htmlReporter.config().setDocumentTitle("ExtentReports");
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setDocumentTitle(filePath);
htmlReporter.config().setEncoding("utf-8");
htmlReporter.config().setReportName("plateClient自動化測試報告");
// 如果cdn.rawgit.com訪問不了,可以設置爲:ResourceCDN.EXTENTREPORTS或者ResourceCDN.GITHUB
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
// htmlReporter.views().dashboard().setSection("Sample", SectionSize.S4, header, list); pro-only
return htmlReporter;
}
public static ExtentXReporter createExtentXReporter() {
ExtentXReporter extentx = new ExtentXReporter("127.0.0.1",27017);
extentx.config().setProjectName("PlatClientTest");
extentx.config().setReportName("PlatClientTest Reports");
extentx.config().setServerUrl("http://localhost:1337");
return extentx;
}
public static KlovReporter createKlovReporter() {
KlovReporter klov = new KlovReporter();
klov.initMongoDbConnection("localhost", 27017);
klov.setKlovUrl("http://localhost:80");
klov.setProjectName("PlatClientTest");
klov.setReportName("buildName");
return klov;
}
}