How to Write a TestNG Listener that can Fetch Name of Executing Test Method?


A TestNG class can have different tests like test1, test2, test3 etc. and these tests can be grouped based on different groups like unit, integration or both or any bug number. User may want to run the @Test method based on groups. And, it is always convenient to know which test method belongs to which group so that these details can be included into report.

TestNG supports multiple ways to get test name at run time such as injection dependency and Listeners are most popular. Even in Listeners it can be achieved using ITestListener or IInvokedMethodListener.

In this article, let’s analyse how to get groups name using IInvokedMethodListener.

Approach/Algorithm to Solve this Problem

  • Step 1: import org.testng.annotations.Test for TestNG.

  • Step 2: Write an annotation as @test in NewTest class

  • Step 3: Create a method for the @Test annotation as test1 and add the different groups name as shown in program section

  • Step 4: Repeat the steps for test2 and test3 for multiple groups name.

  • Step 5: Now, create a ListenerClass that uses IInvokedMethodListener to fetch test names at beforeInvocation and afterInvocation method.

    Following code should bring all groups name of a @Test method:

    method.getTestMethod().getMethodName()

  • Step 6: Now create the testNG.xml and add Listener class.

  • Step 7: Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

Example

The following code to create a TestNG class and displays the Listener functionality:

src/NewTest.java

import org.testng.annotations.Test;
public class NewTest {
    @Test(groups = { "unit", "integration" })
    public void testCase1() {
        System.out.println("in test case 1 of NewTest");
    }
    @Test(groups = { "integration" })
    public void testCase2() {
        System.out.println("in test case 2 of NewTest");
    }
    @Test(groups = { "unit" })
    public void testCase3() {
        System.out.println("in test case 3 of NewTest");

    }
}

src/ListenerClass.java

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

import java.util.Arrays;

public class ListenerClass implements IInvokedMethodListener {

    @Override
    public void afterInvocation(IInvokedMethod method, ITestResult result) {
        System.out.println("This method is invoked after every config method - " + method.getTestMethod().getMethodName());
        System.out.println("This method is invoked after every config method to get groups name- " + Arrays.toString(method.getTestMethod().getGroups()));

    }

    @Override
    public void beforeInvocation(IInvokedMethod method, ITestResult result) {
        System.out.println("This method is invoked before every config method to get test name- " + Arrays.toString(method.getTestMethod().getMethodName()));

    }
}

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases.

It is very handy when limited tests are needed to execute rather than full suite.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Parent_Suite">
    <listeners>
        <listener class-name="ListenerClass"/>
    </listeners>
    <test name="group test">
        <classes>
            <class name="NewTest" />
        </classes>
    </test>
</suite>

Output

This method is invoked before every config method to get test name- testCase1
in test case 1 of NewTest
This method is invoked after every config method - testCase1
This method is invoked after every config method to get groups name- [unit, integration]
This method is invoked before every config method to get test name- testCase2
in test case 2 of NewTest
This method is invoked after every config method - testCase2
This method is invoked after every config method to get groups name- [integration]
This method is invoked before every config method to get test name- testCase3
in test case 3 of NewTest
This method is invoked after every config method - testCase3
This method is invoked after every config method to get groups name- [unit]

===============================================
Parent_Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

Updated on: 18-Aug-2023

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements