Custom Listeners in testng using TestListenerAdapter

If we want to add custom listeners to any testng project we define a class named CustomListener which extends TestListenerAdapter class. The class is used to define what should happen when a testng testcase passes,fails or skips etc.

This class can be customized as per our needs.

In this class we have to implement these 3 methods from the interface we extended.onTestFailure ,onTestSkipped ,onTestSuccess.

Then in

public Class CustomListener extends TestListenerAdapter
{
 public void onTestSuccess(iTestResult tr){
  System.out.println("Test passed " + tr.getName()
}

 public void onTestSkipped(iTestResult tr){
  System.out.println("Test skipped " + tr.getName()
}

 public void onTestFailure(iTestResult tr){
  System.out.println("Test failed" + tr.getName()
 }
}

And then in the testing.xml we let the xml file know that we are using the custom class that we made as our listener.Here in the example below Customreport is the name of the package in which we created our custom class named

<listeners>
<listener class-name="customreport.CustomListener" />
</listeners>