本文介绍了在@BeforeMethod 和@AfterMethod 中获取当前正在执行的@Test 方法名称在testng的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 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:-
//这属于 IInvokedMethodListener 并且会在每个方法之前执行,包括//@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();
}
这篇关于在@BeforeMethod 和@AfterMethod 中获取当前正在执行的@Test 方法名称在testng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!