How to run test groups in TestNG?

Group test is a new innovative feature in TestNG, which is not available in JUnit framework. It permits you to dispatch methods into proper portions and perform sophisticated groupings of test methods.

  • Not only can you declare those methods that belong to groups, but you can also specify groups that contain other groups. Then, TestNG can be invoked and asked to include a certain set of groups (or regular expressions), while excluding other sets.

  • Group tests provide maximum flexibility in how you partition your tests. You do not have to recompile anything if you want to run two different sets of tests back to back.

  • Groups are specified in your testng.xml file using the <groups> tag. It can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.

Now, let's take an example to see how group test works.

Approach/Algorithm to solve this problem −

  • Step 1 − Create two TestNG classes − NewTestngClass and OrderofTestExecutionInTestNG.

  • Step 2 − Write two different @Test methods in both of the classes: NewTestngClass and OrderofTestExecutionInTestNG.

  • Step 3 − Mark each @Test with group as mentioned below in the code file.

  • Step 4 − Now create the testNG.xml as given below to execute @Test those are having group name as functest.

  • Step 5 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.

  • Step 6 − As per the given configuration, it will execute the Test Case 1 of NewTestngClass and both the test cases of OrderofTestExecutionInTestNG class.


The following code shows how to run testing groups −


import org.testng.annotations.Test;
public class NewTestngClass {
   @Test(groups = { "functest", "checkintest" })
   public void testCase1() {
      System.out.println("in test case 1 of NewTestngClass");
   @Test(groups = {"checkintest" })
   public void testCase2() {
      System.out.println("in test case 2 of NewTestngClass");

src/ −

import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
   // test case 1
   @Test(groups = { "functest" })
   public void testCase3() {
      System.out.println("in test case 3 of OrderofTestExecutionInTestNG");
   // test case 2
   @Test(groups = { "functest", "checkintest" })
   public void testCase4() {
      System.out.println("in test case 4 of OrderofTestExecutionInTestNG");


This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "" >
<suite name = "Suite1">
   <test name = "test1">
            <include name = "functest" />
         <class name = "NewTestngClass" />
         <class name = "OrderofTestExecutionInTestNG" />


in test case 1 of NewTestngClass
in test case 3 of OrderofTestExecutionInTestNG
in test case 4 of OrderofTestExecutionInTestNG

Total tests run: 3, Passes: 3, Failures: 0, Skips: 0

Updated on: 09-Mar-2022


Kickstart Your Career

Get certified by completing the course

Get Started