Listeners in TestNG

Vivek Sharma
Jul 21, 2017 · 1 min read

If you want to add logs into your test script you can use listeners. Listeners work on action so have below methods.

OnTestStart : Invoked each time before a test will be invoked.

OnTestSuccess : Invoked each time a test succeeds.

OnTestFailure : Invoked each time a test fails

OnTestSkipped : Invoked each time a test is skipped.

OnTestFinish

How to use in your current project

Create Class ‘ListenersClass.java’

code like

package com.example.logging;

import org.testng.IClass;

import org.testng.ITestResult;

import org.testng.TestListenerAdapter;

public class ListenerClass extends TestListenerAdapter {

@Override

public void onTestStart(ITestResult tr) {

log(“Test Started….”);

}

@Override

public void onTestSuccess(ITestResult tr) {

log(“Test ‘“ + tr.getName() + “‘ PASSED”);

Use the above class in your exiting main test class by importing above class.

Add listners in to testng.xml as well

listeners>

<listener class-name=”Tru.XXX.logging.ListenerClass” />

</listeners>

Enjoy longing so that you can have better understanding where test fails.

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade