2015-07-22 44 views
3
獲取測試組名

我是新來TestNG的,我有下面的代碼:如何在TestNG的

@BeforeMethod 
public void getGroup(ITestAnnotation annotation){ 
    System.out.println("Group Name is --" + annotation.getGroups()) ; 
} 

@Test(groups = { "MobilSite" }) 
public void test1(){ 
    System.out.println("I am Running Mobile Site Test"); 
} 

我想,我嘗試使用ITestAnnotation之前方法的組名,但是當我運行測試我我得到以下錯誤

Method getGroup requires 1 parameters but 0 were supplied in the @Configuration annotation. 

你能幫我參數,我應該從XML傳遞?

回答

-1

如果我理解正確,您想將組添加到beforeMethod。

@BeforeMethod(groups = {"MobilSite"}, alwaysRun = true) 
4

使用反射方法獲取如下組測試:

@BeforeMethod 
public void befMet(Method m){ 
     Test t = m.getAnnotation(Test.class); 
     System.out.println(t.groups()[0]);//or however you want to use it. 
} 
+0

從這裏學到了很多!謝謝! – drkthng

1

在情況下,如果你想知道的所有組的名稱,其執行@Test方法屬於:

@BeforeMethod 
    public void beforeMethod(Method method){ 
     Test testClass = method.getAnnotation(Test.class); 

     for (int i = 0; i < testClass.groups().length; i++) { 
      System.out.println(testClass.groups()[i]); 
     } 
    }