Tuesday, November 26, 2013

Get name of test method in teardown

Sometimes we need name of the test method that has just executed in the tear down method for various purposes such as:
  • If you take screenshot in tear down and want to give last executed method name as a part of screenshot name.
  • May be someone want to track something in the log with the test method information.
  • If you have different classes containing test methods and tear down method, and you want to use test method information in tear down method.
@AfterMethod
public void yourTearDownMethod(ITestResult result) {
  System.out.println("method name:" + result.getMethod().getMethodName();
}
Similar way, you can get other information about the test method by result.getMethod().getXXX()