How to Use TestNG Listeners

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]


Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 5 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across the wide range of browsers and platforms. In this tutorial, you will learn how to do web test automation with Selenium Webdriver and the related tools....

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...

Getting Started with RobotFramework on Windows

By Onur Baskirt / Apr 25, 2016 / 12 Comments
What is RobotFramework? RobotFramework is a GENERIC test automation framework for acceptance testing and acceptance test-driven development (ATTD). What it means that you can do web, mobile, desktop and other test automation activities with related test libraries. These libraries can...
By | 2017-01-13T21:33:41+00:00 December 15th, 2016|Selenium, Test Automation, Test Frameworks, TestNG|0 Comments

About the Author:

Onur Baskirt
Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as research assistant, design verification engineer and software test leader. Also, he worked as software test leader and software operations manager at Turkey's biggest technology retailer, Teknosa. Now, he is working as Head of Software Testing and Manager of two Development Teams at His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.

Leave A Comment