The group test is a new innovative feature in TestNG, it doesn’t exist in Junit framework, it permits you dispatch methods into proper portions and preform sophisticated groupings of test methods. In TestNG, you can declare one method belong to one or more groups, even a certain set of groups can be included or excluded in groups.

How to do? – to use groupd = ‘group name’ in annotation test.

The following example has 4 test methods, they’re each included in group integration, the methods testingFeatureMethod1, testingFeatureMethod2 and testingFeatureMethod3 are included in group unit1. testingFeatureMethod2 is that just the one in group unit2.

package as.java;

import org.testng.annotations.Test;

public class TestNGTutorialGroupTest {

  @Test(groups={"unit1","integration"})
  public void testingFeatureMethod1() {
    System.out.println("testingFeatureMethod1");
  }

  @Test(groups={"unit2","integration"})
  public void testingFeatureMethod2() {
    System.out.println("testingFeatureMethod2");
  }

  @Test(groups={"unit1"})
  public void testingFeatureMethod3() {
    System.out.println("testingFeatureMethod3");
  }

  @Test(groups={"unit1, unit2"})
  public void testingFeatureMethod4() {
    System.out.println("testingFeatureMethod4");
  }
}

Invoking TestNG with:

<test name="Test1">
  <groups>
    <run>
      <include name="unit1"/>
    </run>
  </groups>
  <classes>
    <class name="as.java.TestNGTutorialGroupTest"/>
  </classes>
</test>

The output:

testingFeatureMethod1
testingFeatureMethod3
testingFeatureMethod4

Groups of Groups

Groups can also include other groups. These groups are called “MetaGroups”. For example, you might want to define a group “all” that includes “unit1″ and “unit2″. in below example, group all is defined in Test Suite XML files, and you can also match groups based on regular expressions to allow for platform specific customizations.

<test name="Testng1">
  <groups>
    <define name="all">
      <include name="unit1"/>
      <include name="unit2"/>
    </define>

    <run>
      <include name="all"/>
    </run>
  </groups>

  <classes>
    <class name="as.java.TestNGTutorialGroupTest"/>
  </classes>
</test>