本文介绍了在testng中的@BeforeMethod和@AfterMethod中获取当前正在执行的@Test方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to Print the name of Currently Executing Test Method in @BeforeMethod and @AfterMethod using testng.Like :

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.
}
}

推荐答案

您可以使用这样的监听器链接.链接中的重要代码:-

You can use listeners like this link. Important code from the 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();

}

这篇关于在testng中的@BeforeMethod和@AfterMethod中获取当前正在执行的@Test方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 12:28