码迷,mamicode.com
首页 > 其他好文 > 详细

How to Adding ExtendReport in test framework

时间:2019-10-05 20:29:22      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:nts   over   bsp   UNC   mode   sem   for   imei   manual   

Step 1: Add the extentreport dependency in POM.xml

<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>3.1.5</version>
</dependency>

Step2: Prepare a ExtentReportListener which implents IReporter.  below is an example.

  1 public class ExtentReportListener implements IReporter {
  2 
  3     private static final String OUTPUT_FOLDER = "target/";
  4     private static final String FILE_NAME = "ExtentReport.html";
  5 
  6     private ExtentReports extent;
  7 
  8 
  9     @Override
 10     public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
 11         init(xmlSuites);
 12 
 13         for (ISuite suite : suites) {
 14             Map<String, ISuiteResult> result = suite.getResults();
 15 
 16             for (ISuiteResult r : result.values()) {
 17                 ITestContext context = r.getTestContext();
 18 
 19                 buildTestNodes(context.getFailedTests(), Status.FAIL);
 20                 buildTestNodes(context.getSkippedTests(), Status.SKIP);
 21                 buildTestNodes(context.getPassedTests(), Status.PASS);
 22 
 23             }
 24         }
 25 
 26         for (String s : Reporter.getOutput()) {
 27             extent.setTestRunnerOutput(s);
 28         }
 29 
 30         extent.flush();
 31     }
 32 
 33 
 34     private void init(List<XmlSuite> xmlSuites) {
 35         String suiteName = xmlSuites.get(0).getName();
 36         ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
 37         htmlReporter.config().setDocumentTitle("ExtentReports: " + suiteName);
 38         htmlReporter.config().setReportName(suiteName);
 39         htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
 40         htmlReporter.config().setTheme(Theme.STANDARD);
 41         htmlReporter.config().setChartVisibilityOnOpen(true);
 42         htmlReporter.config().setEncoding("utf-8");
 43         //Fix the report with css stype
 44         htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
 45 
 46         extent = new ExtentReports();
 47         extent.attachReporter(htmlReporter);
 48         extent.setReportUsesManualConfiguration(true);
 49     }
 50 
 51 
 52     private void buildTestNodes(IResultMap tests, Status status) {
 53         // IResultMap is not sorted. To make it look nice in report first we need to sort tests (sort by start time)
 54         SortedSet<ITestResult> sortedSet = new TreeSet<>();
 55         for (ITestResult result : tests.getAllResults()) {
 56             sortedSet.add(result);
 57         }
 58 
 59         // Now we go through sorted results
 60         ExtentTest test;
 61         if (tests.size() > 0) {
 62             for (ITestResult result : sortedSet) {
 63                 test = extent.createTest(result.getTestContext().getCurrentXmlTest().getName() + " - "
 64                         + result.getMethod().getMethodName());
 65 
 66                 // Using Class short name instead of category
 67                 test.assignCategory(result.getMethod().getRealClass().getSimpleName());
 68 
 69                 Throwable throwable = result.getThrowable();
 70 
 71                 if (throwable != null) {
 72 
 73                     // Add parameters
 74                     Object[] parameters = result.getParameters();
 75                     if (parameters.length > 0) {
 76                         try {
 77                             // If parameters are String
 78                             String params = "";
 79                             for (Object object : parameters) {
 80                                 if (params.isEmpty()) {
 81                                     params = (String) object;
 82                                 } else {
 83                                     params = params + "," + (String) object;
 84                                 }
 85                             }
 86                             test.info(params);
 87                         } catch (ClassCastException e) {
 88                             // If parameters are HashMap
 89                             @SuppressWarnings("unchecked")
 90                             HashMap<String, String> table = (HashMap<String, String>) parameters[0];
 91                             test.info(table.toString());
 92                         }
 93                     } else {
 94                         test.info("This test doesn‘t have data");
 95                     }
 96 
 97                     // Finally, log throwable
 98                     test.log(status, throwable);
 99 
100                 } else {
101                     test.log(status, "Test " + status.toString().toLowerCase() + "ed");
102                 }
103 
104                 test.getModel().setStartTime(getTime(result.getStartMillis()));
105                 test.getModel().setEndTime(getTime(result.getEndMillis()));
106             }
107         }
108     }
109 
110 
111     private Date getTime(long millis) {
112         Calendar calendar = Calendar.getInstance();
113         calendar.setTimeInMillis(millis);
114         return calendar.getTime();
115     }
116 }

Step3: update the testng.xml to add the listener

<listeners>
  <listener class-name="ExtentReportListener" />
</listeners>

Step4: run the test suite and check the report with a browser.   

How to Adding ExtendReport in test framework

标签:nts   over   bsp   UNC   mode   sem   for   imei   manual   

原文地址:https://www.cnblogs.com/amy2012/p/11625433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!