testng TestNG Groups TestNG MetaGroups - Groups of groups


Example

TestNG allows defining groups which can include other groups. MetaGroups logically combine one or more group(s) and control the execution of the @Testmethods belonging to those groups.

In below example there are various @Test methods belonging to different group(s). Few are specific to particular stack and few are regression and acceptance tests. Here MetaGroups can be created. Let's pick any two simple MetaGroups:

  1. allstack - includes both liux.jboss.oracle and aix.was.db2 groups and enables all test methods belonging to any one of those groups to run together.
  2. systemtest - includes allstack, regression and acceptance groups and enables all test methods belonging to any one of those groups to run together.

testng.xml configuration

<suite name="Groups of Groups">
    <test name="MetaGroups Test">
        <groups>
            <!-- allstack group includes both liux.jboss.oracle and aix.was.db2 groups -->
            <define name="allstack">
                <include name="liux.jboss.oracle" />
                <include name="aix.was.db2" />
            </define>

            <!-- systemtest group includes all groups allstack, regression and acceptance -->
            <define name="systemtest">
                <include name="allstack" />
                <include name="regression" />
                <include name="acceptance" />
            </define>

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

        <classes>
            <class name="example.group.MetaGroupsTest" />
        </classes>
    </test>

</suite>

MetaGroupsTest class

package example.group;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class MetaGroupsTest {

    @BeforeMethod
    public void beforeMethod(){
        //before method stuffs - setup
    }

    @Test(groups = { "liux.jboss.oracle", "acceptance" })
    public void testOnLinuxJbossOracleStack() {
        //your test logic goes here
    }

    @Test(groups = {"aix.was.db2", "regression"} )
    public void testOnAixWasDb2Stack() {
        //your test logic goes here
    }

    @Test(groups = "acceptance")
    public void testAcceptance() {
        //your test logic goes here
    }

    @Test(groups = "regression")
    public void testRegression(){
        //your test logic goes here
    }

    @AfterMethod
    public void afterMthod(){
        //after method stuffs - cleanup
    }
}