2017-01-06 7 views
2

Я хочу напечатать имя В настоящее время Выполнения метода испытаний в @BeforeMethod и @AfterMethod использовании TestNG. как:Получить имя в настоящее время выполнения @Test имени метода в @BeforeMethod и @AfterMethod в TestNG

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

Возможный дубликат [Есть ли способ получить метаданные метода при использовании метода @BeforeMethod в testng?] (Http://stackoverflow.com/questions/8477702/is-there-a-way-to-get-method м- данных, когда-Усин г-beforemethod-в-TestNG) – juherr

ответ

1

Вы можете использовать слушателей как этот 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(); 

} 
1

От the documentation:

public class LoginTest { 

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


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

    @BeforeMethod 
    public void beforeTestCase(Method m) { 
    System.out.println(m.getName()); 
    } 

    @AfterMethod 
    public void AfterTestCase(Method m) { 
    System.out.println(m.getName()); 
    } 
} 
0

Ниже пример объясняет, как вы можете получить имя метода и имя класса вашего @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 

     }