index-1-1.jpg
  • Aurora Solutions

Groups In TestNG

In the previous article we learned about different annotations provided by TestNG and how to use them. In this article we will cover one of the most important concepts of TestNG, which is grouping of test methods.


In TestNG different test methods can be grouped together into a named group. Thus , Grouping feature allows the test method to be segregated into different section or modules. A test method can belong to single or multiple groups. For example a single test may belong to sanity test and regression test. Grouping helps to execute only a particular set of tests as and when required.


Let's create few tests that belong to a particular group:


Test that belong to a single group:

Sample Project: a. Open Eclipse. b. create a project MutiTest . c. In MultiTest create a package groupPackage. d. In groupPackage create a class SingleGroupTestClass.java


Add the following piece of code in SingleGroupTestClass.java:

public class SingleGroupTestClass {
 @Test(groups={"first-group"})
 public void methodOne(){
    System.out.println("Method one belonging to first-group.");
 }
 @Test
 public void methodTwo(){
    System.out.println("Method two not belonging to any group.");
 }
 @Test(groups={"first-group"})
 public void methodThree(){
    System.out.println("Method three belonging to first-group.");
 }
}

The preceding test class contains three test methods out of which two belong to a group named test-group. A test method can be assigned to first-group using the groups attribute while using the @Test annotation as shown.


Add the following piece of code in testing.xml file:

<suite name="test Suite" verbose="1">
    <test name="Group Test">
          <groups>
              <run>
                  <include name="first-group" />
              </run>
          </groups>
          <classes>
             <class name="SingleGroupTestClass" />
          </classes>
    </test>
</suite>

The preceding XML file contains only one test inside a suite. This contains the groups section defined by using the groups tag as shown in the code. The run tag represents the group that needs to be run. The include tag represents the name of the group that needs to be executed.


Output:

Method one belonging to first-group.
Method three belonging to first-group.

Test that belong to multiple groups Sample Project: a. Open Eclipse. b. create a project MutiTest . c. In MultiTest create a package groupPackage. d. In groupPackage create a class MultipleGroupTestClass.java


Add the following piece of code in MultipleGroupTestClass.java:

public class MultiGroup {
@Test(groups={"first-group"})
public void methodOne(){
System.out.println("Method one belonging to first-group.");
}

@Test(groups={"second-group"})
public void methodTwo(){
System.out.println("Method two belonging to second-group.");
}

@Test(groups={"first-group", "second-group"})
public void methodThree(){
System.out.println("Method three belonging to both groups.");
}
}

The preceding test class contains three test methods out of which two belong to a single group and one belong to multiple groups. A test method can be assigned to first-group or/and second-group using the groups attribute while using the @Test annotation as shown above.


Add the following piece of code in tetsing.xml file:

<suite name="test Suite" verbose="1">
    <test name="First Group Test">
        <groups>
            <run>
              <include name="first-group" />
            </run>
        </groups>
        <classes>
           <class name="MultipleGroupTestClass" />
        </classes>
    </test>
   <test name="Second Group Test">
       <groups>
          <run>
             <include name="second-group" />
           </run>
        </groups>
        <classes>
           <class name="MultipleGroupTestClass" />
        </classes>
   </test>
 </suite>

The preceding XML file contains two tests inside a suite. Each contains the groups section defined by using the groups tag as shown in the code. The run tag represents the group that needs to be run. The include tag represents the name of the group that needs to be executed.


Output:

Method one belonging to first-group.
Method three belonging to both groups.

Method two belonging to second-group.
Method three belonging to both groups.

In this article we have learn that how we can group together test methods into single and multiple groups allowing the test method to be segregated into different section or modules. I look forward to suggestions and questions in the comments section.

0 views0 comments

Recent Posts

See All