0

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

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

4

3 回答 3

1

为什么不使用@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
}
于 2015-10-21T14:48:00.830 回答
1

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

private static final String FOLDER = "Foo";

@Test(groups = FOLDER)
public void test() {
    //create the folder named FOLDER
}
于 2015-10-20T20:25:17.327 回答
1

您可以从 a 获取注释Method(您可以从Class.get{,Declared}Methods()方法中获取):

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

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

String groups = test.groups();
于 2015-10-20T20:35:53.277 回答