Test Listener in TestNG

We might need to do certain things just before and after each test in XML suite is invoked. Sometimes we also need to log results in custom reports when test methods are invoked, passed, failed or skipped.

With the help of Test Listeners, we can write custom code that gets executed when above mentioned events happen.

TestNG framework provides one listener interface called as ITestListener that can be implemented and passed in TestNG.XML file.

Below class implements ITestListener inetrface. We have implemented methods like onTestStart, onTestSuccess, onTestFailure, onTestSkipped, onStart, onFinish etc.

package org.softpost;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class TestMethodListener implements ITestListener {


    public void onTestStart(ITestResult iTestResult) {
        System.out.println("\nThis will be called right before starting test method execution");
    }

    public void onTestSuccess(ITestResult iTestResult) {
        System.out.println("This will be called if test method was successful");

    }

    public void onTestFailure(ITestResult iTestResult) {
        System.out.println("This will be called if test method fails");

    }

    public void onTestSkipped(ITestResult iTestResult) {
        System.out.println("This will be called if test method was skipped");

    }

    public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
        System.out.println("This will be called if test method success rate was acceptable");

    }

    public void onStart(ITestContext iTestContext) {
        System.out.println("This will be called right before starting test tag execution");

    }

    public void onFinish(ITestContext iTestContext) {
        System.out.println("This will be called right after finishing test tag execution");
        System.out.println("***********************************");
    }
}

We can also use the ITestResult and ITestContext interface methods to get more information on which tests we are executing as shown in below image.

ITestResult methods in TestNG

ITestResult methods in TestNG

ITestContext in TestNG

ITestContext in TestNG

Here is the sample TestNG XML file. Note that we have passed name of the listener class in below XML file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite">
    <listeners>
        <listener class-name="org.softpost.TestMethodListener"/>
    </listeners>

    <test name="Tests1">
        <classes>
            <class name="org.softpost.Class1" />
        </classes>
    </test>
    <test name="Tests2">
        <classes>
            <class name="org.softpost.Class2" />
        </classes>
    </test>
</suite>

Here is the output of above code.

[TestNG] Running:
C:\Users\Sagar\IdeaProjects\TestNG-Project\src\test\resources\testng.xml
This will be called right before starting test tag execution

This will be called right before starting test method execution
Test1 from Class1
This will be called if test method was successful

This will be called right before starting test method execution
Test2 from Class1
This will be called if test method was successful
This will be called right after finishing test tag execution
***********************************
This will be called right before starting test tag execution
This will be called right before starting test method execution
Test1 from Class2
This will be called if test method was successful

This will be called right before starting test method execution
Test2 from Class2
This will be called if test method was successful
This will be called right after finishing test tag execution
***********************************
===============================================
Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================

You may also like...