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.

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

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.

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

 

Print Friendly, PDF & Email
DataProvider in TestNG
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...