TestNG Groups Example

By Arvind Rai, March 28, 2023
On this page, we will learn using TestNG groups in unit test cases.
1. In TestNG, we can create the group of test methods. A test method can be associated with one or more than one groups. We can create group of groups i.e. a group that will include other groups.
2. The advantage of creating groups of test methods is that we can include certain set of groups and execute them. We can also exclude the groups of test methods if needed. The grouping of test methods gives us maximum flexibility in how we partition our tests. We need not to recompile anything if we want to run two different sets of tests back to back.
3. Groups are specified in testng.xml file and can be found either under <test> or <suite> tag. The groups specified in <suite> tag are applied to all <test> tags. Suppose a group g1 is specified in <suite> tag and a group g2 is specified in <suite> tag, then groups g1 and g2 both will be included.

On this page, we will learn creating groups, group of groups, excluding groups and partial groups with examples.

1. Using Test groups

In this example, we are creating two groups of test methods that are functional and calculation. All the functional related test methods will be grouped as functional and all the calculation related methods will be grouped as calculation.
Find the sample test files.
MyTestOne.java
public class MyTestOne {
  @Test(groups = {"calculation", "functional"})
  public void divisionTest() {
	System.out.println("MyTestOne: divisionTest...");
	assertEquals(30 / 5, 6);
  }
} 
MyTestTwo.java
public class MyTestTwo {
  @Test(groups = {"functional"} )
  public void nameMatchTest() {
	System.out.println("MyTestTwo: nameMatchTest...");
	String name = "Mohit";
	assertTrue("Mohit".equals(name));
  }
  @Test(groups = {"calculation"} )
  public void multiplyTest() {
	System.out.println("MyTestTwo: multiplyTest...");	
	assertEquals(4 * 16, 64);
  }
} 
We have two test files, some test methods are functional, some are calculation and some are both.
Now find the testng.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1">
  <test name="Regression1">
    <groups>
      <run>
        <include name="functional" />
        <include name="calculation" />
      </run>
    </groups>
    <classes>
      <class name="com.concretepage.MyTestOne" />
      <class name="com.concretepage.MyTestTwo" />
    </classes>
  </test>
</suite> 
In the above xml, we have configured both groups, hence all associated test methods of configured test classes will execute.
To run testng.xml in Eclipse, right click on xml file -> Run As ->TestNG.
Find the output in console.
MyTestOne: divisionTest...
MyTestTwo: multiplyTest...
MyTestTwo: nameMatchTest...
If we include only functional group, then test methods associated with only this group will execute.
<groups>
  <run>
      <include name="functional" />
  </run>
</groups> 
Output
MyTestOne: divisionTest...
MyTestTwo: nameMatchTest... 
If we include only calculation group, then test methods associated with only this group will execute.
<groups>
   <run>
	<include name="calculation" />
   </run>
</groups> 
Output
MyTestOne: divisionTest...
MyTestTwo: multiplyTest... 

2. Using Regular Expressions

Test groups can be included using regular expression. Suppose we have group names such as windows.functional, windows.calculation, linux.functional and linux.calculation. Find the test classes associated with these groups.
MyTestOne.java
public class MyTestOne {
  @Test(groups = {"windows.calculation"})
  public void divisionOnWindowsTest() {
	System.out.println("MyTestOne: divisionOnWindowsTest...");
	assertEquals(30 / 5, 6);
  }
  @Test(groups = {"linux.calculation"})
  public void divisionOnLinuxTest() {
	System.out.println("MyTestOne: divisionOnLinuxTest...");
	assertEquals(40 / 5, 8);
  }
}
MyTestTwo.java
public class MyTestTwo {
  @Test(groups = {"windows.functional"} )
  public void functionalOnWindowsTest() {
	System.out.println("MyTestTwo: functionalOnWindowsTest...");
	String name = "Mohit";
	assertTrue("Mohit".equals(name));
  }
  @Test(groups = {"linux.functional"} )
  public void functionalOnLinuxTest() {
	System.out.println("MyTestTwo: functionalOnLinuxTest...");
	String name = "Krishna";
	assertTrue("Krishna".equals(name));
  }  
} 
Now find the testng.xml file.
<suite name="Suite1" verbose="1">
   <test name="Regression1">
       <groups>
	   <run>
		<include name="windows.*" />
	   </run>
       </groups>
       <classes>
	   <class name="com.concretepage.MyTestOne" />
	   <class name="com.concretepage.MyTestTwo" />
       </classes>
   </test>
</suite> 
Output
MyTestOne: divisionOnWindowsTest...
MyTestTwo: functionalOnWindowsTest...
Now configure regular expression as linux.* .
<groups>
  <run>
     <include name="linux.*" />
  </run>
</groups>  
Output
MyTestOne: divisionOnLinuxTest...
MyTestTwo: functionalOnLinuxTest...

3. Group of Groups

We can create group of groups that is called MetaGroups. In testng.xml file, we use <define> element to create groups of groups.
Suppose we have test methods associated with groups as username, eligibility, division and multiplication.
MyTestOne.java
public class MyTestOne {
  @Test(groups = {"division"})
  public void divisionTest() {
	System.out.println("MyTestOne: divisionTest...");
	assertEquals(30 / 5, 6);
  }
  @Test(groups = {"multiplication"})
  public void multiplicationTest() {
	System.out.println("MyTestOne: multiplicationTest...");
	assertEquals(10 * 4, 40);
  }
} 
MyTestTwo.java
public class MyTestTwo {
  @Test(groups = {"username"})
  public void userNameTest() {
	System.out.println("MyTestTwo: userNameTest...");
	String name = "Mohit";
	assertTrue(name.length() > 3);
  }
  @Test(groups = {"eligibility"})
  public void ageLimitTest() {
	System.out.println("MyTestTwo: ageLimitTest...");
	int age = 20;
	assertTrue(age > 18);
  }  
} 
In testng.xml file, we will create a group authentication that will include groups as username and eligibility. In the same way, we will create a group calculation that will include groups as division and multiplication. We will also create a group all that will include groups authentication and calculation.
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1">
  <test name="Regression1">
    <groups>
      <define name="authentication">
        <include name="username" />
        <include name="eligibility" />
      </define>
      <define name="calculation">
        <include name="division" />
        <include name="multiplication" />
      </define>
      <define name="all">
        <include name="authentication" />
        <include name="calculation" />
      </define>
      <run>
        <include name="all" />
      </run>
    </groups>
    <classes>
      <class name="com.concretepage.MyTestOne" />
      <class name="com.concretepage.MyTestTwo" />
    </classes>
  </test>
</suite> 
When we run the group all, we have following output.
MyTestOne: divisionTest...
MyTestOne: multiplicationTest...
MyTestTwo: ageLimitTest...
MyTestTwo: userNameTest... 
Now run the group authentication as below.
<run>
  <include name="authentication" />
</run> 
Output
MyTestTwo: ageLimitTest...
MyTestTwo: userNameTest... 
Now run the group calculation as below.
<run>
  <include name="calculation" />
</run> 
Output
MyTestOne: divisionTest...
MyTestOne: multiplicationTest... 

4. Exclude Groups

In TestNG, we can exclude groups in the same way as we include groups. The requirement to exclude groups can be when some test methods are breaking. We may wish to run other test methods excluding the broken test methods. In such cases, we need to associate all broken test methods with a group such as broken and exclude them using <exclude> element in testng.xml file.
Find the example.
MyTest.java
public class MyTest {
  @Test(groups = {"calculation", "broken"})
  public void divisionTest() {
	System.out.println("MyTest: divisionTest...");
	assertEquals(20 / 5, 4);
  }
  @Test(groups = {"calculation"})
  public void multiplicationTest() {
	System.out.println("MyTest: multiplicationTest...");
	assertEquals(10 * 5, 50);
  }
} 
testng.xml
<suite name="Suite1" verbose="1">
  <test name="Regression1">
    <groups>
      <run>
        <include name="calculation" />
        <exclude name="broken"/>
      </run>
    </groups>
    <classes>
      <class name="com.concretepage.MyTest" />
    </classes>
  </test>
</suite> 
Output
MyTest: multiplicationTest... 

5. Partial groups

We can associate test class methods with groups at class level that will be applied to all test methods of this class. We can also add other groups to test methods at method level.
MyTest.java
@Test(groups = { "calculation" })
public class MyTest {
  @Test(groups = {"functional"} )
  public void nameMatchTest() {
	System.out.println("MyTest: nameMatchTest...");
	String name = "Mahesh";
	assertTrue("Mahesh".equals(name));
  }  
  public void divisionTest() {
	System.out.println("MyTest: divisionTest...");
	assertEquals(20 / 5, 4);
  }
  public void multiplicationTest() {
	System.out.println("MyTest: multiplicationTest...");
	assertEquals(10 * 5, 50);
  }
} 
The test method nameMatchTest() is associated with groups calculation and functional. The test methods divisionTest() and multiplicationTest are associated with calculation group.
Find the testng.xml to test the code.
testng.xml
<suite name="Suite1" verbose="1">
  <test name="Regression1">
    <groups>
      <run>
        <include name="calculation" />
      </run>
    </groups>
    <classes>
      <class name="com.concretepage.MyTest" />
    </classes>
  </test>
</suite> 
For the group calculation, we have following output.
MyTest: divisionTest...
MyTest: multiplicationTest...
MyTest: nameMatchTest...  
Now use functional group.
<run>
  <include name="functional" />
</run> 
Output
MyTest: nameMatchTest... 

6. Reference

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us