TestNG | Custom TestNG listeners using ITestListener

If you have been working with TestNG, you may be familiar with the default listeners it supplies out of the box BeforeTest, BeforeMethod, AfterTest, AfterMethod, and so on. These listeners play an important role in setting up and tearing down our tests as they execute. However, they are limited to the behavior they expose.

Maybe you would like to perform a specific operation when a test fails, or when a test passes, or maybe you would like to perform a step before the method invocation is ever performed. This is where the additional listeners exposed by TestNG's listener class come in.

There are many TestNG Listener interfaces you could implement. To view the list, see ITestNGListener.

  1. Create an empty class in your java project.

    Copy
    package utilities;

    public class testListenerDemo {
    }
  2. Implement a listener interface.

    Copy
    package utilities;

    import org.testng.ITestListener;

    public class testListenerDemo implements ITestListener {

    }
  3. In Eclipse, hover over the class name and select the option to add unimplemented methods

    Your final class will now look like the below. As you can see, you now have access to many other listeners you can utilize in your framework. This is perfect for collecting reporting data or performing database cleanup based on pass/fail conditions and the like.

    Copy
    package utilities;

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

    public class testListenerDemo implements ITestListener {
        @Override
        public void onTestStart(ITestResult result) {
            // TODO Auto-generated method stub        
        }

        @Override
        public void onTestSuccess(ITestResult result) {
            // TODO Auto-generated method stub        
        }

        @Override
        public void onTestFailure(ITestResult result) {
            // TODO Auto-generated method stub        
        }

        @Override
        public void onTestSkipped(ITestResult result) {
            // TODO Auto-generated method stub        
        }

        @Override
        public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
            // TODO Auto-generated method stub        
        }

        @Override
        public void onStart(ITestContext context) {
            // TODO Auto-generated method stub        
        }

        @Override
        public void onFinish(ITestContext context) {
            // TODO Auto-generated method stub        
        }
    }
  4. Hook this listener into your framework by adding the listener to your testng.xml file inside the listeners tags.

    Copy
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite" parallel="tests" thread-count="10" verbose="10" configfailurepolicy="skip">
        <listeners>
            <listener class-name="org.uncommons.reportng.HTMLReporter"/>
            <listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
            <listener class-name="utilities.testListenerDemo" />
        </listeners>   
        <test name="Test Chrome implicitNotVisible">
            <parameter name="targetEnvironment" value="Chrome" />
            <parameter name="network" value="" />
            <parameter name="networkLatency" value="" />
            <classes>
                <class name="AmazonTesting.SleepTestSystem">
                    <methods>
                        <include name="implicitNotVisible" />
                    </methods>
                </class>
            </classes>   
        </test>
    </suite>

Now you are ready to utilize your new listeners.