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