TestNG Listener – customize ITestListener

TestNG provides many listeners (as interfaces) which you can customise to your need to get output more readable.

Here are the listeners –

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

Source – TestNG Listeners

From this above listeners, we will see implementation of ITestListener

Watch the setup and output here

 

Unimplemented methods to implement are –

void onStart(ITestContext context)
Invoked after the test class is instantiated and before any configuration method is called.

void onFinish(ITestContext context)
Invoked after all the tests have run and all their Configuration methods have been called.

onTestFailure(ITestResult result)
Invoked each time a test fails.

onTestSkipped(ITestResult result)
Invoked each time a test is skipped.

onTestStart(ITestResult result)
Invoked each time before a test will be invoked.

onTestSuccess(ITestResult result)
Invoked each time a test succeeds.

Let’s see the implementation –

Create a new Maven project (com.testNG and projectName – TestNGSample) with below testng dependency

 

Create a testNG class – 1 Passed, 1 Failed and 1 Skipped scenario

TestNGNormal.java

 

Now, create a class TestNGListener.java which will implement ITestListener

 

Create a file, testng.xml

 

Run the testng.xml – Right click on the xml > Run As > TestNG Suite

OutPut –

Reference – TestNG listeners

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *