How to get test case details at runtime in TestNG?

This post will discuss how to obtain details of a test case at runtime. We might require this information in various scenarios, such as creating customized reports or developing a live tracking system to monitor test case progress.

Let’s begin by running a test suite using TestNG. Then, we will try retrieving the details of each test case when it is marked as passed or failed.

Executing a test suite
Making two test classes

We will make two test classes, CodekruTest1 and CodekruTest2.

CodekruTest1.java

package org.website.codekru;

import org.testng.annotations.Test;

public class CodekruTest1 {
	
	@Test
	public void testCase1() {
		System.out.println("Executing test case 1 in CodekruTest1");
	}
	
	@Test
	public void testCase2() {
		System.out.println("Executing test case 2 in CodekruTest1");
	}
	

}

CodekruTest2.java

package org.website.codekru;

import org.testng.annotations.Test;

public class CodekruTest2 {
	
	@Test
	public void testCase1() {
		System.out.println("Executing test case 1 in CodekruTest2");
	}
	
	@Test
	public void testCase2() {
		System.out.println("Executing test case 2 in CodekruTest2");
	}
	

}
Executing these two classes via testng.xml

We will use the testng.xml file to run the classes mentioned above. Running test cases with testng.xml provides better control over the execution process and offers insight into how the cases will be executed.

<suite name="codekru">

	<test name="Test1">
		<classes>
			<class name="org.website.codekru.CodekruTest1" />
		</classes>
	</test>
	
	<test name="Test2">
		<classes>
			<class name="org.website.codekru.CodekruTest2" />
		</classes>
	</test>
</suite>	

We have created two tests (<test>), each containing one class. Let’s run the XML file above.

Executing test case 1 in CodekruTest1
Executing test case 2 in CodekruTest1
Executing test case 1 in CodekruTest2
Executing test case 2 in CodekruTest2
Getting test case details at runtime

TestNG provides an ITestListener interface with several methods that allow us to obtain detailed information about the test cases being executed. For example, we can use the onTestSuccess() method to retrieve essential data points such as test case results, runtime information, and other relevant details whenever a test case is marked as passed. By utilizing these capabilities, we can gain valuable insights into the performance of our automated test suite and get a detailed view of our application’s behavior under testing conditions.

Let’s create a class that implements the ITestListener interface. We will try to retrieve test case details whenever a test case is passed.

package org.website.codekru;

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

public class TestListenerClass implements ITestListener {

	public void onTestSuccess(ITestResult result) {
		
	}

}

We will now fetch the below data points in the OnTestSuccess() method –

  • Name of the test case
  • Name of the test ( test name used in the <test> tag in testng.xml )
  • How many test cases are there in the current test (<test>)
  • How many cases are marked as passed till now

The ITestResult argument passed in the onTestSuccess() describes the results of a test and can thus be used to find the above data points.

How to get the name of the test case/method that was currently executed?
String currentTestCaseName = result.getMethod().getMethodName();
How to get the name of the test ( name passed in the <test> tag)
String currenTestName = result.getTestContext().getCurrentXmlTest().getName();
How to get the total number of test cases in the test?
int totalCasesInTheTest = result.getTestContext().getAllTestMethods().length;
How to get number of passed cases till now in the current test ( <test> tag ) ?
int passedCasesTillNow = result.getTestContext().getPassedTests().size()+1;

We incremented the value by 1 to count the current test case.

Whole code

package org.website.codekru;

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

public class TestListenerClass implements ITestListener {

	public void onTestSuccess(ITestResult result) {
		
		String currentTestCaseName = result.getMethod().getMethodName();
		String currenTestName = result.getTestContext().getCurrentXmlTest().getName();
		int totalCasesInTheTest = result.getTestContext().getAllTestMethods().length;
		int passedCasesTillNow = result.getTestContext().getPassedTests().size()+1;
		
		System.out.println("Current Test Case Name: " + currentTestCaseName);
		System.out.println("Current Test Name: " + currenTestName);
		System.out.println("Total Case In The Test: " + totalCasesInTheTest);
		System.out.println("Passed Cases Till Now In The test: " + passedCasesTillNow);
		
		
	}

}

To use the Listener in the test execution, include it in the testng.xml file as shown below:

<suite name="codekru">

	<listeners>
		<listener class-name="org.website.codekru.TestListenerClass" />
	</listeners>

	<test name="Test1">
		<classes>
			<class name="org.website.codekru.CodekruTest1" />
		</classes>
	</test>

	<test name="Test2">
		<classes>
			<class name="org.website.codekru.CodekruTest2" />
		</classes>
	</test>
</suite>	

Now, execute the above XML file.

Executing test case 1 in CodekruTest1
Current Test Case Name: testCase1
Current Test Name: Test1
Total Case In The Test: 2
Passed Cases Till Now In The test: 1
Executing test case 2 in CodekruTest1
Current Test Case Name: testCase2
Current Test Name: Test1
Total Case In The Test: 2
Passed Cases Till Now In The test: 2
Executing test case 1 in CodekruTest2
Current Test Case Name: testCase1
Current Test Name: Test2
Total Case In The Test: 2
Passed Cases Till Now In The test: 1
Executing test case 2 in CodekruTest2
Current Test Case Name: testCase2
Current Test Name: Test2
Total Case In The Test: 2
Passed Cases Till Now In The test: 2

This is how to easily retrieve relevant information about a test case. We have only used the onTestSuccess() method, but depending on your requirements, you may also want to use other methods, such as onTestStart() or onTestFailure().

We hope you liked the article. Please let us know in the comments if you want us to retrieve specific information about a test case.

Liked the article? Share this on

Leave a Comment

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