TestNG Listeners (2024) | TechGeekNext

TestNG Listeners (2024)

In this tutorial, we will learn about the TestNG Listeners and it's implementation.

Q: What are the TestNG Listeners?
Ans:

Listeners are TestNG annotations which listen to the events in a script and change TestNG behavior as a result. In the code, these listeners act as interfaces.

TestNG Listeners is one of the main features of the TestNG framework. It works as an interface that listens to events in the test scripts which are already defined and modifies the TestNG tool default behavior. TestNG listeners help to customize reports or create logs for particular tests. TestNG helps with the clean-up process after the testing is finished.

The TestNG Listeners are denoted as @listeners annotation. TestNG listeners are defined either before or after the test case.

Types TestNG Listeners

To modify TestNG behavior, listeners are implemented in code using interfaces. The most popular TestNG listeners are given below:

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

ITestListener

This has been the most used TestNG listener. ITestListener is an interface that is implemented in the class, which overrides the methods defined by ITestListener. The ITestListener is a class that listens for events and then executes the methods in response. It includes the methods listed below.

  • onStart(): An onStart() is call on only when any test method gets start.
  • onTestFinish(): An onTestFinish() is call on when the test finished.
  • onTestFailure(): An onTestFailure() is call on when the test fails.
  • onTestSuccess(): An onTestSuccess() is call on when a test is successes or pass.
  • onTestSkipped(): An onTestSkipped() is call on when test method is skipped.
  • onTestFailedButWithinSuccessPercentage(): An onTestFailedButWithinSuccessPercentage() is call on every time when the test method fails but within success percentage.

TestNG ITestListener Implementation

All methods in ITestListener class are now default in the current version of the TestNG ITestListener interface. This implies we don't have to override all methods; we can just override the ones we require. TestNG ITestListener Create a TestListener class and implement ITestListener.

package com.techgeeknext.testng.listeners;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class TestListener implements ITestListener{

	@Override
	public void onTestStart(ITestResult result) {
		System.out.println("====onTestStart Listener : "+ result);
	}

	@Override
	public void onTestSuccess(ITestResult result) {
		System.out.println("====onTestSuccess Listener : "+ result);
	}

	@Override
	public void onTestFailure(ITestResult result) {
		System.out.println("====onTestFailure Listener : "+ result);
	}

	@Override
	public void onTestSkipped(ITestResult result) {
		System.out.println("====onTestSkipped Listener : "+ result);
	}

	@Override
	public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
		System.out.println("====onTestFailedButWithinSuccessPercentage Listener : "+ result);
	}

	@Override
	public void onTestFailedWithTimeout(ITestResult result) {
		System.out.println("====onTestFailedWithTimeout Listener : "+ result);
	}

	@Override
	public void onStart(ITestContext context) {
		System.out.println("====onStart Listener===");
	}

	@Override
	public void onFinish(ITestContext context) {
		System.out.println("====onFinish Listener===");
	}


}

Use TestNG TestListener in a class

Use @Listeners annotation with Listener class to apply the listener methods on the same class.

package com.techgeeknext.testcases;

import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import com.techgeeknext.testng.listeners.TestListener;

@Listeners(TestListener.class)
public class FirstTestDemo {
	@Test(groups = { "Pre-Conn-Group" })
	public void testDBConnec() {
		System.out.println("testDBConnec Test");
	}

	@Test(groups = { "Pre-User-Group" })
	public void TestLoginUser() {
		System.out.println("TestLoginUser Test");
	}

	@Test(groups = { "Post-Group" })
	public void TestLogoutUser() {
		System.out.println("TestLogoutUser Test");
	}

	@Test
	public void SimpleTest() {
		System.out.println("SimpleTest Test");
	}
}
output: TestNG Listener

Use TestNG Listener from testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">

	<listeners>
		<listener class-name="com.techgeeknext.testng.listeners.TestListener" />
	</listeners>

	<test name="Test">
		<classes>
			<class name="com.techgeeknext.testcases.FirstTestDemo" />
		</classes>
	</test> <!-- Test -->

</suite> <!-- Suite -->

Recommendation for Top Popular Post :