Get testCaseKey value from TestCase in TestNG

42 Views Asked by At

I have a test case like this:

@TestCaseKey(testCaseKey = "TestCase-001")
    @Test(groups = {WEB},
            description = "Verify this and That")
    public void verify(){
        System.out.println("Print this");
}

I want to get value of testCaseKey, and I want to parse it , within my test case, how can I do that?

1

There are 1 best solutions below

0
Alexey R. On

You can use just a usual way of accessing any sort of annotation in Java.

  1. Obtain method instance (in case you're looking for method annotation)
  2. Get all annotation by class
  3. Obtain required value

Example in code:

@TestCaseKey(testCaseKey = "TestCase-001")
@Test()
public void verify() throws NoSuchMethodException {
    String key = this
                    .getClass()
                    .getMethod("verify", null)
                    .getAnnotation(TestCaseKey.class).testCaseKey();
    System.out.println("Your key: " + key);
}