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:
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:
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:
All method got executed as all belong to group G1.
Testng xml for group G2:
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.
Author: Amod Mahajan
A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.