How to retrieve test method name in TestNG before and after execution?

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 Test method name. A Test method name can be retrieved before or after the execution of the test.

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

  • If the user wants to know which Test method is just executed, @AfterMethod can be used.

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

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 a Test method name. However, any of these dependencies can be used for @BeforeMethod or @AfterMethod. The only change will be in the import part where the corresponding library should be imported as per the native dependency used.

Scenario 1

Suppose the user wants to retrieve the name of a Test method before its execution. In this case, the code will be written inside @BeforeMethod to retrieve the Test method name.

As @BeforeMethod executes each time before the @Test method, the Test method name will be printed before execution and after that, the Test method will be executed.

Approach/Algorithm to solve this problem:

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

  • Step 2 − Write the following code inside @BeforeMethod

public void name(Method method) {
   System.out.println("Test name: " + method.getName());
}

Note − Instead of Parameter Method, any of remaining 3 native dependencies can be used. For example, ITestContext or XmlTest or ITestResult.

  • Step 3 − Write two different @Test methods in the class NewTestngClass.

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

  • Step 5 − Now, 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 commonTestNG class NewTestngClass

src/ NewTestngClass.java

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.lang.reflect.Method;

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");
   }
   @BeforeMethod
   public void name(Method method) {
      System.out.println("Test name: " + method.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"/>
      </classes>
   </test>
</suite>

Output

Test name: testCase1
in test case 1 of NewTestngClass
Test name: testCase2
in test case 2 of NewTestngClass
===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Scenario 2

Suppose the user wants to retrieve the name of a Test method after its execution. In this case, the code will be written inside @AfterMethod to retrieve the Test method name. As @AfterMethod executes each time after the @Test method, the Test method name will be printed after its execution.

Approach/Algorithm to solve this problem:

  • Step 1 − Create a TestNG class NewTestngClass and write the @AfterMethod method.

  • Step 2 − Write the following code inside @AfterMethod

public void name(Method method) {
   System.out.println("Test name: " + method.getName());
}

Note − Instead of parameter Method, any of remaining 3 native dependencies can be used. For example, ITestContext or XmlTest or ITestResult

  • Step 3 − Write two different @Test methods in the class NewTestngClass.

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

  • Step 5 − Now, 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 commonTestNG class NewTestngClass

src/ NewTestngClass.java

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
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");
   }
   @AfterMethod
   public void name(Method method) {
      System.out.println("Test name: " + method.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"/>
      </classes>
   </test>
</suite>

Output

in test case 1 of NewTestngClass
Test name: testCase1
in test case 2 of NewTestngClass
Test name: testCase2
===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
raja
Updated on 12-Jan-2022 11:28:10

Advertisements