Best Testng code snippet using org.testng.Interface IReporter.generateReport
Source:ExtentReporterN.java
...15import com.relevantcodes.extentreports.ExtentTest;16import com.relevantcodes.extentreports.LogStatus;17public class ExtentReporterN implements IReporter{18 /**This interface can be implemented by clients to generate a report. Its method19 * generateReport() will be invoked after all the suite have run and the parameters20 * give all the test results that happened during that run.21 */22private ExtentReports extent;23 /**so that the result is private24 *The type ExtentReporterN must implement the inherited abstract method IReporter.generateReport(List<XmlSuite>, List<ISuite>, String)25 * generateReport is a built in method that 26 * creating a method, use List to contain array, create a xml file which will have all the classes,27 * that has methods, testcases, so it will28 * generate a report on those methods, that is passing/failing/skipping29 * It needs to be string30 */31public void generateReport(List<XmlSuite> xmlSuites, List<ISuite>suites, String outPutDirectory){32 33 /**outPutDirectory will have all the status, pass/fail/skip34 * creating an object for extent, creating a virtual obj to save the ouput35 * .separator = Generate a report for the given suites into the specified output directory.36 * it doesn't matter how I received the result,(linux/xml/language), the extent report file should be saved under "Extent.html" name, 37 * so it can be opened from anywhere(mobile/web)38 * true= sometime running the result, if there's no result, don't generate the report, 39 * true makes sure to generate report only when there's a result40 */41 42extent = new ExtentReports(outPutDirectory + File.separator + "Extent.html", true);43 /**ISuite contains all classes, it obtain a key value , it can't be duplicated, then it will map it to one44 * location, the location is (in our case) ExtentReort, key value is the status (pass.fail/skip)that is interface45 * go back to class, print fail to the class, if the method inside the class failed...
Source:ExtentReporterNG.java
...18 private ExtentReports extent;19 20 21 /*22 * generateReport - This is abstract method of IReporter interface23 * 24 */25 26 public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,27 String outputDirectory) {28 extent = new ExtentReports(outputDirectory + File.separator29 + "ExtentReport.html", true);30 for (ISuite suite : suites) {31 Map<String, ISuiteResult> result = suite.getResults();32 for (ISuiteResult r : result.values()) {33 ITestContext context = r.getTestContext();34 buildTestNodes(context.getPassedTests(), LogStatus.PASS);35 buildTestNodes(context.getFailedTests(), LogStatus.FAIL);36 buildTestNodes(context.getSkippedTests(), LogStatus.SKIP);37 }38 }39 extent.flush();40 extent.close();...
Source:IReporter.java
...3import org.testng.reporters.PojoReporterConfig;4import org.testng.xml.XmlSuite;5import java.util.List;6/**7 * This interface can be implemented by clients to generate a report. Its method generateReport()8 * will be invoked after all the suite have run and the parameters give all the test results that9 * happened during that run.10 */11public interface IReporter extends ITestNGListener {12 /**13 * Generate a report for the given suites into the specified output directory.14 *15 * @param xmlSuites The list of <code>XmlSuite</code>16 * @param suites The list of <code>ISuite</code>17 * @param outputDirectory The output directory18 */19 default void generateReport(20 List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {21 // not implemented22 }23 /**24 * Get the reporter configuration object.25 * <p>26 * <b>NOTE</b>: Reporter configuration objects must adhere to the JavaBean object conventions,27 * providing getter and setter methods that conform to standard naming rules. This enables28 * {@link org.testng.internal.ReporterConfig} to serialize, deserialize, and instantiate the reporter.29 *30 * @return reporter configuration object31 */32 default IReporterConfig getConfig() {33 return new PojoReporterConfig(this);...
Source:IReporterClass.java
...9/**The IReporter interface in the TestNG Listener provides us with a medium to generate custom reports */10public class IReporterClass implements IReporter {11 12 13 public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {14 15 for(ISuite suite:suites) {16 String suiteName = suite.getName();17 18 Map<String, ISuiteResult> suiteResults = suite.getResults();19 for (ISuiteResult sr : suiteResults.values()) {20 ITestContext tc = sr.getTestContext();21 System.out.println("Passed tests for suite '" + suiteName +22 "' is:" + tc.getPassedTests().getAllResults().size());23 System.out.println("Failed tests for suite '" + suiteName +24 "' is:" + tc.getFailedTests().getAllResults().size());25 System.out.println("Skipped tests for suite '" + suiteName +26 "' is:" + tc.getSkippedTests().getAllResults().size());27 }...
generateReport
Using AI Code Generation
1public class CustomReport implements IReporter {2 private static final String OUTPUT_FOLDER = "test-output/";3 private static final String FILE_NAME = "CustomReport.html";4 public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {5 List<ISuite> suiteList = suites;6 Map<String, ISuiteResult> suiteResultMap = suiteList.get(0).getResults();7 ISuiteResult suiteResult = suiteResultMap.get("Suite");8 ITestContext testContext = suiteResult.getTestContext();9 IResultMap failedTests = testContext.getFailedTests();10 IResultMap passedTests = testContext.getPassedTests();11 IResultMap skippedTests = testContext.getSkippedTests();12 IResultMap allTests = testContext.getPassedTests();13 allTests.getAllMethods().addAll(failedTests.getAllMethods());14 allTests.getAllMethods().addAll(skippedTests.getAllMethods());15 createCustomReport(failedTests, passedTests, skippedTests, allTests);16 }17 private void createCustomReport(IResultMap failedTests, IResultMap passedTests, IResultMap skippedTests, IResultMap allTests) {18 try {19 File file = new File(OUTPUT_FOLDER + FILE_NAME);20 if (!file.exists()) {21 file.createNewFile();22 }23 FileWriter fw = new FileWriter(file.getAbsoluteFile());24 BufferedWriter bw = new BufferedWriter(fw);25 bw.write("<html>");26 bw.write("<head>");27 bw.write("<style>");28 bw.write("table, th, td {");29 bw.write("border: 1px solid black;");30 bw.write("border-collapse: collapse;");31 bw.write("}");32 bw.write("th, td {");33 bw.write("padding: 5px;");34 bw.write("}");35 bw.write("</style>");36 bw.write("</head>");37 bw.write("<body>");38 bw.write("<h1>TestNG Custom Report</h1>");39 bw.write("<table style=\"width:100%\">");
generateReport
Using AI Code Generation
1public class TestNGReport implements IReporter {2 private static final Logger LOGGER = LogManager.getLogger(TestNGReport.class);3 private static final String REPORT = "report";4 private static final String REPORT_HTML = "report.html";5 private static final String REPORT_JSON = "report.json";6 public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {7 LOGGER.info("Generating report");8 TestNGReportModel testNGReportModel = new TestNGReportModel();9 for (ISuite suite : suites) {10 for (ISuiteResult suiteResult : suite.getResults().values()) {11 ITestContext testContext = suiteResult.getTestContext();12 testNGReportModel.addTestResults(testContext.getPassedTests().getAllResults(),13 testContext.getFailedTests().getAllResults(),14 testContext.getSkippedTests().getAllResults());15 }16 }17 TestNGReportGenerator testNGReportGenerator = new TestNGReportGenerator();18 testNGReportGenerator.generateReport(testNGReportModel);19 testNGReportGenerator.generateReport(testNGReportModel, outputDirectory + File.separator + REPORT_HTML);20 testNGReportGenerator.generateReport(testNGReportModel, outputDirectory + File.separator + REPORT_JSON);21 LOGGER.info("Report generated");22 }23}
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!