I am trying to get a string from a TestNG annotation @Test(groups="Foo")
and then use this as a name for a folder I am dynamically generating.
How do I get the text "Foo"
from the TestNG annotation so I can use it?
I am trying to get a string from a TestNG annotation @Test(groups="Foo")
and then use this as a name for a folder I am dynamically generating.
How do I get the text "Foo"
from the TestNG annotation so I can use it?
I think a simpler solution to reading the attribute of the annotation (which would involve reflection and friends) would be to use the same constant String:
private static final String FOLDER = "Foo";
@Test(groups = FOLDER)
public void test() {
//create the folder named FOLDER
}
You can get the annotation from a Method
(which you can get from the Class.get{,Declared}Methods()
method):
Test test = method.getAnnotation(Test.class);
This will be non-null if the annotation was present, and null if it was not. If it is non-null, you can then just call the groups()
method on test
:
String groups = test.groups();
Why not using a @BeforeMethod
method?
@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
}