0

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?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
David
  • 3
  • 3

3 Answers3

1

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
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
1

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();
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

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
}
juherr
  • 5,640
  • 1
  • 21
  • 63