Listeners in TestNG

In the previous post, we have learnt about dataProvider in TestNG. In this post, we will learn about Listeners in TestNG which is a very important concept in TestNG.

What is Listeners?

As it name says, We can make TestNG to listen (or say act accordingly) when a test starts, fails , passes, skips etc. Listeners can be useful for reporting and modify TestNG behavior during run time. There are several interfaces that allow you to modify TestNG’s behavior. These interfaces are broadly called “TestNG Listeners”.

  • IAnnotationTransformer
  • IAnnotationTransformer2
  • IHookable
  • IInvokedMethodListener
  • IMethodInterceptor
  • IReporter
  • ISuiteListener
  • ITestListener

We need to implement any of these required interface to use its methods like onStart(),onFinish(), onTestFailure (),onTestSuccess etc. If our script fails, passes, skips any test then these methods will be automatically invoked. A figure depicting the same is shown below ,though it has only few methods mentioned.

Listenerrs Testng


Let’s talk about some of the interfaces and their methods.

ISuiteListener:

Method Description
onStart(ISuite arg0) Invoked before TestNG suite starts executing.
onFinish(ISuite arg0) Invoked after TestNG suite finishes execution.

ITestListener:

Method Description
onStart(ITestContext arg0) Invoked after the test class is instantiated and before any configuration method is called.
onFinish(ITestContext arg0) Invoked after all the tests have run and all their Configuration methods have been called.
onTestFailure(ITestResult result) Invoked each time when a test fails.
onTestSkipped(ITestResult arg0) Invoked each time when a test skips.
onTestStart(ITestResult arg0) Invoked each time before a test will be invoked.
onTestSuccess(ITestResult arg0) Invoked each time when a test succeeds.
onTestFailedButWithinSuccessPercentage(ITestResult arg0) IInvoked each time a method fails but has been annotated with successPercentage and this failure still keeps it within the success percentage requested.

IInvokedMethodListener:

Method Description
afterInvocattion() Invoke after each method.
beforeInvocation() Invoke before each method.

Let’s understanding the working of Listener step by step.

Step#1 Create a customListeners class which is implementing intefaces “ITestListener“,”ISuiteListener” and their methods that will be invoked at test pass, test failure, test skipped etc.

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

public class CustomListeners implements ITestListener,ISuiteListener{

	// This belongs to ISuiteListener and will execute, once the Suite is finished
	public void onFinish(ISuite arg0) {
		System.out.println("Ending Test Suite "+arg0.getName());		
	}

	// This belongs to ISuiteListener and will execute before the Suite start
	public void onStart(ISuite arg0) {
		System.out.println("Starting Test Suite "+arg0.getName());		
	}

	// This belongs to ITestListener and will execute after starting of Test set/batch
	public void onFinish(ITestContext arg0) {
		System.out.println("Finish Test execution "+arg0.getName());	
		
	}

	// This belongs to ITestListener and will execute before starting of Test set/batch
	public void onStart(ITestContext arg0) {
		System.out.println("Starts Test execution "+arg0.getName());		
	}

	//Not so important..ignore this as of now
	public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
		// TODO Auto-generated method stub		
	}


	// This belongs to ITestListener and will execute when a test is skipped
	public void onTestSkipped(ITestResult arg0) {
		System.out.println("Skipped test "+arg0.getName());	
		
	}

	// This belongs to ITestListener and will execute before the main test start i.e. @Test
	public void onTestStart(ITestResult arg0) {
		System.out.println("Starts Main Test execution "+arg0.getName());
		
	}

	// This belongs to ITestListener and will execute when a test is passed
	public void onTestSuccess(ITestResult arg0) {
		System.out.println("Passed test "+arg0.getName());
		
	}

	// This belongs to ITestListener and will execute when a test is failed
	public void onTestFailure(ITestResult arg0) {
		System.out.println("Failed test "+arg0.getName());
		
	}

}

Step#2 Create a ListnerTest class which will execute few methods of returning results like pass, fail , skip etc.

import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ListenerTest {
	
	//Should be passed
	@Test
	public void Test1(){
		System.out.println("This is Test1");
	}
	
	//Should be Failed
	@Test
	public void Test2(){
		System.out.println("This is Test2");
		Assert.assertEquals("A", "B");
	}
	
	//Should be Skipped
	@Test
	public void Test3(){
		System.out.println("This is Test3");
		throw new SkipException("This is a skip exception");
	}
	
	
	@BeforeMethod
	public void Test4(){
		System.out.println("This is Test4");
	}
	

	@AfterMethod
	public void Test5(){
		System.out.println("This is Test5");
	}

}

How the Listeners will be invoked?

For making Listeners effectively , we can insert a listeners tag in testng.xml. Look at the below code which is having listeners tag with class-name defined into this.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="ExampleTestSuite" >
<listeners>
<listener class-name="com.testing.CustomListeners" />
</listeners>
<test name="ListenerExample">
    <classes>
      <class name="com.testing.ListenerTest"></class>
    </classes>
  </test>
</suite>

Output: After running the test, you will see the below output.

Listeners result


Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...