2015-07-22 46 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]); 
     } 
    }