BAEL-768 - Simplifying report generation code
This commit is contained in:
parent
9f031bb287
commit
4e8a466b45
@ -1,71 +1,83 @@
|
|||||||
package com.baeldung.reports;
|
package com.baeldung.reports;
|
||||||
|
|
||||||
import org.testng.IReporter;
|
import org.slf4j.Logger;
|
||||||
import org.testng.IResultMap;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.testng.ISuite;
|
import org.testng.*;
|
||||||
import org.testng.ISuiteResult;
|
|
||||||
import org.testng.ITestContext;
|
|
||||||
import org.testng.ITestResult;
|
|
||||||
import org.testng.xml.XmlSuite;
|
import org.testng.xml.XmlSuite;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.*;
|
||||||
import java.io.File;
|
import java.nio.file.Files;
|
||||||
import java.io.FileWriter;
|
import java.nio.file.Paths;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class CustomisedReports implements IReporter {
|
public class CustomisedReports implements IReporter {
|
||||||
private PrintWriter reportWriter;
|
private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class);
|
||||||
|
|
||||||
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
|
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
|
||||||
new File(outputDirectory).mkdirs();
|
String reportTemplate = initReportTemplate();
|
||||||
try {
|
|
||||||
reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDirectory, "my-report.html"))));
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
String resultRow = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
|
String resultRow = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
|
||||||
initReportTemplate();
|
StringBuilder rows = new StringBuilder();
|
||||||
for (ISuite suite : suites) {
|
suites.forEach(suite -> {
|
||||||
Map<String, ISuiteResult> suiteResults = suite.getResults();
|
Map<String, ISuiteResult> suiteResults = suite.getResults();
|
||||||
suiteResults.forEach((testName, suiteResult) -> {
|
suiteResults.forEach((testName, suiteResult) -> {
|
||||||
|
|
||||||
ITestContext testContext = suiteResult.getTestContext();
|
ITestContext testContext = suiteResult.getTestContext();
|
||||||
|
|
||||||
IResultMap failedResult = testContext.getFailedTests();
|
Stream<ITestResult> failedTests = testContext.getFailedTests().getAllResults().stream();
|
||||||
Set<ITestResult> testsFailed = failedResult.getAllResults();
|
Stream<ITestResult> passedTests = testContext.getPassedTests().getAllResults().stream();
|
||||||
for (ITestResult testResult : testsFailed) {
|
Stream<ITestResult> skippedTests = testContext.getSkippedTests().getAllResults().stream();
|
||||||
reportWriter.println(String.format(resultRow, "danger", suite.getName(), testName, testResult.getName(), "FAILED", "NA"));
|
|
||||||
|
String suiteName = suite.getName();
|
||||||
|
|
||||||
|
Stream<ITestResult> allTestResults = Stream.concat(Stream.concat(failedTests, passedTests), skippedTests);
|
||||||
|
generateReportRows(resultRow, rows, testName, suiteName, allTestResults);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
reportTemplate = reportTemplate.replaceFirst("</tbody>", rows.toString() + "</tbody>");
|
||||||
|
saveReportTemplate(outputDirectory, reportTemplate);
|
||||||
}
|
}
|
||||||
|
|
||||||
IResultMap passResult = testContext.getPassedTests();
|
private void generateReportRows(String resultRow, StringBuilder rows, String testName, String suiteName, Stream<ITestResult> allTestResults) {
|
||||||
Set<ITestResult> testsPassed = passResult.getAllResults();
|
allTestResults
|
||||||
for (ITestResult testResult : testsPassed) {
|
.forEach(testResult -> {
|
||||||
reportWriter.println(String.format(resultRow, "success", suite.getName(), testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis())));
|
String testReportRow = "";
|
||||||
|
if (testResult.getStatus() == ITestResult.FAILURE) {
|
||||||
|
testReportRow = String.format(resultRow, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA");
|
||||||
}
|
}
|
||||||
|
if (testResult.getStatus() == ITestResult.SUCCESS) {
|
||||||
|
testReportRow = String.format(resultRow, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis()));
|
||||||
|
|
||||||
IResultMap skippedResult = testContext.getSkippedTests();
|
|
||||||
Set<ITestResult> testsSkipped = skippedResult.getAllResults();
|
|
||||||
for (ITestResult testResult : testsSkipped) {
|
|
||||||
reportWriter.println(String.format(resultRow, "warning", suite.getName(), testName, testResult.getName(), "SKIPPED", "NA"));
|
|
||||||
}
|
}
|
||||||
|
if (testResult.getStatus() == ITestResult.SKIP) {
|
||||||
|
testReportRow = String.format(resultRow, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA");
|
||||||
|
}
|
||||||
|
rows.append(testReportRow);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
finishReportTemplate();
|
|
||||||
|
private String initReportTemplate() {
|
||||||
|
String template = null;
|
||||||
|
byte[] reportTemplate = null;
|
||||||
|
try {
|
||||||
|
reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html"));
|
||||||
|
template = new String(reportTemplate, "UTF-8");
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.error("Problem initializing template", e);
|
||||||
|
}
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveReportTemplate(String outputDirectory, String reportTemplate) {
|
||||||
|
new File(outputDirectory).mkdirs();
|
||||||
|
try {
|
||||||
|
PrintWriter reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDirectory, "my-report.html"))));
|
||||||
|
reportWriter.println(reportTemplate);
|
||||||
reportWriter.flush();
|
reportWriter.flush();
|
||||||
reportWriter.close();
|
reportWriter.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.error("Problem saving template", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initReportTemplate() {
|
|
||||||
reportWriter.println(
|
|
||||||
"<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\">"
|
|
||||||
+ "<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\">");
|
|
||||||
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>");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void finishReportTemplate() {
|
|
||||||
reportWriter.println(" </tbody></div></body></html>");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
33
testng/src/test/resources/reportTemplate.html
Normal file
33
testng/src/test/resources/reportTemplate.html
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<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">
|
||||||
|
<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">
|
||||||
|
<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>
|
||||||
|
<tr class="success">
|
||||||
|
<td>My test suite</td>
|
||||||
|
<td>numbersXML</td>
|
||||||
|
<td>givenNumberObjectFromDataProvider_ifEvenCheckOK_thenCorrect</td>
|
||||||
|
<td>PASSED</td>
|
||||||
|
<td>0</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user