TestNG Tutorials 30: Groups Attribute at Class Level In TestNG
Hello Folks,
In previous post, we learnt about basics of group concepts in TestNG and how to use with @Test annotated methods.
Before continuing this post, I will advise you to go through this post first:
@Test annotation at class level in TestNG
Suppose if you need to write multiple @Test annotated methods in a class, and each belongs to same group, you need to mention “groups” attribute for each method which is not easy as you need to write same lines of code again and again. Another problem if you want to update/change group name, you need to go to each test method and change it.
Solution is marking a class to group/s instead of methods.
TestNG allows you to mention “groups” attribute at class level also which will be inherited automatically to @Test annotated methods. It will eliminate writing same lines of code for each methods.
See an example below:
Testng xml:
Output on running:
Overriding “groups” attribute at test method level from class level:
Value of “groups” attribute can be overridden at test level from class level. Calling it “overridden” will not be perfect conceptually because when you declare a group for a test method at test level, that method will actually belong to group mentioned at class level and method level both. It is as good as multiple groups concept in TestNG.
We will see an example below:
Testng xml for group G1:
Output:
All method got executed as all belong to group G1.
Testng xml for group G2:
Output:
Since only method “G1Method3” belong to “G2”, so TestNG executes only that like normal grouping concept.
You can minimise same lines of code and maintenance as well. If you need to change group name, you just need to change at class level instead at each method level.
More about TestNG in upcoming posts. Stay tuned.
If you have any doubt, feel free to comment below. If you like my posts, please like, comment, share and subscribe. #ThanksForReading
#HappySelenium