top of page

TestNG custom listeners: ITestListener


For a better understanding of what these two types can offer, the following references should be read:   http://testng.org/javadocs/org/testng/ITestListener.html and http://testng.org/javadoc/org/testng/ITestContext.html.

Implementing the custom listener

The custom listener that will be used must implement all the ITestListener interface's methods, as depicted below:

public class SimpleTestListener implements ITestListener{

@Override
public void onTestStart(ITestResult result) {
}

@Override
public void onTestSuccess(ITestResult result) {
}

@Override
public void onTestFailure(ITestResult result) {
}

@Override
public void onTestSkipped(ITestResult result) {
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
}

@Override
public void onStart(ITestContext context) {
}

@Override
public void onFinish(ITestContext context) {
} 

 As a simple example, one would like for a test to print the name of it's method, the time when it started running (in milliseconds), and the result of the run.  In this case, the listener's implemented methods would look something like:

@Override
public void onTestStart(ITestResult result) {
        System.out.println("Test has started running:"  + result.getMethod().getMethodName() + " at:" + result.getStartMillis());
}

 
@Override
public void onTestSuccess(ITestResult result) {
   System.out.println("Result was: success");
}

@Override
public void onTestFailure(ITestResult result) {
   System.out.println("Result was: failure");
}

@Override
public void onTestSkipped(ITestResult result) {
   System.out.println("Test was skipped!");
}


Also, the list of failed and list of passed tests should be displayed. In this case, the listener's implemented methods would look something like:


@Override 
public void onFinish(ITestContext context) { 
System.out.println("Passed tests: " + context.getPassedTests());                      System.out.println("Failed tests:" + context.getFailedTests()); 
} 

Using the listener

Now, after the listener has been implemented, it needs to be declared so that the tests know how to use it. There are two ways of doing that: the first one is by adding a @Listeners annotation to the tests that need to behave as the listener says. The disadvantage here is that, if there are many test classes that need to use the listener, each test class must introduce the new annotation. This might be a more cumbersome job. The advantage is that if you want to run this class from your IDE (not from the command line), by using the annotation, the listener will be applied to this class. Such a test would look something like (note that the @Listeners annotation must be placed above a @Test annotation):

@Listeners(CustomTestListener.class)
@Test
public void someTest() {
...
}

However, if tests are run from an .xml configuration file (as described in https://imalittletester.com/2014/03/01/running-testng-tests/), a 'listeners' section can be added to this file,  in the 'suite' section, right above the 'test' section, so that all the tests that are run by using this file will by default use the new listener.  Note that if you take this approach, when running any test from the IDE directly (with 'Run as TestNG test'), the listener will not be applied. It applies only when running tests by specifying this configuration file. The listeners section would look like:


<listeners>
<listener class-name="path.To.The.Listener.CustomTestListener" />
</listeners>

Recent Posts

See All

Creating an Architecture for Your Automated Tests Writing Automated Tests in Small Increments 4 Times Your Automation Passed While Bugs Were Present Now That You’ve Created Automated Tests, Run Them!

This week my newest article was released, this time on how to use Log4j to log relevant test automation information: https://blog.testproject.io/2021/05/05/logging-test-automation-information-with-log

bottom of page