User_77609
User_77609

Reputation: 33

TestNG - Add custom attribute to @Test annotation

I need to add a custom attribute to existing @Test annotation like below.

@Test (description = "some description", newAttribute = "some value") 
public void testMethod() {
    // unit/integration test code
}

Where 'newAttribute' is the new attribute I have added to existing attributes of @Test. Later as part of reporting, I will read/scrap all files in the workspace for this new attribute and enumerate the tests/methods that use it.

Can it be done in Java? Any help/pointers will be appreciated. Any better way of getting all the tests that use a particular attribute/annotation?

Upvotes: 2

Views: 4935

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 43997

No, there is no such thing as annotation inheritance and therefore, you cannot transparently add a value to a compatible type without recompiling the library that defines the annotation. You can however add another annotation to the method:

@Test (description = "some description") 
@NewAttribute ("some value")
public void testMethod() {
    // unit/integration test code
}

You can then read this custom annotation at a later point.

Upvotes: 1

Related Questions