BAEL-786 Updated custom test reports

This commit is contained in:
dhruba619 2017-04-24 01:04:35 +05:30
parent c817aec2dc
commit cf24ede9ab
2 changed files with 70 additions and 45 deletions

View File

@ -7,31 +7,32 @@ import org.testng.ITestListener;
import org.testng.ITestResult;
public class CustomisedListener implements ITestListener {
private static final Logger LOGGER = LoggerFactory.getLogger("TEST_REPORT");
private static final Logger LOGGER = LoggerFactory.getLogger("CUSTOM_LOGS");
@Override
public void onFinish(ITestContext context) {
LOGGER.info("PASSED TEST CASES");
context.getPassedTests()
.getAllResults()
.stream()
.forEach(result -> {
LOGGER.info(result.getName());
});
.getAllResults()
.stream()
.forEach(result -> {
LOGGER.info(result.getName());
});
LOGGER.info("FAILED TEST CASES");
context.getFailedTests()
.getAllResults()
.stream()
.forEach(result -> {
LOGGER.info(result.getName());
});
LOGGER.info("Test completed on: " + context.getEndDate().toString());
.getAllResults()
.stream()
.forEach(result -> {
LOGGER.info(result.getName());
});
LOGGER.info("Test completed on: " + context.getEndDate()
.toString());
}
@Override
public void onStart(ITestContext arg0) {
LOGGER.info("Started testing on: " + arg0.getStartDate()
.toString());
.toString());
}
@Override
@ -54,13 +55,14 @@ public class CustomisedListener implements ITestListener {
@Override
public void onTestStart(ITestResult arg0) {
LOGGER.info("Testing: " + arg0.getName() + " " + arg0.getStartMillis());
LOGGER.info("Testing: " + arg0.getName());
}
@Override
public void onTestSuccess(ITestResult arg0) {
LOGGER.info("Tested: " + arg0.getName() + " " + arg0.getEndMillis());
long timeTaken = ((arg0.getEndMillis() - arg0.getStartMillis()));
LOGGER.info("Tested: " + arg0.getName() + " Time taken:" + timeTaken + " ms");
}

View File

@ -1,50 +1,73 @@
package com.baeldung.reports;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.IReporter;
import org.testng.IResultMap;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.xml.XmlSuite;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CustomisedReports implements IReporter {
private static final Logger LOGGER = LoggerFactory.getLogger("TEST_REPORT");
private PrintWriter reportWriter;
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
new File(outputDirectory).mkdirs();
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>";
initReportTemplate();
for (ISuite suite : suites) {
Map<String, ISuiteResult> suiteResults = suite.getResults();
for (String testName : suiteResults.keySet()) {
ISuiteResult suiteResult = suiteResults.get(testName);
ITestContext testContext = suiteResult.getTestContext();
suites.stream()
.forEach(suite -> {
String suiteName = suite.getName();
Map<String, ISuiteResult> suiteResults = suite.getResults();
suiteResults.values()
.stream()
.forEach(result -> {
ITestContext context
= ((ISuiteResult) result).getTestContext();
IResultMap failedResult = testContext.getFailedTests();
Set<ITestResult> testsFailed = failedResult.getAllResults();
for (ITestResult testResult : testsFailed) {
reportWriter.println(String.format(resultRow, "danger", suite.getName(), testName, testResult.getName(), "FAILED", "NA"));
}
LOGGER.info("Passed tests for suite '"
+ suiteName + "' is:"
+ context.getPassedTests()
.getAllResults()
.size());
LOGGER.info("Failed tests for suite '"
+ suiteName + "' is:"
+ context.getFailedTests()
.getAllResults()
.size());
LOGGER.info("Skipped tests for suite '"
+ suiteName + "' is:"
+ context.getSkippedTests()
.getAllResults()
.size());
});
});
IResultMap passResult = testContext.getPassedTests();
Set<ITestResult> testsPassed = passResult.getAllResults();
for (ITestResult testResult : testsPassed) {
reportWriter.println(String.format(resultRow, "success", suite.getName(), 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"));
}
}
}
finishReportTemplate();
reportWriter.flush();
reportWriter.close();
}
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>");
}
}