testng TestNG Groups TestNG Groups configuration and basic example


Example

Groups can be configured under Suite and/or Test element of testng.xml. All groups which are marked as included in tesng.xml will be considered for execution, excluded one will be ignored. If a @Test method has multiple groups and from those groups if any single groups is excluded in testng.xml that @Test method will not run.

Below is the typical testng.xml configuration at Test level for running groups:

<suite name="Suite World">
<test name="Test Name">
  <groups>
    <run>
      <include name="functest" />
      <exclude name="regtest" />
    </run>
  </groups>
  <classes>
    <class name="example.group.GroupTest"/>
  </classes>
</test>
</suite>

And this how it's test class will look like:

package example.group;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class GroupTest {
    
    @BeforeClass
    public void deSetup(){
        //do configuration stuff here
    }
    
    @Test(groups = { "functest", "regtest" })
    public void testMethod1() {
    }

    @Test(groups = {"functest", "regtest"} )
    public void testMethod2() {
    }

    @Test(groups = { "functest" })
    public void testMethod3() {
    }
    
    @AfterClass
    public void cleanUp(){
        //do resource release and cleanup stuff here
    }
}

On running this GroupTest TestNG class only testMethod3() will be executed.

Explanation:

  • <include name="functest" /> all the test methods of functest group are eligible for run if it not excluded by any other group.
  • <exclude name="regtest" /> no test methods of regtest group are eligible for run.
  • testMethod1() and testMethod2() are in regtest group, so they will not have run.
  • testMethod3() is in regtest group, so it will run.