TestNG testing framework — ITestListener

Arvind Choudhary
3 min readJun 6, 2022
TestNG
TestNG

What are Listeners?

A person who “Listens”, In IT we change the person with process. We have many different applications which accept (listen) requests on different ports to serve client needs.

Like:

  1. MySQL server(Database) listens on 3306(by default).
  2. Oracle DB listener runs on 1521(by default).

In Listeners, we also have one such category for Event Listeners, listeners who perform actions based on some events(i.e. Keypress, mouse movements, etc).

Now the main question still stays what does it have to do with Testing, what help could listeners bring to a testing framework?

WHAT IF, you can deploy some custom code at a different phase of the automated execution cycle without using any of Annotated methods, i.e. @Before & @After annotations because these methods have a specific order, and stuffing too much can make life harder for us as well as increase code execution time.

We can leverage TestNG Listeners, now that we know what listeners are and that TestNG has few listeners for us.

For this article, I’ll only pick the ITestListener interface

Listed down are methods from the ITestListener interface.

ITestListener interfaces methods

Examples:

ListenerClassExample.java

ListenerClassExample.java

Runner.java

Runner.java

How make TestNG aware of our configuration?

Now that we made our changes(we implemented ITestListener), so now the question comes up where can we declare it so that testng can invoke its methods based on events.

In TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="suite">
<listeners> <!-- Listeners -->
<listener class-name="ListenerClassExample"/>
</listeners>
<test name="test"> <!-- Test -->
<classes>
<class name="Runner" />
</classes>
</test>
</suite> <!-- Suite -->

We declare our listener class name within listeners tags, we can have other TestNG listeners included there.

In Runner.java — TestNG runner class

@Listeners(ListenerClassExample.class)

with @Listener Annotation on any class we can declare that this class should be invoked with the mentioned listener. (refer to Runner.java code above)

Output

Runner.java(Without Listener)

testMethod
testMethod_fail
java.lang.AssertionError:
Expected :true
Actual :false
<Click to see difference>
at org.testng.Assert.fail(Assert.java:110)
at org.testng.Assert.failNotEquals(Assert.java:1413)
at org.testng.Assert.assertTrue(Assert.java:56)
at org.testng.Assert.assertTrue(Assert.java:66)
at Runner.testMethod_fail(Runner.java:18)
at ......
testMethod_skipTest ignored.===============================================
Default Suite
Total tests run: 3, Passes: 1, Failures: 1, Skips: 1
===============================================

Runner.java(With Listener)

:::onStart called
:::onTestStart called
testMethod
:::onTestSuccess called
:::onTestStart called
testMethod_fail
java.lang.AssertionError:
Expected :true
Actual :false
<Click to see difference>
at org.testng.Assert.fail(Assert.java:110)
at org.testng.Assert.failNotEquals(Assert.java:1413)
at org.testng.Assert.assertTrue(Assert.java:56)
at org.testng.Assert.assertTrue(Assert.java:66)
at Runner.testMethod_fail(Runner.java:18)
at ......
:::onTestFailure called
:::onTestStart called
testMethod_skip
Test ignored.
:::onTestSkipped called
:::onFinish called
===============================================
Default Suite
Total tests run: 3, Passes: 1, Failures: 1, Skips: 1
===============================================

Based on the above output you can see, that those different methods are invoked based on events like onStart, onTestStart, onTestPass, onTestFail, and so on from the Listener class.

Why is Listener important?

To get Ahead on the path of Automation Testing, you would have wished or thought to create a Test Automation Framework, of your OWN.

The framework where you could write your own implementation, to make Testing easier(solve problems which you came across) & add some custom reporting (Extent/Allure).

With the Listener class, you can always incorporate your custom code which is not for testing but for better reporting or initialization(like reading properties or XML or JSON).

Few GitHub repositories use ITestListener for better reporting.

  1. https://github.com/Arvind142/TestNG-Modular-Automation-Framework.git
  2. https://github.com/rajatt95/MasterSeleniumFramework.git

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response