How to get a list of all the test methods in a TestNG class?


TestNG supports native dependency injection. It allows to declare additional parameters in methods. At the runtime, TestNG automatically fills these parameters with the right values. Here is a list of some native dependencies in TestNG:

  • ITestContext

  • XmlTest

  • Method

  • ITestResult

These dependencies help to retrieve the names of test methods depending on where they are called. If the user wants to retrieve the names of all the test methods that are going to be executed in a class, the best place is @BeforeClass or @AfterClass. @BeforeClass and @AfterClass support ITestContext and XmlTest.

The following table shows the full access of these dependencies −

AnnotationITestContextXmlTestMethodITestResult
BeforeSuiteYesNoNoNo
BeforeTestYesYesNoNo
BeforeGroupsYesYesNoNo
BeforeClassYesYesNoNo
BeforeMethodYesYesYesYes
TestYesNoNoNo
AfterMethodYesYesYesYes
AfterClassYesYesNoNo
AfterGroupsYesYesNoNo
AfterTestYesYesNoNo
AfterSuiteYesNoNoNo

In this article, we will use XmlTest dependency to show how to retrieve the names of all the test methods in a class.

Scenario 1

Suppose the user wants to retrieve the names of all the test methods present in a class before the execution. In this case, the code will be written inside @BeforeClass to retrieve the names of all the test methods that will be executed inside the class.

Since @BeforeClass executes each time before a new class starts executing in a test suite, the names of all the test methods will be printed before the execution of test methods in each class.

Approach/Algorithm to solve this problem −

  • Step 1 − Create two TestNG classes: NewTestngClass and OrderofTestExecutionInTestNG.

  • Step 2 − Write the following code inside @BeforeClass in both the classes;

public void name(ITestContext context) {
   System.out.println("in beforeclass of <class name>");
   ITestContext[] a = context.getAllTestMethods();
   for (ITestContext b : a) {
      if (b.getRealClass() == this.getClass()) {
         System.out.println(b.getConstructorOrMethod().getName());
      }
   }
}

Note: getAllTestMethods() gets the names of all the test methods as object, while getName() fetches the names individually.

  • Step 3 − Write two different @Test methods in both the classes.

  • Step 4 − Now create the testNG.xml as given below to run the TestNG classes.

  • Step 5 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.

Example

Use the following code for the common TestNG class "NewTestngClass" −

src/ NewTestngClass.java

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
public class NewTestngClass {
   @Test
   public void testCase1() {
      System.out.println("in test case 1 of NewTestngClass");
   }
   @Test
   public void testCase2() {
      System.out.println("in test case 2 of NewTestngClass");
   }
   @BeforeClass
   public void name(ITestContext context) {
      System.out.println("in beforeClass of NewTestngClass");
      ITestNGMethod[] a = context.getAllTestMethods();
      for (ITestNGMethod b : a) {
         if (b.getRealClass() == this.getClass()) {                                  
            System.out.println(b.getConstructorOrMethod().getName());
         }
      }
   }
}

src/OrderofTestExecutionInTestNG.java −

import org.testng.annotations.Test;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.annotations.BeforeClass;
public class OrderofTestExecutionInTestNG {
   // test case 3
   @Test
   public void testCase3() {
      System.out.println("in test case 3 of OrderofTestExecutionInTestNG");
   }
   // test case 4
   @Test
   public void testCase4() {
      System.out.println("in test case 4 of OrderofTestExecutionInTestNG");
   }
   @BeforeClass
   public void name(ITestContext context) {
      System.out.println("in beforeClass of OrderofTestExecutionInTestNG");
      ITestNGMethod[] a = context.getAllTestMethods();
      for (ITestNGMethod b : a) {
         if (b.getRealClass() == this.getClass()) {                  
            System.out.println(b.getConstructorOrMethod().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 the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "OrderofTestExecutionInTestNG"/>
      </classes>
   </test>
</suite>

Output

in beforeClass of NewTestngClass
testCase1
testCase2
in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
in beforeClass of OrderofTestExecutionInTestNG
testCase3
testCase4
in test case 3 of OrderofTestExecutionInTestNG
in test case 4 of OrderofTestExecutionInTestNG

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

Scenario 2

Suppose the user wants to retrieve the names of all the test methods present in a class after the execution. In this case, the code will be written inside @AfterClass to retrieve the names of all the test methods that are executed inside the class.

As @AfterClass executes each time after a new class completes execution in a test suite, the names of all the test methods will be printed after the execution of test methods in each class.

Approach/Algorithm to solve this problem −

  • Step 1 − Create two TestNG classes as NewTestngClass and OrderofTestExecutionInTestNG.

  • Step 2 − Write the following code inside @AfterClass in both the classes −

public void name(ITestContext context) {
   System.out.println("in beforeclass of ");
   ITestContext[] a = context.getAllTestMethods();
   for (ITestContext b : a) {
      if (b.getRealClass() == this.getClass()) {
         System.out.println(b.getConstructorOrMethod().getName());
      }
   }
}

NotegetAllTestMethods() gets the names of all the test methods as an object, while getName() fetches the names individually.

  • Step 3 − Write two different @Test methods in both the classes

  • Step 4 − Now create the testNG.xml as given below to run the TestNG classes.

  • Step 5 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.

Example

Use the following code for the common TestNG class "NewTestngClass":

src/ NewTestngClass.java

import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
public class NewTestngClass {
   @Test
   public void testCase1() {
      System.out.println("in test case 1 of NewTestngClass");
   }
   @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");
      ITestNGMethod[] a = context.getAllTestMethods();
      for (ITestNGMethod b : a) {
         if (b.getRealClass() == this.getClass()) {          
            System.out.println(b.getConstructorOrMethod().getName());
         }
      }
   }
}

src/OrderofTestExecutionInTestNG.java −

import org.testng.annotations.Test;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.annotations.BeforeClass;
public class OrderofTestExecutionInTestNG {
   // test case 3
   @Test
   public void testCase3() {
      System.out.println("in test case 3 of OrderofTestExecutionInTestNG");
   }
   // test case 4
   @Test
   public void testCase4() {
      System.out.println("in test case 4 of OrderofTestExecutionInTestNG");
   }
   @AfterClass
   public void name(ITestContext context) {
      System.out.println("in AfterClass of OrderofTestExecutionInTestNG");
      ITestNGMethod[] a = context.getAllTestMethods();
      for (ITestNGMethod b : a) {
         if (b.getRealClass() == this.getClass()) {            
            System.out.println(b.getConstructorOrMethod().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 the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "NewTestngClass"/>
         <class name = "OrderofTestExecutionInTestNG"/>
      </classes>
   </test>
</suite>

Output

in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
in AfterClass of NewTestngClass
testCase1
testCase2
in test case 3 of OrderofTestExecutionInTestNG
in test case 4 of OrderofTestExecutionInTestNG
in AfterClass of OrderofTestExecutionInTestNG
testCase3
testCase4

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

Updated on: 09-Mar-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements