TestNG IExecutionListener Example

0

In this article on listeners, we will see an example of IExecutionListener listener that is used to monitor when a TestNG run starts and ends.

In TestNG, all listeners extend the marker interface ITestNGListener.

IExecutionListener has two methods, onExecutionStart() and onExecutionFinish(). Method onExecutionStart() is invoked before TestNG starts running the suites and onExecutionFinish() is run once all the suites have been run.

You can provide more then one IExecutionListener listener as you configure TestNG.

Use case for IExecutionListener

You may want to start a server or notify some interested parties before you start running your tests.
We will provide two implementations of IExecutionListener.

TomcatLauncher:

package com.javarticles.testng;

import org.testng.IExecutionListener;

public class TomcatLauncher implements IExecutionListener {
	private long startTime;

	@Override
	public void onExecutionStart() {
		startTime = System.currentTimeMillis();
		System.out.println("TestNG is going to start. Let's start the tomcat...");
		
	}

	@Override
	public void onExecutionFinish() {
		System.out.println("TestNG has finished, took around " + (System.currentTimeMillis() - startTime) + "ms, shutdown the server");
	}
}

JmsNotifier:

package com.javarticles.testng;

import org.testng.IExecutionListener;

public class JmsNotifier implements IExecutionListener {

	@Override
	public void onExecutionStart() {
		System.out.println("Notify using JMS that TestNG is going to start");
		
	}

	@Override
	public void onExecutionFinish() {
		System.out.println("Notify using JMS, TestNG is finished");
	}
}

Register Listeners using @Listeners

You can register listeners using @Listeners at class level. We specify here an array of test listeners class that extend org.testng.ITestNGListener.

TestA:

package com.javarticles.testng;

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

@Listeners({TomcatLauncher.class, JmsNotifier.class})
public class TestA {

    @Test
    public void someTestMethod() {
        System.out.println("someTestMethod");
    }
}

testngA.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite">
	<test name="Test">
		<classes>
			<class name="com.javarticles.testng.TestA" />
		</classes>
	</test>
</suite>

Output:

TestNG is going to start. Let's start the tomcat...
Notify using JMS that TestNG is going to start
[TestNG] Running:
  C:\javarticles_ws\testNGExecutionListener\test\testngA.xml

someTestMethod

===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

TestNG has finished, took around 43ms, shutdown the server
Notify using JMS, TestNG is finished

Register Listeners in testNG.xml

You can also register listeners using using the <listeners> element.

testngB.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite">
	<listeners>
		<listener class-name="com.javarticles.testng.JmsNotifier" />
		<listener class-name="com.javarticles.testng.TomcatLauncher" />
	</listeners>
	<test name="Test">
		<classes>
			<class name="com.javarticles.testng.TestB" />
		</classes>
	</test>
</suite>

Output:

Notify using JMS that TestNG is going to start
TestNG is going to start. Let's start the tomcat...
[TestNG] Running:
  C:\javarticles_ws\testNGExecutionListener\test\testngB.xml

someTestMethod

===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Notify using JMS, TestNG is finished
TestNG has finished, took around 112ms, shutdown the server

Download source code

This was an example about testNG Listeners. You can download the source code here: testNGExecutionListener.zip

Share.

Comments are closed.