TestNG Listeners Annotation

Let’s learn about TestNG Listeners. What are these listeners? What does TestNG stand to offer to us with them? This question is something that someone seemed to have.


Listeners are used to generating logs. Listeners implement the interface “org.testng.ITestListener“. There are methods which give you a real time information.

Listeners provide us the flexibility to change and modify the TestNG default behavior.


Let’s discuss few important listeners in TestNG.


TestNG Listeners

1. ITestListener – ITestListener has many methods which help the user to generate the real time logs. This listener makes the call before and after the Test method only. Let’s discuss the important methods of the ITestLisTener –

  • onStart() – This Invoked after the Test Class is instantiated but before any test method is called.
  • onFinish() – This invoked after all methods done and before the class finishes execution.
  • onTestStart() – Invoked each time before a test will be invoked.
  • onTestFailure() – Invoked each time a test fails.
  • onTestSkipped() – Invoked each time a test is skipped.
  • onTestSuccess() – Invoked each time a test succeeds.


2. IInvokedMethodListener – It has only two methods and it makes the call before and after each and every method. Let’s discuss the included methods as well –

  • beforeInvocation() – Invoke before each method.

  • afterInvocattion() – Invoke after each Method.


Let’s understand the uses of all these listeners with an easy logging example. First, make your Test class-


In above class, we have created 4 test methods. In which thridTest(), We are failing explicitly and also created the dependency of forthTest() on the third test. SoforthTest() will always skip.

Now let’s create the Listener class to generate the logs on the basis of above test execution.


Now we need to modify our “testng.xml” file. We need to add our listener in our testng.xml file. Let’s see the structure of testng.xml after modification.

Listeners in TestNG

You can see the output. All methods executed and real-time logs of the whole test class were generated. So this the use of listeners in TestNG.


As we said Listeners are also used to modify the TestNG default behavior. We will look into this in details in Selenium tutorial. I can create and explain the example here but it will not worth.


So that’s all about TestNG, Feel free to contact us in the case of any doubt and confusion.

TestNG Tutorials:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply