Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 16, 2016

Custom Reporter in TestNG





Custom reporter in testng will discuss about writing our own reporter in TestNG. Reporting is one of the most important things to remember in automation testing. Reports are used to understand the result of the test execution, point of failure, and reasons for the failure. TestNG by default generates a different type of report (i.e. HTML and an XML reports). TestNG also allows us to write their own reporter and use it with TestNG.

To generate custom reports in testng we need to use Reporters. To implement reporters, need to implement the IReporter interface in your class. These classes are called when the whole suite run ends. The object containing the information of the whole test run is passed to this class when called.
For this we need to have at least 2 classes. One is for actual test cases and other is for to implement the IReporter.
Below is the sample program for test cases class:

import org.testng.Assert;
import org.testng.annotations.Test;

public class loggerTest {
    @Test
    public void testOne() {
        Assert.assertTrue(true);
    }

    @Test
    public void testTwo() {
        Assert.assertTrue(false);
    }

    @Test(dependsOnMethods = { "testTwo" })
    public void testThree() {
        Assert.assertTrue(true);
    }
}

When you execute the above program, the status of the test cases should be listen by the listener class. We have three methods called testOne, testTwo and testThree in the above program one is to pass and second is to fail and third is to skip status. After execution of the suits, the listener will track the status of the each test.
Below is the sample listener class which will implements the IReporter

import java.util.List;
import java.util.Map;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.xml.XmlSuite;

public class CustomReporter implements IReporter {
    @Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
        // Iterating over each suite included in the test
        for (ISuite suite : suites) {
            // Following code gets the suite name
            String suiteName = suite.getName();
            // Getting the results for the said suite
            Map<String, ISuiteResult> suiteResults = suite.getResults();
            for (ISuiteResult sr : suiteResults.values()) {
                ITestContext tc = sr.getTestContext();
                System.out.println(
                        "Passed tests for suite '" + suiteName + "' is:" + tc.getPassedTests().getAllResults().size());
                System.out.println(
                        "Failed tests for suite '" + suiteName + "' is:" + tc.getFailedTests().getAllResults().size());
                System.out.println("Skipped tests for suite '" + suiteName + "' is:"
                        + tc.getSkippedTests().getAllResults().size());
            }
        }
    }
}

The above class implements the IReporter. So, it has to implement all the methods which are available in that interface. And it is not mandatory to use all the methods, we can left blank some of the methods which are not relevant to the requirement.
Now we have to create a testng.xml file to execute the above tests and it should have a mandatory tag called listener. It will call the listener class to listen the status of the each and every test which will execute.
Below is the sample program to execute the above program:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="CustomReporter"></listener>
</listeners>
  <test name="Test">
    <classes>
      <class name="loggerTest"/>
    </classes>
  </test>
</suite>

Output for the above program is:

1

This way we can use custom listeners to generate the reports after execution.
Please watch the you tube video for better understanding.



Share this post: on Twitter on Facebook

Custom Logger in TestNG Execute Multiple XML files in TestNG

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

PARALLEL EXECUTION OF classes

TestNG

Parallel Execution of Classes In TestNG

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025