Sunday 15 March 2020

TestNG: Working with ITestListener

ItestListener is a listener class for running test class. Below table summarizes the methods in ItestListener class.

Method
Description
void onTestStart(ITestResult result)
Invoked each time before a test will be invoked.
void onTestSuccess(ITestResult result)
Invoked each time a test succeeds.
void onTestFailure(ITestResult result)
Invoked each time a test fails.
void onTestSkipped(ITestResult result)
Invoked each time a test is skipped.
void onTestFailedButWithinSuccessPercentage(ITestResult result)
Invoked each time a method fails but has been annotated with successPercentage and this failure
still keeps it within the success percentage requested.
void onTestFailedWithTimeout(ITestResult result)
Invoked each time a test fails due to a timeout.
void onStart(ITestContext context)
Invoked before running all the test methods belonging to the classes inside the test tag and calling all their Configuration methods.
void onFinish(ITestContext context)
Invoked after all the test methods belonging to the classes inside the test tag have run
and all their Configuration methods have been called.
Find the below working application.

Step 1: Implement ItestListener interface.

CustomTestListener.java
package com.sample.app.tests;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class CustomTestListener implements ITestListener {
 public void onTestStart(ITestResult result) {
  System.out.println(result.getName() + " test case started");
 }

 public void onTestSuccess(ITestResult result) {
  System.out.println("The name of the testcase passed is :" + result.getName());
 }

 public void onTestFailure(ITestResult result) {
  System.out.println("The name of the testcase failed is :" + result.getName());
 }

 public void onTestSkipped(ITestResult result) {
  System.out.println("The name of the testcase skipped is :" + result.getName());
 }

 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
  System.out.println("The name of the testcase on test failed but within success percentage :" + result.getName());
 }

 public void onTestFailedWithTimeout(ITestResult result) {
  System.out.println("Test failed with time out :" + result.getName());
 }

 public void onStart(ITestContext context) {

 }

 public void onFinish(ITestContext context) {
  System.out.println("Test Finished : " + context.getName());
 }
}

Step 2: Specify the listener using @Listener annotation.
@Listeners(CustomTestListener.class)

CustomTestListenerDemo.java
package com.sample.app.tests;

import static org.testng.Assert.assertTrue;

import org.testng.SkipException;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(CustomTestListener.class)
public class CustomTestListenerDemo {
 @BeforeTest
 public void beforeTest() {
  System.out.println("Inside before test");
 }

 @AfterTest
 public void afterTest() {
  System.out.println("Inside after test");
 }

 @Test
 public void test1() {
  System.out.println("Inisde test 1");
 }

 @Test
 public void test2() {
  System.out.println("Inisde test 2");
 }

 @Test
 public void test3() {
  throw new SkipException("test3 is skipped");
 }

 int i = 0;

 @Test(successPercentage = 50, invocationCount = 5)
 public void test4() {
  i++;
  System.out.println("Inisde test 4, invocation count : " + i);

  if (i % 2 == 0) {
   assertTrue(false);
  }
 }

}

Run CustomTestListenerDemo.java, you will see below messages in console.
[RemoteTestNG] detected TestNG version 7.0.0
Inside before test
test1 test case started
Inisde test 1
The name of the testcase passed is :test1
test2 test case started
Inisde test 2
The name of the testcase passed is :test2
test3 test case started
The name of the testcase skipped is :test3
test4 test case started
Inisde test 4, invocation count : 1
The name of the testcase passed is :test4
test4 test case started
Inisde test 4, invocation count : 2
The name of the testcase on test failed but within success percentage :test4
test4 test case started
Inisde test 4, invocation count : 3
The name of the testcase passed is :test4
test4 test case started
Inisde test 4, invocation count : 4
The name of the testcase on test failed but within success percentage :test4
test4 test case started
Inisde test 4, invocation count : 5
The name of the testcase passed is :test4
Inside after test
Test Finished : Default test
PASSED: test1
PASSED: test2
PASSED: test4
PASSED: test4
PASSED: test4
SKIPPED: test3
org.testng.SkipException: test3 is skipped
 at com.sample.app.tests.CustomTestListenerDemo.test3(CustomTestListenerDemo.java:35)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
 at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
 at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
 at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
 at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
 at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
 at java.util.ArrayList.forEach(ArrayList.java:1257)
 at org.testng.TestRunner.privateRun(TestRunner.java:770)
 at org.testng.TestRunner.run(TestRunner.java:591)
 at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
 at org.testng.SuiteRunner.run(SuiteRunner.java:304)
 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
 at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
 at org.testng.TestNG.runSuites(TestNG.java:1032)
 at org.testng.TestNG.run(TestNG.java:1000)
 at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
 at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 8, Failures: 0, Skips: 1
===============================================


===============================================
Default suite
Total tests run: 8, Passes: 5, Failures: 2, Skips: 1
===============================================

You can even specify the listener in an xml file.

CustomTestListenerDemo1.java
package com.sample.app.tests;

import static org.testng.Assert.assertTrue;

import org.testng.SkipException;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class CustomTestListenerDemo1 {
 @BeforeTest
 public void beforeTest() {
  System.out.println("Inside before test");
 }

 @AfterTest
 public void afterTest() {
  System.out.println("Inside after test");
 }

 @Test
 public void test1() {
  System.out.println("Inisde test 1");
 }

 @Test
 public void test2() {
  System.out.println("Inisde test 2");
 }

 @Test
 public void test3() {
  throw new SkipException("test3 is skipped");
 }

 int i = 0;

 @Test(successPercentage = 50, invocationCount = 5)
 public void test4() {
  i++;
  System.out.println("Inisde test 4, invocation count : " + i);

  if (i % 2 == 0) {
   assertTrue(false);
  }
 }

}

listenerDemo.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >

<suite name="sanity test suite" time-out="5000">

 <listeners>
  <listener
   class-name="com.sample.app.tests.CustomTestListener"></listener>
 </listeners>

 <test name="Demo test app">
  <classes>
   <class name="com.sample.app.tests.CustomTestListenerDemo1"></class>
  </classes>
 </test>


</suite>

How to run the test from xml file?
Right click on xml file -> Run As -> TestNG Suite.



Previous                                                    Next                                                    Home

No comments:

Post a Comment