top of page

TestNG Tutorial

Listeners In TestNG

What Are Listeners In TestNG ?

TestNG listeners are interfaces that modifies the default testNG's behaviour. These are called when an event occurs in tests. ​
In testNG, below are the major types of listeners : ​
  1. ITestListener

  2. IReporter

  3. IAnnotaionTransformer

  4. IAnnotaionTransformer2

  5. IConfigurableListener

  6. IConfigurationListener

  7. IExecutionListener

  8. IHookable

  9. IInvokedMethodListener

  10. IInvokedMethodListener2

  11. IMethodInterceptor

  12. ISuiteListener


ITestListener is the most adopted listener above all. So, here we'll move forward with ITestListener. ​
Below are the methods of interface ITestListener :

public void onStart(ITestContext context) ​ public void onFinish(ITestContext context) ​ public void onTestStart(ITestResult result) public void onTestSuccess(ITestResult result) ​ public void onTestFailure(ITestResult result) ​ public void onTestSkipped(ITestResult result) ​ public void onTestFailedButWithinSuccessPercentage(ITestResult result)

Listeners can be implemented at two levels :
  • At Class Level

  • At Suite Level

Implementeing listeners at class level To apply listeners on test classes, first create a listener class and implement the mentioned listener interface then override its methods. Now use '@Listeners' annotation before the test class and pass the .class name of the created listener class.
Syntax :

@Listeners(classname.class)

Example :

package testNGexamples; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; public class ListenerClass implements ITestListener { ​ @Override public void onStart(ITestContext context) { System.out.println("Test Class Instantiated :"); } ​ @Override public void onFinish(ITestContext context) { System.out.println("All Tests Finished : :"); } ​ @Override public void onTestStart(ITestResult result) { System.out.println(result.getName() + "Test Method is Called :"); } ​ @Override public void onTestSuccess(ITestResult result) { System.out.println(result.getName() + "Test is Success :"); } ​ @Override public void onTestFailure(ITestResult result) { System.out.println(result.getName() + "Test Failed :"); } ​ @Override public void onTestSkipped(ITestResult result) { System.out.println(result.getName() + "Test skipped :"); } ​ @Override public void onTestFailedButWithinSuccessPercentage(ITestResult result) { System.out.println(result.getName() + "Test Failed But Within Succes Percentage :"); } }


Adding above class in test class using @Listeners annotation as :

package testNGexamples; import org.testng.Assert; import org.testng.SkipException; import org.testng.annotations.Listeners; import org.testng.annotations.Test; @Listeners(ListenerClass.class) public class TestNGWithListeners { int num = 1, i = 1; ​ @Test public void simpleFunction() { System.out.println("simpleFunction is Passed :"); } ​ @Test(successPercentage = 60, invocationCount = 2) public void repeatedTest() { Assert.assertEquals(num, i); num++; } ​ @Test public void testAssertCheck() { Assert.assertEquals(num, i); num++; } ​ @Test public void testSkip() { throw new SkipException("testSkip must be Skipped :"); } }


Output :

[RemoteTestNG] detected TestNG version 7.4.0 Test Class Instantiated : repeatedTestTest Method is Called : repeatedTestTest is Success : repeatedTestTest Method is Called : repeatedTestTest Failed But Within Succes Percentage : simpleFunctionTest Method is Called : simpleFunction is Passed : simpleFunctionTest is Success : testAssertCheckTest Method is Called : testAssertCheckTest Failed : testSkipTest Method is Called : testSkipTest skipped : All Tests Finished : : PASSED: simpleFunction PASSED: repeatedTest FAILED: testAssertCheck =============================================== Default test Tests run: 4, Failures: 1, Skips: 1 =============================================== =============================================== Default suite Total tests run: 5, Passes: 2, Failures: 2, Skips: 1 ===============================================


Implementeing listeners at suite level To apply listeners at suite level, just remove @Listeners annotation from the class and add <listener> in .xml as : ​

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Test-Suite"> <listeners> <listener class-name="testNGexamples.ListenerClass"></listener> </listeners> <test name="TestNG Listeners Execution"> <classes> <class name="testNGexamples.TestNGWithListeners"></class> </classes> </test> </suite>

Here 'ListenerClass' is the class in which interface methods have been overrided and 'TestNGWithListeners' is the class on which listener is applied. ​ ​ ​ ​
bottom of page