How to get the test group name in TestNG before and after execution?


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's a set of native dependencies in TestNG

  • ITestContext
  • XmlTest
  • Method
  • ITestResult

These dependencies help to retrieve the description of a Test method, if written. Group name of a Test method can be retrieved before or after the execution of a test.

  • If the user wants to get the group name of a test method prior to its execution, then @BeforeMethod can be useful to retrieve it.

  • On the other hand, if the user wants to know the group of test method is just executed, then @AfterMethod can be used.

The actual code can be written in either of these methods to retrieve the Test method description. @BeforeMethod and @AfterMethod support all these native dependencies. The full access of these dependencies is given below −

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

In this article, we will use Method dependency (in @BeforeMethod and @AfterMethod) to show how to retrieve a group of test methods. However, any of these dependencies can be used for @BeforeMethod or @AfterMethod.

Scenario 1

Suppose the user wants to retrieve the group of test methods before the execution. In this case, the code will be written inside @BeforeMethod to retrieve the group of test method. As @BeforeMethod executes each time before the @Test method, the group name of test method will be printed before execution and after that, the Test method will be executed. In this scenario, we will implement Method dependency.

Approach/Algorithm to solve this problem:

  • Step 1 − Create a TestNG class OrderofTestExecutionInTestNG and write the @BeforeMethod method.

  • Step 1 − Write the following code inside @BeforeMethod −

public void name(Method method) {
   Test testClass = method.getAnnotation(Test.class);
   for (int i = 0; i < testClass.groups().length; i++)
   {
      System.out.println(testClass.groups()[i]);
   }
}
  • Step 3 − Write two different @Test methods in the class OrderofTestExecutionInTestNG and add the group as test1 and test2 for the first Test method, while only test2 for the second Test method as shown in the following code.

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

  • 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, OrderofTestExecutionInTestNG

src/ OrderofTestExecutionInTestNG.java

import org.testng.annotations.*;
import java.lang.reflect.Method;
public class OrderofTestExecutionInTestNG {
   // test case 1
   @Test(groups={"test1", "test2"})
   public void testCase1() {
      System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
   }
   // test case 2
   @Test(groups={"test2"})
   public void testCase2() {
      System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
   }
   @BeforeMethod()
   public void name(Method method) {
      Test testClass = method.getAnnotation(Test.class);
      for (int i = 0; i < testClass.groups().length; i++) {
         System.out.println("Group name is: "+testClass.groups()[i]);
      }
   }
}

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

Group name is: test1
Group name is: test2
in test case 1 of OrderofTestExecutionInTestNG
Group name is: test2
in test case 2 of OrderofTestExecutionInTestNG
===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Scenario 2

Suppose the user wants to retrieve the group name of Test method after the execution. In this case, the code will be written inside @AfterMethod to retrieve the group of test method description.

As @AfterMethod executes each time after the @Test method, the group of test method will be printed after the execution of the Test method. In this scenario, we will implement Method dependency.

Approach/Algorithm to solve this problem

  • Step 1 − Create a TestNG class OrderofTestExecutionInTestNG and write the @BeforeMethod method.

  • Step 2 − Write the following code inside @BeforeMethod

public void name(Method method) {
   Test testClass = method.getAnnotation(Test.class);
   for (int i = 0; i < testClass.groups().length; i++) {
      System.out.println(testClass.groups()[i]);
   }
}
  • Step 3 − Write two different @Test methods in the class OrderofTestExecutionInTestNG and add the group as test1 and test2 for the first Test method, while only test2 for the second Test method, as shown in the following code.

  • 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, OrderofTestExecutionInTestNG

src/ OrderofTestExecutionInTestNG.java

import org.testng.annotations.*;
import java.lang.reflect.Method;
public class OrderofTestExecutionInTestNG {
   // test case 1
   @Test(groups={"test1", "test2"})
   public void testCase1() {
      System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
   }
   // test case 2
   @Test(groups={"test2"})
   public void testCase2() {
      System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
   }
   @AfterMethod()
   public void name(Method method) {
      Test testClass = method.getAnnotation(Test.class);
      for (int i = 0; i < testClass.groups().length; i++) {
         System.out.println("Group name is: "+testClass.groups()[i]);
      }
   }
}

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 test case 1 of OrderofTestExecutionInTestNG
Group name is: test1
Group name is: test2
in test case 2 of OrderofTestExecutionInTestNG
Group name is: test2
===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Updated on: 12-Jan-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements