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:


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

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning