获取@BeforeMethod中当前正在执行的@Test方法名称和testng中@AfterMethod的名称

时间:2017-01-06 09:11:07

标签: automation annotations testng

我想使用 testng @BeforeMethod@AfterMethod中打印当前执行测试方法的名称。 喜欢:

public class LoginTest {

@Test
public void Test01_LoginPage(){
    //Some Code here
}


@Test
public void Test02_LoginPage(){
    //Some Code Here
}

@BeforeMethod
public void beforeTestCase(){
    //Print Test method name which is going to execute.
}

@AfterMethod
public void AfterTestCase(){
    //Print Test method name which is executed.
}
}

3 个答案:

答案 0 :(得分:2)

来自the documentation

../../uJ/cerberOS_BSP.h:55:13: error: array type has incomplete element type
 extern char cmp_ids[][];

答案 1 :(得分:2)

您可以使用此类link之类的侦听器。链接中的重要代码: -

// This belongs to IInvokedMethodListener and will execute before every method including //@Before @After @Test

public void beforeInvocation(IInvokedMethod arg0, ITestResult arg1) {

    String textMsg = "About to begin executing following method : " + returnMethodName(arg0.getTestMethod());

    Reporter.log(textMsg, true);

}

// This belongs to IInvokedMethodListener and will execute after every method including @Before @After @Test

public void afterInvocation(IInvokedMethod arg0, ITestResult arg1) {

    String textMsg = "Completed executing following method : " + returnMethodName(arg0.getTestMethod());

    Reporter.log(textMsg, true);

}

// This will return method names to the calling function

private String returnMethodName(ITestNGMethod method) {

    return method.getRealClass().getSimpleName() + "." + method.getMethodName();

}

答案 2 :(得分:1)

以下示例说明如何在before方法和After方法中获取@test方法的方法名称和类名

@Test
public void exampleTest(){


}   


@BeforeMethod
        public  void  beforemethod(Method method){
//if you want to get the class name in before method
      String classname = getClass().getSimpleName();
//IF you want to get the method name in the before method 
      String methodName = method.getName() 
//this will return you   exampleTest   
        }

@AfterMethod
        public  void  beforemethod(Method method){
//if you want to get the class name in After method
      String classname = getClass().getSimpleName();
//IF you want to get the method name in the After method 
      String methodName = method.getName()  
//this will return you   exampleTest   

        }