The Ultimate Guide to TestNG Groups

Last Updated : 23 Jul, 2025

TestNG is a framework for testing in Java, which is one of the most used tools because of its simplicity and efficient test management. One of the most useful keywords for this is groups, wherein the developers can group the different test cases in groups and the tests can be executed group wise. Basically, this article explains other known as TestNG Groups in details, its components, kinds and even some practical cases with examples.

What Are TestNG Groups?

TestNG Groups is a feature that allows you to categorize and organize test methods into logical groups based on specific criteria. This feature comes in handy in making extensive projects with hundreds of thousands of test cases. You don’t have to execute all the test cases, you can run only a selected test case. For example, smoke tests or sanity tests only.

Key Technical Components:

  1. Defining Groups: You define groups in TestNG using the groups attribute in the @Test annotation.
  2. Running Groups: TestNG has a testng.xml, which allows to run only certain groups.
  3. Grouping Tests: A test method can belong to multiple groups, while multiple test methods can also be in one group.
  4. Excluding Groups: TestNG includes additional features allowing running test cases on task groups without running them.

How to Define and Use TestNG Groups

1. Grouping in TestNG

Group members can be specified by the use of the groups parameter within the @Test annotation.

Java
import org.testng.annotations.Test;

public class TestNGGroupExample {

    @Test(groups = "functional")
    public void testFunctional() {
        System.out.println("Functional test executed.");
    }

    @Test(groups = "smoke")
    public void testSmoke() {
        System.out.println("Smoke test executed.");
    }

    @Test(groups = { "functional", "regression" })
    public void testFunctionalAndRegression() {
        System.out.println("Functional and Regression test executed.");
    }
}

In this example:

  • The testFunctional method belongs to functional grouped tests.
  • The testSmoke method cannot be said to belong to functional grouped tests but smoke group.
  • The testFunctionalAndRegression method belongs to the functionality as well as the regression groups.

2. Executing Groups

Some of the groups can be executed by either modifying the testng.xml configuration file or simply specifying groups on the command line.

Example: Executing Groups via testng.xml

XML
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="TestNGGroupsSuite">
    <test name="TestNGGroups">
        <groups>
            <run>
                <include name="functional" />
                <exclude name="smoke" />
            </run>
        </groups>
        <classes>
            <class name="TestNGGroupExample" />
        </classes>
    </test>
</suite>

Additionally, to execute some of the groups using the command line you can use the -groups parameter option.

  • The functional group will be executed.
  • The smoke group will be excluded.
java -cp /path/to/testng.jar:/path/to/project testng -groups functional

Output

Output
Output


Advanced Grouping Techniques

1. Group Dependencies

In ‘TestNG’, you can also define and create group dependencies using the dependsOnGroups attribute. This means that you can construct a group of tests that can only run after a different group of tests have been run successfully.

Java
import org.testng.annotations.Test;

public class GroupDependencies {

    @Test(groups = "init")
    public void initialize() {
        System.out.println("Initialization complete.");
    }

    @Test(groups = "process", dependsOnGroups = "init")
    public void process() {
        System.out.println("Process started after initialization.");
    }

    @Test(groups = "cleanup", dependsOnGroups = "process")
    public void cleanup() {
        System.out.println("Cleanup completed after processing.");
    }
}

testng.xml

XML
<suite name="TestNGGroupsSuite">
    <test name="TestNGGroups">
        <groups>
            <run>
                <include name="process" />
                <include name="init" />
                <include name="cleanup" />
            </run>
        </groups>
        <classes>
            <class name="testNG.GroupDependencies" />
        </classes>
    </test>
</suite>

Output

Output
Output

In this example:

  1. The test case for process has a dependency on the init group of tests.
  2. The test case for cleanup has a dependency on the process group of tests.

2. Group Filtering with Include and Exclude

It is possible to include and exclude groups within both testng.xml and command line options in selective manner. Thus running targeted test suites becomes very flexible.

Example: Excluding Groups via testng.xml

XML
<groups>
    <run>
        <exclude name="regression" />
    </run>
</groups>

As in this case, the regression group of tests will not be executed, only the tests defined by the other groups will be executed

Conclusion

TestNG Groups are efficient and combinable structures that ease the management of huge amounts of test cases and selective execution, organization of the test processes and easier debugging. Tests categorization as well as test automation are made more maintainable using TestNG Groups towards complex projects. This means certain functional tests, critical smoke tests or fully completed series of regression tests could be performed say simultaneously with overlapping controls with the use of TestNG Groups. Using TestNG Groups effectively can save time, reduce complexity, and enhance the overall productivity of your testing efforts.

Comment

Explore