|
1 | 1 | package com.baeldung.reports; |
2 | 2 |
|
3 | | -import org.slf4j.Logger; |
4 | | -import org.slf4j.LoggerFactory; |
5 | 3 | import org.testng.IReporter; |
| 4 | +import org.testng.IResultMap; |
6 | 5 | import org.testng.ISuite; |
7 | 6 | import org.testng.ISuiteResult; |
8 | 7 | import org.testng.ITestContext; |
| 8 | +import org.testng.ITestResult; |
9 | 9 | import org.testng.xml.XmlSuite; |
10 | 10 |
|
| 11 | +import java.io.BufferedWriter; |
| 12 | +import java.io.File; |
| 13 | +import java.io.FileWriter; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.PrintWriter; |
11 | 16 | import java.util.List; |
12 | 17 | import java.util.Map; |
| 18 | +import java.util.Set; |
13 | 19 |
|
14 | 20 | public class CustomisedReports implements IReporter { |
15 | | - private static final Logger LOGGER = LoggerFactory.getLogger("TEST_REPORT"); |
| 21 | + private PrintWriter reportWriter; |
16 | 22 |
|
17 | | - @Override |
18 | 23 | public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { |
| 24 | + new File(outputDirectory).mkdirs(); |
| 25 | + try { |
| 26 | + reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDirectory, "my-report.html")))); |
| 27 | + } catch (IOException e) { |
| 28 | + e.printStackTrace(); |
| 29 | + } |
| 30 | + String resultRow = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>"; |
| 31 | + initReportTemplate(); |
| 32 | + for (ISuite suite : suites) { |
| 33 | + Map<String, ISuiteResult> suiteResults = suite.getResults(); |
| 34 | + for (String testName : suiteResults.keySet()) { |
| 35 | + ISuiteResult suiteResult = suiteResults.get(testName); |
| 36 | + ITestContext testContext = suiteResult.getTestContext(); |
19 | 37 |
|
20 | | - suites.stream() |
21 | | - .forEach(suite -> { |
22 | | - String suiteName = suite.getName(); |
23 | | - Map<String, ISuiteResult> suiteResults = suite.getResults(); |
24 | | - suiteResults.values() |
25 | | - .stream() |
26 | | - .forEach(result -> { |
27 | | - ITestContext context |
28 | | - = ((ISuiteResult) result).getTestContext(); |
29 | | - |
30 | | - LOGGER.info("Passed tests for suite '" |
31 | | - + suiteName + "' is:" |
32 | | - + context.getPassedTests() |
33 | | - .getAllResults() |
34 | | - .size()); |
35 | | - LOGGER.info("Failed tests for suite '" |
36 | | - + suiteName + "' is:" |
37 | | - + context.getFailedTests() |
38 | | - .getAllResults() |
39 | | - .size()); |
40 | | - LOGGER.info("Skipped tests for suite '" |
41 | | - + suiteName + "' is:" |
42 | | - + context.getSkippedTests() |
43 | | - .getAllResults() |
44 | | - .size()); |
45 | | - }); |
46 | | - }); |
| 38 | + IResultMap failedResult = testContext.getFailedTests(); |
| 39 | + Set<ITestResult> testsFailed = failedResult.getAllResults(); |
| 40 | + for (ITestResult testResult : testsFailed) { |
| 41 | + reportWriter.println(String.format(resultRow, "danger", suite.getName(), testName, testResult.getName(), "FAILED", "NA")); |
| 42 | + } |
47 | 43 |
|
| 44 | + IResultMap passResult = testContext.getPassedTests(); |
| 45 | + Set<ITestResult> testsPassed = passResult.getAllResults(); |
| 46 | + for (ITestResult testResult : testsPassed) { |
| 47 | + reportWriter.println(String.format(resultRow, "success", suite.getName(), testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis()))); |
| 48 | + } |
| 49 | + |
| 50 | + IResultMap skippedResult = testContext.getSkippedTests(); |
| 51 | + Set<ITestResult> testsSkipped = skippedResult.getAllResults(); |
| 52 | + for (ITestResult testResult : testsSkipped) { |
| 53 | + reportWriter.println(String.format(resultRow, "warning", suite.getName(), testName, testResult.getName(), "SKIPPED", "NA")); |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + } |
| 58 | + finishReportTemplate(); |
| 59 | + reportWriter.flush(); |
| 60 | + reportWriter.close(); |
48 | 61 | } |
49 | 62 |
|
| 63 | + private void initReportTemplate() { |
| 64 | + reportWriter.println( |
| 65 | + "<html>" + "<head>" + "<title>My Custom Report</title>" + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" + "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\">" |
| 66 | + + "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js\"></script>" + "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script></head>" + "<body><div class=\"container\">"); |
| 67 | + reportWriter.println("<table class=\"table\"><thead><tr>" + "<th>Suite</th>" + "<th>Test</th>" + "<th>Method</th>" + "<th>Status</th>" + "<th>Execution Time(ms)</th>" + "</tr></thead> <tbody>"); |
| 68 | + } |
| 69 | + |
| 70 | + private void finishReportTemplate() { |
| 71 | + reportWriter.println(" </tbody></div></body></html>"); |
| 72 | + } |
50 | 73 | } |
0 commit comments