Groups in TestNG

TestNG allows us to perform sophisticated groupings of test methods. Using TestNG we can execute only set of groups while excluding another set. This gives us the maximum flexibility in divide tests and doesn't require us to recompile anything if you want to run two different sets of tests back to back.

Groups are specified in testng.xml file and can be used either under the or tag. Groups specified in the tag apply to all the tags underneath.
import org.testng.annotations.Test;
public class groupExample {
@Test(groups="ReTesting")
public void testCaseOne()
{
System.out.println("Im in testCaseOne - And in ReTesting Group");
}
@Test(groups="ReTesting")
public void testCaseTwo(){
System.out.println("Im in testCaseTwo - And in ReTesting Group");
}
@Test(groups="Smoke Test")
public void testCaseThree(){
System.out.println("Im in testCaseThree - And in Smoke Test Group");
}
@Test(groups="Regression")
public void testCaseFour(){
System.out.println("Im in testCaseFour - And in Regression Group");
}
}

The below is the XML file to execute, the test methods with group.


We will execute the group “ReTesting” which will execute the test methods which are defined with group as “Regression”

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Sample Suite">
  <test name="testing">
  <groups>
      <run>
        <include name="Regression"/>
      </run>
    </groups>
    <classes>
       <class name="com.testng.group.groupExample" />
    </classes>
  </test>
</suite>