2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.ci.tests.run;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.List;
29 import org.testng.IReporter;
30 import org.testng.IResultMap;
31 import org.testng.ISuite;
32 import org.testng.ISuiteResult;
33 import org.testng.ITestContext;
34 import org.testng.ITestResult;
35 import org.testng.xml.XmlSuite;
37 import com.relevantcodes.extentreports.ExtentReports;
38 import com.relevantcodes.extentreports.ExtentTest;
39 import com.relevantcodes.extentreports.LogStatus;
41 public class ExtentReporterNG implements IReporter {
42 private ExtentReports extent;
45 public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
46 extent = new ExtentReports(outputDirectory + File.separator + "ExtentReportsTestNG.html", true);
48 for (ISuite suite : suites) {
49 Map<String, ISuiteResult> result = suite.getResults();
51 for (ISuiteResult r : result.values()) {
52 ITestContext context = r.getTestContext();
54 buildTestNodes(context.getPassedTests(), LogStatus.PASS);
55 buildTestNodes(context.getFailedTests(), LogStatus.FAIL);
56 buildTestNodes(context.getSkippedTests(), LogStatus.SKIP);
64 private void buildTestNodes(IResultMap tests, LogStatus status) {
67 if (tests.size() > 0) {
68 for (ITestResult result : tests.getAllResults()) {
69 test = extent.startTest(result.getMethod().getMethodName());
71 test.getTest().setStartedTime(getTime(result.getStartMillis()));
72 test.getTest().setEndedTime(getTime(result.getEndMillis()));
74 for (String group : result.getMethod().getGroups())
75 test.assignCategory(group);
77 String message = "Test " + status.toString().toLowerCase() + "ed";
79 if (result.getThrowable() != null)
80 message = result.getThrowable().getMessage();
82 test.log(status, message);
89 private Date getTime(long millis) {
90 Calendar calendar = Calendar.getInstance();
91 calendar.setTimeInMillis(millis);
92 return calendar.getTime();