TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

TestNG

TestNG Custom Listener Example

Introduction

In this article, we will discuss the TestNG Custom Listener example. We use Custom Listener to listen to the tests and perform some custom implementations according to the needs. A custom listener in TestNG API can be created by implementing the ITestListener interface. Test Listener allows us to listen to test events and perform some actions based on Test Success, Failed, Skipped real time.

ITestListener Interface methods

ITestListener interface method signatures:

org.testng.ITestListener.onTestStart(ITestResult)
org.testng.ITestListener.onTestSuccess(ITestResult)
org.testng.ITestListener.onTestFailure(ITestResult)
org.testng.ITestListener.onTestSkipped(ITestResult)
org.testng.ITestListener.onTestFailedButWithinSuccessPercentage(ITestResult)
org.testng.ITestListener.onTestFailedWithTimeout(ITestResult)
org.testng.ITestListener.onStart(ITestContext)
org.testng.ITestListener.onFinish(ITestContext)

 

Custom Listener Diagram

 

 

We might need to look at 2 things before going further.

ITestResult – describes the result of a test, individual test details like pass/fail/skip, etc

ITestContext – test context which contains all the information for a given test run ( example passed tests, failed tests, etc. )

Custom Listener

A custom listener which implements the above interface is as shown below. We provide the implementations for the interface methods to log the test events on the console.

package com.testingdocs.listener;


//Custom Listener - TestingDocs TestNG Tutorials
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class TDocsListener implements ITestListener {

public void onTestStart(ITestResult result) {
System.out.println("TestingDocs>> onTestStart ::" + result.getMethod()
.getMethodName()); 
}

public void onTestSuccess(ITestResult result) {
System.out.println("TestingDocs>> onTestSuccess ::" + result.getMethod()
.getMethodName());
}


public void onTestFailure(ITestResult result) {
System.out.println("TestingDocs>> onTestFailure ::" + result.getMethod()
.getMethodName()); 
}

public void onTestSkipped(ITestResult result) { 
System.out.println("TestingDocs>> onTestSkipped ::" + result.getMethod()
.getMethodName());
}

public void onStart(ITestContext context) {
System.out.println("TestingDocs>> OnStart ::" + context.getName()); 
}

public void onFinish(ITestContext context) {
System.out.println("****************TestRun Report Header 
********************" );
System.out.println("Total Passed" + context.getPassedTests());
System.out.println("Total Failed" + context.getFailedTests());
System.out.println("Total Skipped" + context.getSkippedTests());
System.out.println("*****************TestRun Report Footer 
********************" );
}

public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub 
} 
}

 

Test Listener TestNG

 

There are several other interfaces in TestNG framework that can be used. For example,

ISuiteListener etc.

How to use the Listener ?

We can use the custom listener that we defined in many ways, as  below:

  • @Listeners Annotation
  • TestNG testng.xml XML file configuration
  • Command-line (-listener argument)

Adding a @Listeners annotation to the tests that need to behave as the listener is defined.

@Listeners(MyCustomListener.class)

Another popular way to specify the custom listener is from the TestNG XML configuration file. Also, we can add the ‘listeners’ section to the file. Furthermore, it only applies when running tests by specifying this particular configuration file.

Now let’s run a sample test class with the custom listener and check the output. I’m gonna use a class level @Listeners annotation for running the sample test class. The sample test class will have 1 pass test, 1 fail test, and one skip test.

Sample test class

import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test; 
import com.testingdocs.listener.TDocsListener;
@Listeners(TDocsListener.class) 
public class SampleTests { 

@Test public void samplePassTest() {
 Assert.assertEquals("testingdocs", "testingdocs");
} 

@Test public void sampleFailTest() { 
Assert.assertEquals("testing", "testingdocs");
 } 

@Test(dependsOnMethods={"sampleFailTest"}) 
public void sampleSkipTest() {
 Assert.assertEquals("", ""); 
} 
}

 

Run output:

TestingDocs>> OnStart ::Default test
TestingDocs>> onTestStart ::sampleFailTest
TestingDocs>> onTestFailure ::sampleFailTest
TestingDocs>> onTestStart ::samplePassTest
TestingDocs>> onTestSuccess ::samplePassTest
TestingDocs>> onTestStart ::sampleSkipTest
TestingDocs>> onTestSkipped ::sampleSkipTest
****************TestRun Report Header ********************
Total Passed[ResultMap map={[TestResult name=samplePassTest 
status=SUCCESS method=SampleTests.samplePassTest()[pri:0, instance:
com.testingdocs.sample.SampleTests@29e495ff] output={null}]=
SampleTests.samplePassTest()[pri:0, instance:
com.testingdocs.sample.SampleTests@29e495ff]}]
Total Failed[ResultMap map={[TestResult name=sampleFailTest 
status=FAILURE method=SampleTests.sampleFailTest()[pri:0, instance:
com.testingdocs.sample.SampleTests@29e495ff] output={null}]=
SampleTests.sampleFailTest()[pri:0, instance:
com.testingdocs.sample.SampleTests@29e495ff]}]
Total Skipped[ResultMap map={[TestResult name=sampleSkipTest 
status=SKIP method=SampleTests.sampleSkipTest()[pri:0, instance:
com.testingdocs.sample.SampleTests@29e495ff] output={null}]=
SampleTests.sampleSkipTest()[pri:0, instance:
com.testingdocs.sample.SampleTests@29e495ff]}]
*****************TestRun Report Footer ********************

TestNG Custom Listener

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

Related Posts

Tests TestNG Suite testng.xml

TestNG /

Run tests from TestNG Suite testng.xml file

Add TestNG library

TestNG /

Add the TestNG library to the project

new TestNG class

TestNG /

Create TestNG test class in Eclipse IDE.

TestNG Plugin IntelliJ

TestNG /

Enable TestNG in IntelliJ IDE

Installing Eclipse from Update Site

TestNG /

Install TestNG latest version from the update site

‹ How to Take Screenshot when an Assert fails› Customize SoftAssert in TestNG

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version