【问题标题】:Get string from @Test annotation TestNG从@Test注解TestNG中获取字符串
【发布时间】:2015-10-20 20:22:18
【问题描述】:

我正在尝试从 TestNG 注释 @Test(groups="Foo") 中获取一个字符串,然后将其用作我动态生成的文件夹的名称。

如何从 TestNG 注释中获取文本 "Foo" 以便我可以使用它?

【问题讨论】:

    标签: java selenium selenium-webdriver testng testng-dataprovider


    【解决方案1】:

    我认为读取注释属性的更简单的解决方案(这将涉及反射和朋友)是使用相同的常量字符串:

    private static final String FOLDER = "Foo";
    
    @Test(groups = FOLDER)
    public void test() {
        //create the folder named FOLDER
    }
    

    【讨论】:

      【解决方案2】:

      您可以从Method 获得注解(您可以从Class.get{,Declared}Methods() 方法获得):

      Test test = method.getAnnotation(Test.class);
      

      如果存在注解,这将是非空的,如果不存在,则为空。如果它是非空的,那么你可以在test 上调用groups() 方法:

      String groups = test.groups();
      

      【讨论】:

        【解决方案3】:

        为什么不使用@BeforeMethod 方法?

        @BeforeMethod
        public void generateFolderFromGroups(Method m) {
            Test test = m.getAnnotation(Test.class);
            String[] groups = test.groups();
            // generate folder from groups
        }
        
        @Test(groups = "Foo")
        public void test() {
            // the Foo folder will be already created
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-09
          • 1970-01-01
          • 1970-01-01
          • 2020-11-26
          • 1970-01-01
          • 2022-11-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多