How to get current class name from AfterClass in TestNG?


TestNG supports native dependency injection. It allows to declare additional parameters in methods. At the run time, TestNG automatically fill these parameters with right value. Following are few native dependencies in TestNG:

  • ITestContext

  • XmlTest

  • Method

  • ITestResult

These dependencies help to retrieve the test class name depends on where these are called. If user wants to retrieve test class name after execution, the best place is @AfterClass.

@AfterClass supports ITestContext and XmlTest.

However, the full access of these dependencies is as following:

Annotation

ITestContext

XmlTest

Method

ITestResult

BeforeSuite

Yes

No

No

No

BeforeTest

Yes

Yes

No

No

BeforeGroups

Yes

Yes

No

No

BeforeClass

Yes

Yes

No

No

BeforeMethod

Yes

Yes

Yes

Yes

Test

Yes

No

No

No

AfterMethod

Yes

Yes

Yes

Yes

AfterClass

Yes

Yes

No

No

AfterGroups

Yes

Yes

No

No

AfterTest

Yes

Yes

No

No

AfterSuite

Yes

No

No

No

In this article, we will use ITestContext dependency to show how to retrieve test class name as well as without using any dependency.

Scenario 1

When user wants to retrieve the name of test class after the execution. In this case, code will be written inside @AfterClass to retrieve current class name that will be executed.

As @AfterClass executes at last, class name will be printed after execution of any class.

Approach/Algorithm to solve this problem

  • Step 1: Create a TestNG classes − NewTestngClass

  • Step 2: Write the following code inside @AfterClass in the class;

  • public void name(ITestContext context) {
            System.out.println("in Afterclass of NewTestngClass");
            System.out.println("Test Class name using ITestContext is:"+context. getCurrentXmlTest().getClasses().stream().findFirst().get().getName());
    	System.out.println("Test Class name without using ITestContext is:"+this.getClass().getName());
    
        }
    
    • The first line will print in which class we are present. It is hard− coded.

    • The 2nd line of code will print the test class name at runtime using ITestContext Dependency.

    • The 3rd line of code will print the test class name at run time without using any dependency.

  • Step 3: Now create the testNG.xml as given below to run the TestNG classes.

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

Example

The following code for common TestNG class − NewTestngClass:

src/ NewTestngClass.java

import org.testng.ITestContext;
import org.testng.annotations.*;

public class NewTestngClass {

    // test case 1
    @Test()
    public void testCase1() {

        System.out.println("in test case 1 of NewTestngClass");
    }
    // test case 2
    @Test()
    public void testCase2() {
        System.out.println("in test case 2 of NewTestngClass");
    }
    @AfterClass
    public void name(ITestContext context) {
        System.out.println("in Afterclass of NewTestngClass");
        System.out.println("Test Class name using ITestContext is:"+context. getCurrentXmlTest().getClasses().stream().findFirst().get().getName());
	System.out.println("Test Class name without using ITestContext is:"+this.getClass().getName());

    }
} 

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 "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1" parallel = "none">
    <test name = "test1" preserve-order = "true">
        <classes>
            <class name = "NewTestngClass"/>

        </classes>
    </test>
</suite>

Output

in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
Test Class name using ITestContext is: NewTestngClass
in Afterclass of NewTestngClass
Test Class name without using ITestContext is: NewTestngClass

===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Updated on: 17-Aug-2023

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements