How to create Test Group in TestNG

TestNG Groups permit you to perform groupings of various test cases. In this section, we are going to Understand How we can Merge Multiple Testcases into Certain Groups and How to execute them accordingly.

It gives you great adaptability by dividing your test cases into Groups and the <groups> tag is Declared inside the <suite>tag in Testng.xml File, Before Going ahead You can  Learn How to Create Testng.xml File

and Using  the <include>and <exclude>tags in TestNG.xml we can include and Exclude Testcases in TestNG Groups.

Test Group class in TestNG

package Grouping;

import org.testng.annotations.Test;

public class Group_case {
    @Test (groups="GroupOne")//Groupone is define here
     public void TestOne() {
          System.out.println("This is TestOne ");
      }
     @Test (groups="GroupTwo")//GroupTwo is define here
     public void TestTwo() {
          System.out.println("This is TestTwo ");
      }
     @Test (groups="GroupTwo")//GroupTwo is define here
     public void TestThree() {
          System.out.println("This is TestThree ");
      }
     @Test (groups={"GroupTwo","GroupThree"})//GroupTwo and GroupThree is defined here and this would be Executed Based the condition we specified in xml file
     public void TestFour() {
          System.out.println("This is TestFour ");
      }
}

TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Grouping Testcase in TestNg">
    <!--This is the name of the suite-->
    <test thread-count="1" name="Grouping">
        <!--This is the name for the Positive Testcase-->
        <groups>
            <run>
                <include name="GroupOne"></include>
                <include name="GroupTwo"></include>
            </run>
        </groups>
        <classes>
            <class name="Grouping.Group_case"></class>
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

From The Above Class and TestNg.Xml file, In-class We have Defined Three Groups named GroupOne, GroupTwo, GroupThree Respectively, also Group two and GroupThree are assigned to the Fourth Testcase in class

using the TestNG.xml File We are calling the Groups by Include tag and all the Testcase Associated with that group Would be Executed, which in this case is Groupon and GroupTwo.

<classes> <class name="Grouping.Group_case"></class> </classes>

Using the Class tag the association with class is created in TestNg.xml File where the classes name Defined in the class name is a combination of the Package name and class name in which Grouping is the name of Package and Group_case is the name of the class.

Output

Test Scenario

  • Demonstrate the Function of <Exclude>tag.

Using the Exclude Tag We can ignore the Group which we don’t Require during execution.

Code

Group_case class

Group_case class
package Grouping;

import org.testng.annotations.Test;

public class Group_case {
    @Test (groups="GroupOne")//Groupone is define here
     public void TestOne() {
          System.out.println("This is TestOne ");
      }
     @Test (groups="GroupTwo")//GroupTwo is define here
     public void TestTwo() {
          System.out.println("This is TestTwo ");
      }
     @Test (groups="GroupTwo")//GroupTwo is define here
     public void TestThree() {
          System.out.println("This is TestThree ");
      }
     @Test (groups={"GroupTwo","GroupThree"})//GroupTwo and GroupThree is defined here and this would be Executed Based the condition we specified in xml file
     public void TestFour() {
          System.out.println("This is TestFour ");
      }
}

In the above class, we have Defined two Groups for the Fourth Testcase which are GroupTwo and GroupThree.

TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Grouping Testcase in TestNg">
    <!--This is the name of the suite-->
    <test thread-count="1" name="Grouping">
        <!--This is the name for the Positive Testcase-->
        <groups>
            <run>
                <include name="GroupOne"></include>
                <include name="GroupTwo"></include>
                <exclude name="GroupThree"></exclude>
            </run>
        </groups>
        <classes>
            <class name="Grouping.Group_case"></class>
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

In the above example, we have Excluded the  GroupThree so the Associated Testcase Four will not be Executed.

OutPut

We Successfully Demonstrated the Testcase Grouping in TestNg Using the Include and Exclude Tags.