what are testng listeners

0 votes
I am using selenium with TestNG. I would like to know what are listeners in TestNG

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

answered by (171 points)
selected by
0 votes
Best answer

We generally use TestNG listeners to generate logs. We can either extend 'TestListenerAdapter' or implement Interface 'ITestListener' which is a listener for test running.

What are Listeners in TestNG : -

TestNG listeners are set of class exposed into TestNG framework which we can utilize to modify default TestNG's behavious.

E.g. We use 'ITestListener' or 'TestListenerAdapter' in order to write our own implementation when a Test fails or skips etc.

Below is list of methods

OnTestStart : This method can be  Invoked each time before a test will be invoked.
OnTestSuccess: Invoked each time a test succeeds.
OnTestFailure: Invoked each time a test fails E.g. we can utilize this method to take screenshot everytime test fails.
OnTestSkipped: Invoked each time a test is skipped.
OnTestFinish: Invoked after all the tests have run and all their Configuration methods have been called.

The below is the example program that demonstrates the Logging Listeners.

Step 1: Here is simple TestNG test class, LoggingClass.java, which has @Test methods.

package com.example.logging;

import org.testng.annotations.Test;

public class LoggingClass {

    @Test(priority = 0)
    public void methodAddingNumbers() {
        System.out.println("Helloo.. Im in method adding numbers");

    @Test(priority = 1)
    public void dividedByZero() {
        System.out.println("Helloo.. Im in method divided by zero");
        int e = 1 / 0;

    @Test(dependsOnMethods = { "dividedByZero" })
    public void methodSkip() {
        System.out.println("Helloo.. Im in method skip");


In this example, method  "methodAddingNumbers()" will pass but "dividedByZero()" will fail. We can use '@Test(expectedExceptions=ArithmeticException.class)' to catch the exception and then that method will result as 'PASSED'. Method "methodSkip()" will be skipped as method it depends on, dividedByZero, is not passed.

Step 2: Here is 'ListenersClass.java'. We are going to implement TestNG listeners here.

package com.example.logging;

import org.testng.IClass;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class ListenerClass extends TestListenerAdapter {

    public void onTestStart(ITestResult tr) {
        log("Test Started...");

    public void onTestSuccess(ITestResult TestResult) {

        log("Test '" + TestResult.getName() + "' PASSED");

        // This will print the class name in which the method is present

        // This will print the priority of the method.
        // If the priority is not defined it will print the default priority as
        // 'o'
        log("Priority of this method is " + TestResult.getMethod().getPriority());

        System.out.println("... ...");

    public void onTestFailure(ITestResult TestResult) {

        log("Test '" + TestResult.getName() + "' FAILED");
        log("Priority of this method is " + TestResult.getMethod().getPriority());
        System.out.println("... ...");

    public void onTestSkipped(ITestResult TestResult) {
        log("Test '" + TestResult.getName() + "' SKIPPED");
        System.out.println("... ...");

    private void log(String methodName) {

    private void log(IClass testClass) {

To get list of more methods present inside "TestListenerAdapter" you canfollow this link

Step 3: Here is testng.xml file.

<suite name="Log Suite Example" verbose="1">
        <listener class-name="com.example.logging.ListenerClass" />

    <test name="TestNG logs sample" preserve-order="true">
            <class name="com.example.logging.LoggingClass">
                    <include name="methodAddingNumbers" />
                    <include name="dividedByZero" />
                    <include name="methodSkip" />


Ouput :

[TestNG] Running:

Test Started....
Helloo.. Im in method adding numbers
Test 'methodAddingNumbers' PASSED
[TestClass name=class com.example.logging.LoggingClass]
Priority of this method is 0
... ...
Test Started....
Helloo.. Im in method divided by zero
Test 'dividedByZero' FAILED
Priority of this method is 1
... ...
Test 'methodSkip' SKIPPED
... ...

Log Suite Example
Total tests run: 3, Failures: 1, Skips: 1