Skip to content

Commit 4e8a466

Browse files
BAEL-768 - Simplifying report generation code
1 parent 9f031bb commit 4e8a466

2 files changed

Lines changed: 90 additions & 45 deletions

File tree

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,83 @@
11
package com.baeldung.reports;
22

3-
import org.testng.IReporter;
4-
import org.testng.IResultMap;
5-
import org.testng.ISuite;
6-
import org.testng.ISuiteResult;
7-
import org.testng.ITestContext;
8-
import org.testng.ITestResult;
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.testng.*;
96
import org.testng.xml.XmlSuite;
107

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;
8+
import java.io.*;
9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
1611
import java.util.List;
1712
import java.util.Map;
18-
import java.util.Set;
13+
import java.util.stream.Stream;
1914

2015
public class CustomisedReports implements IReporter {
21-
private PrintWriter reportWriter;
16+
private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class);
2217

2318
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-
}
19+
String reportTemplate = initReportTemplate();
3020
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) {
21+
StringBuilder rows = new StringBuilder();
22+
suites.forEach(suite -> {
3323
Map<String, ISuiteResult> suiteResults = suite.getResults();
3424
suiteResults.forEach((testName, suiteResult) -> {
25+
3526
ITestContext testContext = suiteResult.getTestContext();
3627

37-
IResultMap failedResult = testContext.getFailedTests();
38-
Set<ITestResult> testsFailed = failedResult.getAllResults();
39-
for (ITestResult testResult : testsFailed) {
40-
reportWriter.println(String.format(resultRow, "danger", suite.getName(), testName, testResult.getName(), "FAILED", "NA"));
41-
}
28+
Stream<ITestResult> failedTests = testContext.getFailedTests().getAllResults().stream();
29+
Stream<ITestResult> passedTests = testContext.getPassedTests().getAllResults().stream();
30+
Stream<ITestResult> skippedTests = testContext.getSkippedTests().getAllResults().stream();
31+
32+
String suiteName = suite.getName();
33+
34+
Stream<ITestResult> allTestResults = Stream.concat(Stream.concat(failedTests, passedTests), skippedTests);
35+
generateReportRows(resultRow, rows, testName, suiteName, allTestResults);
36+
});
37+
});
38+
reportTemplate = reportTemplate.replaceFirst("</tbody>", rows.toString() + "</tbody>");
39+
saveReportTemplate(outputDirectory, reportTemplate);
40+
}
4241

43-
IResultMap passResult = testContext.getPassedTests();
44-
Set<ITestResult> testsPassed = passResult.getAllResults();
45-
for (ITestResult testResult : testsPassed) {
46-
reportWriter.println(String.format(resultRow, "success", suite.getName(), testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis())));
42+
private void generateReportRows(String resultRow, StringBuilder rows, String testName, String suiteName, Stream<ITestResult> allTestResults) {
43+
allTestResults
44+
.forEach(testResult -> {
45+
String testReportRow = "";
46+
if (testResult.getStatus() == ITestResult.FAILURE) {
47+
testReportRow = String.format(resultRow, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA");
4748
}
49+
if (testResult.getStatus() == ITestResult.SUCCESS) {
50+
testReportRow = String.format(resultRow, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis()));
4851

49-
IResultMap skippedResult = testContext.getSkippedTests();
50-
Set<ITestResult> testsSkipped = skippedResult.getAllResults();
51-
for (ITestResult testResult : testsSkipped) {
52-
reportWriter.println(String.format(resultRow, "warning", suite.getName(), testName, testResult.getName(), "SKIPPED", "NA"));
5352
}
53+
if (testResult.getStatus() == ITestResult.SKIP) {
54+
testReportRow = String.format(resultRow, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA");
55+
}
56+
rows.append(testReportRow);
5457
});
55-
}
56-
finishReportTemplate();
57-
reportWriter.flush();
58-
reportWriter.close();
5958
}
6059

61-
private void initReportTemplate() {
62-
reportWriter.println(
63-
"<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\">"
64-
+ "<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\">");
65-
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>");
60+
private String initReportTemplate() {
61+
String template = null;
62+
byte[] reportTemplate = null;
63+
try {
64+
reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html"));
65+
template = new String(reportTemplate, "UTF-8");
66+
} catch (IOException e) {
67+
LOGGER.error("Problem initializing template", e);
68+
}
69+
return template;
6670
}
6771

68-
private void finishReportTemplate() {
69-
reportWriter.println(" </tbody></div></body></html>");
72+
private void saveReportTemplate(String outputDirectory, String reportTemplate) {
73+
new File(outputDirectory).mkdirs();
74+
try {
75+
PrintWriter reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDirectory, "my-report.html"))));
76+
reportWriter.println(reportTemplate);
77+
reportWriter.flush();
78+
reportWriter.close();
79+
} catch (IOException e) {
80+
LOGGER.error("Problem saving template", e);
81+
}
7082
}
7183
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<html>
2+
<head><title>My Custom Report</title>
3+
<meta name="viewport" content="width=device-width, initial-scale=1">
4+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
5+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
6+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
7+
</head>
8+
<body>
9+
<div class="container">
10+
<table class="table">
11+
<thead>
12+
<tr>
13+
<th>Suite</th>
14+
<th>Test</th>
15+
<th>Method</th>
16+
<th>Status</th>
17+
<th>Execution Time(ms)</th>
18+
</tr>
19+
</thead>
20+
<tbody>
21+
<tr class="success">
22+
<td>My test suite</td>
23+
<td>numbersXML</td>
24+
<td>givenNumberObjectFromDataProvider_ifEvenCheckOK_thenCorrect</td>
25+
<td>PASSED</td>
26+
<td>0</td>
27+
</tr>
28+
29+
</tbody>
30+
</table>
31+
</div>
32+
</body>
33+
</html>

0 commit comments

Comments
 (0)