2011-12-12 100 views

回答

13

查看文檔中的dependency injection部分。它指出,依賴注入可以在此情況下可以使用例如:

不限@BeforeMethod(和@AfterMethod)可以聲明java.lang.reflect.Method類型的參數。此參數將接收一旦此@BeforeMethod完成後(或在爲@AfterMethod運行該方法後)將被調用的測試方法。

所以基本上你只需要在你的@BeforeMethod聲明java.lang.reflect.Method類型的參數,你將有機會獲得以下測試名稱的名稱。喜歡的東西:

@BeforeMethod 
protected void startTest(Method method) throws Exception { 
    String testName = method.getName(); 
    System.out.println("Executing test: " + testName); 
} 

還有一個方式使用ITestNGMethod接口(documentation),但我不是如何使用它完全確定,我只是讓你看看,如果你'感興趣。

+0

在你獲得方法名和類名運行多次的時間與我們在excel工作表中的manydata一樣多,所以我想將testcase名稱作爲變量(這在excel中)傳遞給AbstractBaseTestCase類中的before方法,是否有任何方法可以實現此目的? –

1

下面的例子解釋瞭如何方法

我正在使用的數據有多個數據集提供,所以在某種程度上報告它顯示了同樣的方法我的測試用例之前
@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()  
     } 

@Test 
public void exampleTest(){ 


} 
相關問題