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

A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “TestNG Tutorials 30: Groups Attribute at Class Level In TestNG

  1. Hi Amod,

    I have read your article and it is very impressive and easy to learn.

    Currently, I ran into on issue where I have put “group” at class level and that class extends another class which needs to be run.

    Behaviour: What is happening here that, it does not goes into the parent class(i.e. parent class gets skipped) and it continues execution in same class. This is failing my execution.

    Here is sample:
    @Test(groups = {“create”})
    public class QuickDemoTestCase extends BaseTestCase {

    private CreateQuickDemo createQuickDemo;

    public void setUpPage() {
    createQuickDemo = new CreateQuickDemo(driver);

    @Test(suiteName = “Smoke”, testName = “navigateToQuickDemo”, priority = 1,
    groups = {“Mobile-123”, “quickDemo”, “create”},
    description = “This method will navigate user to Quick Demo screen”)
    private void navigateToQuickDemo() throws Exception {

    My “BaseTestCase” class contains server setup and capabilities setup.

    Need an insight here.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.