Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 15, 2016

TestNG Grouping Tests





In TestNG, we can group multiple test methods into a named group. You can execute a particular set of test methods belonging to a group or multiple groups. This feature allows the test methods to be segregated into different sections or modules. You can have a set of test that belong to sanity test whereas others may belong to regression tests. Apart from this you can also segregate the tests based on the functionalities or features that the test method verifies. This helps in executing only a particular set of tests as and when required.

We can assign single test to multiple groups i.e. multiple groups can contain the same test method in their group.

1.Test belongs to single group:

Below is the sample program:

import org.testng.annotations.Test;

public class TestBelongsToSingleGroup
{
    @Test(groups = {"sanity-group"})
    public void testOne()
    {
        System.out.println("This test belongs to Sanity group");
    }
    
    @Test
    public void testTwo()
    {
        System.out.println("This test NOT belongs to any group");
    }
    
    @Test(groups = {"sanity-group"})
    public void testThree()
    {
        System.out.println("This test belongs to Sanity group");
    }
}

Execute the above program using testng.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="GroupSuite">
  <test name="GroupTest">
      <groups>
          <run>
              <include name="sanity-group"/>
          </run>
      </groups>
    <classes>
      <class name="TestBelongsToSingleGroup"/>
    </classes>
  </test>
</suite>

Output for the above program is:

Test belongs to single group

The above XML file contains only one test inside a suite. This contains the groups section defined by using the groups tag as shown in the code. The run tag represents the group that needs to be run. The include tag represents the name of the group that needs to be executed.

After executing the above xml file the given output had only two test methods result which are belongs to sanity-group and the one test is not executed which is not part of this group.

2.Test belongs to multiple groups:

We can assign a single test method to multiple groups. For example, you want to execute one script in sanity test and regression test. Then you can assign this test method to both the groups.

Below is the sample program:

import org.testng.annotations.Test;

public class TestBlongsToMultipleGroups
{
    @Test(groups = {"sanity-group"})
    public void testOne()
    {
        System.out.println("This test belongs to sanity-group");
    }
    
    @Test(groups = {"sanity-group","regression-group"})
    public void testTwo()
    {
        System.out.println("This test belongs to sanity-group and regression-group");
    }
    
    @Test(groups = {"regression-group"})
    public void testThree()
    {
        System.out.println("This test belongs to regression-group");
    }
}

The above class contains three test methods. Two of the test methods belong to one group each, where as one of the methods belongs to two groups, sanity-group and regression-group respectively.

Execute the above program using testng.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="GroupSuite">
  <test name="GroupTest One">
      <groups>
          <run>
              <include name="sanity-group"/>
          </run>
      </groups>
    <classes>
      <class name="TestBlongsToMultipleGroups"/>
    </classes>
  </test>
  <test name="GroupTest two">
      <groups>
          <run>
              <include name="regression-group"/>
          </run>
      </groups>
    <classes>
      <class name="TestBlongsToMultipleGroups"/>
    </classes>
  </test>
</suite>

The above testng XML suite contains two tests, each of them executing test methods belonging to a particular group.



Output for the above program is:

Test belongs to multiple groups

If we observe the output, testOne and testThree executes only once but the testTwo executes two times as this is belongs to two groups.

Including and Excluding groups:

TestNG also allows you to include and exclude certain groups from test execution. This helps in executing only a particular set of tests and excluding certain tests. A simple example can be when a feature is broken and you need to exclude a fixed set of tests from execution since these test will fail upon execution. Once the feature is fixed you can then verify the feature by just executing the respective group of tests.

Below is the sample program:

import org.testng.annotations.Test;

public class IncludeExcludeGroup
{
    @Test(groups={"include-group"})
    public void testMethodOne()
    {
        System.out.println("Test method one belonging to a group.");
    }


    @Test(groups={"include-group"})
    public void testMethodTwo()
    {
        System.out.println("Test method two belonging to a group.");
    }


    @Test(groups={"include-group","exclude-group"})
    public void testMethodThree()
    {
        System.out.println("Test method three belonging to two groups.");
    }
}

The above class contains three test methods that print a message onto console when executed. All the three methods belong to a group include-group whereas the testMethodThree method also belongs to the group exclude-group.

Execute the above program using testng.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
      <groups>
          <run>
              <include name = "include-group"/>
              <exclude name = "exclude-group"/>
          </run>
      </groups>
    <classes>
      <class name="IncludeExcludeGroup"/>
    </classes>
  </test>
</suite>

The above XML contains a simple test in which the group include-group is included in the test using the include XML tag and the group exclude-group is being excluded from the test execution by using the exclude tag.

Output for the above program is:

IncludeExcludegroup

From the test results TestNG executed two methods from the group include-group and excluded the third method that belonged to the group exclude-group, which was excluded from the test execution. If a test method belongs to both included and excluded group, the excluded group takes the priority and the test method will be excluded from the test execution. You can have as many include and exclude groups as you want while creating a test suite in TestNG.



Share this post: on Twitter on Facebook

TestNG DataProvider Dependency Test in TestNG

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025