Table of contents
1.
What is TestNG Listeners?
2.
ITestListener interface
3.
How to create the TestNG Listeners?
4.
What is ITestResult?
5.
Frequently Asked Questions
5.1.
How does a TestNG listener work?
5.2.
What does ITestListener do?
5.3.
Can we have multiple listeners in TestNG XML?
6.
Conclusion
Last Updated: Jul 17, 2024
Easy

TestNG Listeners- ITestListner and ITestResult

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Whenever we write a piece of code or some tests for events on our webpage, we expect them to work according to the events triggered by the user. So we need some code or statement that observes and listens to all the events and executes them. But do we have such statements to handle the events for us? Yes, we have listeners who listen to all the events and execute them. Let’s discuss TestNG Listeners and how to use them in your tests in this article.

TestNG Listeners- ITestListner and ITestResult

What is TestNG Listeners?

TestNG Listeners are the annotations provided by TestNG to listen to every event on the application and execute them if the event matches with the event we want to execute or want the listener to listen. It executes the code for events and modifies the default behavior of the TestNG events. The listeners are activated either before the test case or after it. Whenever a test case fails in Selenium, the listeners redirect us to the new behaviour written for the tests. The listeners are implemented using the ITestListener interface. Let’s learn about the ITestListener interface now.

ITestListener interface

ITestListener is the most used TestNG Listener with Selenium webDriver. We implement the ITestListener because it is an interface, and the class in which we implement this interface will override all the methods in it. The ITestListener has the following methods in it.

  • onStart(): this method is invoked after any test class is instantiated and before executing any testNG method.
  • onTestSuccess(): this method is invoked on the successful execution of any test.
  • onTestFailure(): this method is invoked when a test fails to execute.
  • onTestSkipped(): this method is invoked when we skip a test.
  • onTestFailedButWithinSuccessPercentage(): this method is invoked whenever a method fails to execute but within the defined success percentage.
  • onFinish(): this method is invoked on the execution of all the test cases of a class.
import org.testng.Assert;  
import org.testng.annotations.Listeners;  
import org.testng.annotations.Test; 
 
@Listeners(com.javatpoint.Listener.class)  
public class TestNGClass {
  
  @Test  
  public void addition() {  
    int sum = 0;  
    int num1 = 3;  
    int num2 = 3;  
    sum = num1 + num2;  
    System.out.println(sum);  
  }
  
  @Test  
    public void fail() {  
    System.out.println("Test case has been failed");  
    Assert.assertTrue(false);  
  }  
}

We implemented two methods; addition() and fail() in the above class “TestNGClass”. The method addition() has three variables sum, num1, and num2 initialized with 0, 3, and 3, respectively. Now let’s create a code for TestNG Listener.

import org.testng.ITestListener;  
import org.testng.ITestResult;  
public class TestNGListener implements ITestListener {
  
@Override  
public void onTestStart(ITestResult result) {  
// TODO Auto-generated method stub  
}  
  
@Override  
public void onTestSuccess(ITestResult result) {  
// TODO Auto-generated method stub  
System.out.println("Success of test cases and its details are : "+result.getName());  
}  
  
@Override  
public void onTestFailure(ITestResult result) {  
// TODO Auto-generated method stub  
System.out.println("Failure of test cases and its details are : "+result.getName());  
}  
  
@Override  
public void onTestSkipped(ITestResult result) {  
// TODO Auto-generated method stub  
System.out.println("Skip of test cases and its details are : "+result.getName());  
}  
  
@Override  
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {  
// TODO Auto-generated method stub  
System.out.println("Failure of test cases and its details are : "+result.getName());  
}  
  
@Override  
public void onStart(ITestContext context) {  
// TODO Auto-generated method stub  
}  
  
@Override  
public void onFinish(ITestContext context) {  
// TODO Auto-generated method stub  
}  
}  

We created a TestNG Listener in the above code. The test cases in the code created above will be executed, and the methods of ITestListener will get executed accordingly when we run this TestNG Listener with our XML code.

How to create the TestNG Listeners?

Step 1. Implement the ITestListener Interface: Create a class that implements the org.testng.ITestListener interface. This interface provides methods for various test events like onTestStart, onTestSuccess, etc. You can override the methods you want to implement custom behavior for.

Step 2. Annotate the Listener Class (Optional): You can optionally add the @Listeners annotation to your test class to associate the listener with that specific class.

Step 3. Define the Listener in testng.xml (Alternative): Alternatively, you can define the listener in your testng.xml suite configuration file. Add a <listeners> tag with the fully qualified class name of your listener class.

Here's a breakdown of both approaches:

Using @Listeners annotation:

@Listeners(MyTestListener.class)  // Associate the listener with this class
public class MyTestClass {
    // ... your test methods
}

Defining listener in testng.xml:

<suite name="MySuite">
  <listeners>
    <listener class-name="com.example.MyTestListener" />  // Full class name
  </listeners>
  </suite>

What is ITestResult?

ITestResult is used along with all the listeners. It describes the result of a test along with the instance Result. We use ITestResult as an argument to execute the test methods and determine whether the test case is executed successfully or failed. If the tests are failed, they are passed to the onTestFailure() method, and the output shows that the test cases are failed.

Xpath in Selenium

Frequently Asked Questions

How does a TestNG listener work?

The TestNG Listener executes the code for events and modifies the default behaviour of the TestNG methods. The listeners are activated either before the test case or after it. Whenever a test case fails in Selenium, the listeners redirect us to the new behaviour written for the tests.   

What does ITestListener do?

We implement the ITestListener to our testNG class, and the class in which we implement this interface will override all the methods in it. It is the most used TestNG Listener and has various methods listed earlier in this article.

Can we have multiple listeners in TestNG XML?

We cannot have multiple listeners a test in a test file with multiple classes. It can give us errors. But we can execute multiple TestNG Listeners by creating them and adding them to the <listeners> tag in XML, which implements the listeners throughout the test suite.

Conclusion

We have discussed TestNG Listeners and ITestListener in this article. Now you can perform tests using the listeners to modify the behavior of the tests as discussed in this article.

Hey Ninjas! We hope this blog helped you understand the concept of the TestNG Listeners better. If you want to learn more, check out Coding Ninjas for more unique courses and guided paths. Also, try Code360 for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Live masterclass