我正在尝试从 TestNG 注释中获取一个字符串@Test(groups="Foo")
,然后将其用作我动态生成的文件夹的名称。
如何"Foo"
从 TestNG 注释中获取文本以便我可以使用它?
我正在尝试从 TestNG 注释中获取一个字符串@Test(groups="Foo")
,然后将其用作我动态生成的文件夹的名称。
如何"Foo"
从 TestNG 注释中获取文本以便我可以使用它?
为什么不使用@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
}
我认为读取注释属性(这将涉及反射和朋友)的更简单的解决方案是使用相同的常量字符串:
private static final String FOLDER = "Foo";
@Test(groups = FOLDER)
public void test() {
//create the folder named FOLDER
}
您可以从 a 获取注释Method
(您可以从Class.get{,Declared}Methods()
方法中获取):
Test test = method.getAnnotation(Test.class);
如果存在注解,这将是非空的,如果不存在,则为空。如果它是非空的,那么您可以只调用该groups()
方法test
:
String groups = test.groups();