Listeners


There are several interfaces that allow you to modify TestNG's behavior. These interfaces are broadly called "TestNG Listeners". Here are a few listeners:

1).IExecutionListener
2). IAnnotationTransformer
3). IHookable
4). IInvokedMethodListener
5). IMethodInterceptor
6). IReporter
7). ISuiteListener
8). ITestListener

When you implement one of these interfaces, you can let TestNG know about it with either of the following ways:

Using -listener on the command line.
Using <listeners> with ant.
Using <listeners> in your testng.xml file.
Using the @Listeners annotation on any of your test classes.
Using ServiceLoader.

Here I am discussing only on IExecution Listeners

Example of IExecution Listener

IExecutionListener is a listener that monitor the beginning and end of a TestNG run. It has two methods, onExecutionStart () and onExecutionFinish ().onExecutionStart() is called before the TestNG starts running the suites and onExecutionFinish() is called after TestNG is done running all the test suites.

Lets create a IExecutionListener class is TestExecutionListener

package testngexp;
import org.testng.IExecutionListener;

publicclass TestExecutionListener implements IExecutionListener {
@Override
publicvoid onExecutionStart() {
System.out.println("-----TestNG is going to start------");
}

@Override
publicvoid onExecutionFinish() {
System.out.println("------TestNG Execution has been finished------");
}
}

In the above class we @Overrided two methods onExecutionStart and OnExecutionFinish.

Lets create another java class

package testngexp;
import org.testng.annotations.Test;

publicclassTCExeListnerExp {
@Test
publicvoid TestMethod(){
System.out.println("This Execution Listener Examples");
}

@Test
publicvoid TestMethod2(){
System.out.println("This Execution Listener Examples two");
}
}

TestNG.xml file

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"verbose="1">
<listeners>
<listenerclass-name="testngexp.TestExecutionListener"/>
</listeners>
<testname="Test1">
<classes>
<classname="testngexp.TCExeListnerExp"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->

Here , we should specify Lisnteners class in <listeners> tag with fully qualified name
<listenerclass-name="testngexp.TestExecutionListener"/>

Results
image

you can see the StartExecution messages are printed before TestNG starts running the suites(RED Marked) and the Finished Execution messages are printed once all the suites have been run.
No need to defined @ Listener class in TestNG.xml,
Another way you can define the listener class,Using the @Listeners annotation on any of your test classes.


Example

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

@Listeners({testngexp.TestExecutionListener.class})

publicclass TCExeListnerExp {
@Test
publicvoid TestMethod(){
System.out.println("This Execution Listener Examples");
}

@Test
publicvoid TestMethod2(){
System.out.println("This Execution Listener Examples two");
}
}


You should have defined listener classes at class level

Results
image

Comments