top of page

TestNG Groups

What Are TestNG Groups ?

TestNG groups imply the process of grouping more than one tests together into  a single set which can be executed in a single command.Tests can belong to different different classes.

We define a group in our .xml and then pass this group name with test annotations.

Syntax :

@Test(groups = { "GroupName" })

Multiple group name can be passed in a test annotation by separating each group name with ' , ' .

@Test(groups = { "GroupName1","GroupName2" })

Groups are created at these two levels :

  • Suite Level : Groups are defined in <suite> tag that apply on all the <test> tag in .xml.

  • Class Level : Groups are defined in <test> tag that apply only on that particular <test> tag in .xml.

Let's create two example classes as TestNGGroupExample1 and TestNGGroupExample2 :

TestNGGroupExample1 :

package testNGexamples;

import org.testng.annotations.Test;

public class TestNGGroupExample1 {

    @Test(groups = { "Test Group In Suite" })
   public void test1() {
       System.out.println("test1 of Test Group In Suite is executing :");
   }

    @Test(groups = { "Test Group In Test" })
   public void test2() {
       System.out.println("test2 of Test Group In Test is executing :");

    }

}
 

TestNGGroupExample2 :

package testNGexamples;

import org.testng.annotations.Test;

public class TestNGGroupExample2 {

    @Test(groups = { "Test Group In Test" })
   public void test3() {
       System.out.println("test3 of Test Group In Test is executing :");

    }

    @Test(groups = { "Test Group In Suite" })
   public void test4() {
       System.out.println("test4 of Test Group In Suite is executing :");

    }

}
 

Now group the tests of these classes at suite and class level.

.XML for testNG group at suite level :

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Suite">
   <groups>
       <run>
           <include name="Test Group In Suite"></include>
       </run>
   </groups>
   <test name="Group Test1">
       <classes>
           <class name="testNGexamples.TestNGGroupExample1"></class>
       </classes>
   </test>
   <test name="GroupTest2">
       <classes>
           <class name="testNGexamples.TestNGGroupExample2"></class>
       </classes>
   </test>
</suite>

Output :

[RemoteTestNG] detected TestNG version 7.4.0
test1 of Test Group In Suite is executing :
test4 of Test Group In Suite is executing :

===============================================
Test-Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

.XML for testNG group at class level :

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Suite">
   <test name="Group Test">
       <groups>
           <run>
               <include name="Test Group In Test"></include>
           </run>
       </groups>
       <classes>
           <class name="testNGexamples.TestNGGroupExample1"></class>
           <class name="testNGexamples.TestNGGroupExample2"></class>
       </classes>
   </test>
</suite>

 

Output :

[RemoteTestNG] detected TestNG version 7.4.0
test2 of Test Group In Test is executing :
test3 of Test Group In Test is executing :

===============================================
Test-Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

TestNG supports nested group as well that is a group inside another group.

In .xml we have to define the nested group within <group> tag and then include it inside <run> .

 <groups>

<define name="Nested-Group-Name">

<include name="Test-Group-Name"></include>

</define>

 <run>

<include name="Nested-Group-Name"></include>

           </run>

</groups>

Refer next page Dependent Tests In TestNG

bottom of page