How to retrieve all test methods name in TestNG suite?

TestNGRest AssuredDynamic Programming

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

  • ITestContext
  • XmlTest
  • Method
  • ITestResult

These dependencies help to retrieve the name of the Test methods. If the user wants to retrieve the names of all the Test methods that are going to get executed, then the best place is @BeforeSuite or @AfterSuite.

@BeforeSuite and @AfterSuite support only ITestContext. The full access of these dependencies is shown below −

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

In this article, we will use Method dependency to show how to retrieve the names of all the Test methods.

Scenario 1

Suppose the user wants to retrieve the names of all the Test methods before the execution. In this case, the code will be written inside @BeforeSuite to retrieve the names of all the Test methods that will be executed inside the suite.

As @BeforeSuite executes first and only once for a test suite, the names of all the Test methods will be printed before the execution.

Approach/Algorithm to solve this problem

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

  • Step 2 − Write the following code inside @BeforeSuite in any one of classes; we will write it inside NewTestngClass

public void name(ITestContext context) {
   System.out.println("in beforeSuite of NewTestngClass");
   ITestNGMethod[] a = context.getAllTestMethods();
   for (ITestNGMethod b : a) {
      System.out.println(b.getConstructorOrMethod().getName());
   }
}

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

  • Step 3 − Write two different @Test methods in both the classes, NewTestngClass and OrderofTestExecutionInTestNG.

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

  • Step 5 − Finally, run the testNG.xml or directly testNG class 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.BeforeSuite;
import org.testng.annotations.Test;
import org.testng.ITestContext;
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");
   }
   @BeforeSuite
   public void name(ITestContext context) {
      System.out.println("in beforeSuite of NewTestngClass");
      ITestNGMethod[] a = context.getAllTestMethods();
      for (ITestNGMethod b : a) {
         System.out.println(b.getConstructorOrMethod().getName());
      }
   }
}

src/OrderofTestExecutionInTestNG.java:

import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
   // test case 1
   @Test
   public void testCase3() {
      System.out.println("in test case 3 of OrderofTestExecutionInTestNG");
   }
   // test case 2
   @Test
   public void testCase4() {
      System.out.println("in test case 4 of OrderofTestExecutionInTestNG");
   }
}

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 beforeTest of NewTestngClass
testCase1
testCase2
testCase3
testCase4
in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
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 after the execution. In this case, the code will be written inside @AfterSuite to retrieve the names of all the Test methods that are executed inside the suite.

As @AfterSuite executes at the end and only once for a test suite, the names of all the Test methods will be printed after the execution.

Approach/Algorithm to solve this problem:

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

  • Step 2 − Write the following code inside @AfterSuite in any one of classes; we will write it inside NewTestngClass

public void name(ITestContext context) {
   System.out.println("in beforeSuite of NewTestngClass");
   ITestNGMethod[] a = context.getAllTestMethods();
   for (ITestNGMethod b : a) {
      System.out.println(b.getConstructorOrMethod().getName());
   }
}

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

  • Step 3 − Write two different @Test method in both the classes, NewTestngClass and OrderofTestExecutionInTestNG.

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

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

Program Code

Use the following code for the common TestNG class, NewTestngClass

src/ NewTestngClass.java

import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;
import org.testng.ITestContext;
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");
   }
   @AfterSuite
   public void name(ITestContext context) {
      System.out.println("in AfterSuite of NewTestngClass");
      ITestNGMethod[] a = context.getAllTestMethods();
      for (ITestNGMethod b : a) {
         System.out.println(b.getConstructorOrMethod().getName());
      }
   }
}

src/OrderofTestExecutionInTestNG.java:

import org.testng.annotations.Test;
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");
   }
}

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" 7gt;

<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 test case 3 of OrderofTestExecutionInTestNG
in test case 4 of OrderofTestExecutionInTestNG
in AfterTest of NewTestngClass
testCase1
testCase2
testCase3
testCase4
===============================================
Suite1
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
================================================
raja
Published on 12-Jan-2022 12:53:16
Advertisements