0

I am using Reflection Method Class,or also ITestResult Class result to fetch the name of the currently running test,


@AfterMethod
public String getTestMethodName(ITestResult result)
{
return result.getName();
}
Rohit
  • 11
  • 1
    Does this answer your question? [Retrieve test name on TestNG](https://stackoverflow.com/questions/8596632/retrieve-test-name-on-testng) – JeffC Aug 30 '22 at 14:32
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 31 '22 at 12:23

2 Answers2

1

Declare a parameter of type ITestResult in your @AfterMethod and TestNG will inject it

@AfterMethod
public void afterMethod(ITestResult result) {
  System.out.println("method name:" + result.getMethod().getMethodName());
}

OR

If you want to get the method name before the test is executed you can use the following:

import java.lang.reflect.Method;

@BeforeMethod
public void nameBefore(Method method)
{
    System.out.println("Test name: " + method.getName());       
}
Akzy
  • 1,817
  • 1
  • 7
  • 19
0

Whenever you are creating a new object, pass the testcase name to the constructor and initilaize a testcaseName variable with the value from the Test method. LoginTest.java:

 public class LoginTest{
@Test
    public void sampleTest(Method method) {
        String testcaseName = method.getName();
        LoginPageActions loginPageActions = new LoginPageActions(testcaseName);
    }

}

LoginPageActions.java:

 public class LoginPageActions{
    private String testCaseName = null;
public LoginPageActions(String testcaseName) {
        this.testCaseName = testcaseName;

    }
}
Jishnu G
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 07 '22 at 13:37