【问题标题】:How to Get test group name in TestNG如何在 TestNG 中获取测试组名称
【发布时间】:2015-10-11 21:46:07
【问题描述】:

我是 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 传递的参数吗?

【问题讨论】:

    标签: java selenium testng


    【解决方案1】:

    使用反射方法得到测试组如下:

    @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.
    }
    

    【讨论】:

      【解决方案2】:

      如果你想知道执行@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]);
              }
          }
      

      【讨论】:

        【解决方案3】:

        如果我理解正确,您想将组添加到 beforeMethod。

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

        【讨论】:

          猜你喜欢
          • 2012-01-25
          • 1970-01-01
          • 2011-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多