In this article I will explain and show an example of TestNG Listeners. Actually, TestNG provides many types of listeners but we generally use some of them. Thats why I did not give all type of TetsNG Listeners but you can find all of them in this article.

A TestNG listener always extends org.testng.ITestNGListener and TestNG provides us below listener types:

  • IExecutionListener
  • IAnnotationTransformer
  • ISuiteListener
  • ITestListener
  • IConfigurationListener
  • IMethodInterceptor
  • IInvokedMethodListener
  • IHookable
  • IReporter

In this article I will show the most common Listener type  ITestListener.

In our tests we implement ITestListener and use its methods. It has following methods and events:

  • onStart is invoked after the test class is instantiated and before any configuration method is called.
  • onTestSuccess is invoked on success of a test.
  • onTestFailure is invoked on failure of a test.
  • onTestSkipped is invoked whenever a test is skipped.
  • onTestFailedButWithinSuccessPercentage is invoked each time a method fails but is within the success percentage requested. Don’t worry I will explain it with example ūüėČ
  • onFinish is invoked after all the tests have run and all their Configuration methods have been called.

Now, Lets do an example and examine how our Listener works on specific events.

We have three JAVA classes; BaseTest, Listener and SampleTest.


In BaseTest, I used @BeforeMethod and @AfterMethod annotations. @BeforeMethod is triggered before the test and @AfterMethod is triggered after the test finishes.


SampleTest is our test class,

test1 passes without any problem.

test2 always fails because it expects RuntimeExceptions but in the test method there is not any runtime exception.

test3 always skipped because it throws SkipException.

test4 is a little bit different than others.

I set %60 success percentage and 5 invocation count.  Thus, it runs 5 times and if 3 times it passes, test method is considered as passed! In the code, test method fails 2 times and passes 3 times so success ratio is (3/5)*100=60 thus we can consider this test method as passed! As you can see below picture, all the 5 tests of test4() method is shown as passed! Because we are in success ratio %60.

But, when we increase the success ratio to %80, this time one of the tests will be shown as failed! Because, %80 success ratio means “4 times tests pass” but our tests pass 3 times. Thus one of them is shown as failed.

Now, let’s set the success ratio to %100 (it is the default ratio). If all test pass, we can reach %100 success ratio but in our test method, 3 of our test pass and two of them fail. Thus, we can not reach %100 success ratio and we will see two failed tests as a result.


In listener class, I implemented ITestListener class and I explained its method.



Test Output:

Test Code:

References: – listeners/[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]