TestNG test groups and it’s usage are explained in detailed in this post.

TestNG test groups Usage:
  • Used for grouping of test methods
  • These groups allows you to segregate test methods into sets and then runs different set of test methods back to back without recompiling the code.
  • Specify group names in @Test annotation to make the test method as part of specific group.
  • @Test(groups ={"sanity","regression"})
    public void sample1()
    {
     ...
    }
    @Test(groups ={"regresssion"})
    public void sample2()
    {
     ...
    }
    
    • In the above code two types of test methods are available
    • Sanity test methods should be run when doing the sanity test after the build is deployed to make sure that no basic functionality is broken.
    • Regression test methods should be run to make sure all the scenarios are covered.
    • In the above test method “sample1” executes when invoked with group “sanity”.
    • Test methods “sample1” & “sample2” executes when invoked with group “regression”.
    • We can specify any number of group names on test method. That means a method can belong to multiple groups like “sample1” above.
  • Specify groups in testng.xml file <test> tag
  • <suite name="Sample Suite"> 
      <test name="Sample Test">
        <groups>
         <run>
          <include name="sanity"/>
         </run>
        </groups>
        <classes>
         <class name="..."/>
        </classes>
      </test>
    </suite>
    
    • When invoked the above testng.xml file, it will run method “sample1” only.
Groups with regular expression
  • Groups that are specified in testng.xml file supports regular expressions.
    • To run specific tests on Firefox browser and specific tests in IE browser. Below is the example
  • @Test(groups ={"firefox.sanity"})
        public void testWithFirefox()
        {
            System.out.println("From testWithFirefox....");
        }
    
        @Test(groups ={"ie.sanity"})
        public void testWithIE()
        {
            System.out.println("From testWithIE....");
        }
    
        @Test(groups ={"firefox.regression"})
        public void testWithFirefoxT00()
        {
            System.out.println("From testWithFirefoxT00....");
        }
    
  • Following is the xml file to run Firefox test methods
  • <suite name="Groups Sample" verbose="1">
        <test name="Groups" preserve-order="true">
            <groups>
                <run>
                    <include name="firefox.*"/>
                </run>
            </groups>
            <classes>
                <class name="...."/>
            </classes>
        </test>
    </suite>
    
  • Here “firefox.*” means it includes test methods of groups “firefox.sanity” & “firefox.regression”.
 Groups Exclusion
  • TestNG allows to exclude the groups.
  • Specify <exclude> tag with group name in <groups> tags as below
  • <groups>
      <run>
         <include name="regression"/>
         <exclude name="broken"/>
      </run>
    </groups>
    
  • Sometimes we can exclude the test methods in the execution process using exclude strategy in groups. This might occur when some tests hae to be fixed and some tests are getting failed in the execution process.
    • To do this, create a new group “broken” and then add to the methods to make these failed test methods belongs to it.
    • For example in the above test method “sample2” is broken, so to disable it, we can do like this.
    • @Test(groups ={"regresssion","broken"})
      public void sample2()
      {
       ...
      }
      
  • As discussed above, test methods “sample1” & “sample2” belongs to “regression” group.
  • But when executed the above xml file with exclude group, it will run only “sample1” test method.
Groups of Groups
  • Groups can include other Groups as well, these groups are called Meta Group
  • Following  is the complete class that has test methods with groups, you can use directly by just copying it.
  • package com.exploreselenium;
    
    import org.testng.annotations.Test;
    
    public class GroupsTest{
    
        @Test(groups ={"sanity","regression"})
        public void sample1()
        {
            System.out.println("From sample1....");
        }
    
        @Test(groups ={"regression"})
        public void sample2()
        {
            System.out.println("From sample2....");
        }
    
        @Test(groups ={"firefox.sanity"})
        public void testWithFirefox()
        {
            System.out.println("From testWithFirefox....");
        }
    
        @Test(groups ={"ie.sanity"})
        public void testWithIE()
        {
            System.out.println("From testWithIE....");
        }
    
        @Test(groups ={"firefox.regression"})
        public void testWithFirefoxT00()
        {
            System.out.println("From testWithFirefoxT00....");
        }
    
    }
    
  • You can define a group “all-sanity” that includes “sanity”,  “firefox.sanity” and “ie.sanity”. Below is the complete xml file named as “groups.xml“.
  • <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="Groups Sample" verbose="1">
        <test name="Groups" preserve-order="true">
            <groups>
                <define name="all-sanity">
                    <include name="sanity"/>
                    <include name=".*.sanity"/>
                </define>
                <run>
                    <include name="all-sanity"/>
                </run>
            </groups>
            <classes>
                <class name="com.exploreselenium.GroupsTest"/>
            </classes>
        </test>
    </suite>
    
  • Below is the result when executed groups.xml file.
  • [TestNG] Running:
      G:\ExploreSelenium\Config\Suites\groups.xml
    
    From sample1....
    From testWithFirefox....
    From testWithIE....
    
    ===============================================
    Groups Sample
    Total tests run: 3, Failures: 0, Skips: 0
    ===============================================
    
    • It executes the test methods “sample1”, “testWithFirefox” & “testWithIE”  as those are belonged to “sanity”, “firefox.sanity” & “ie.sanity” respectively.
Summary:

In this post, we learned Test Groups, how to partition tests for execution, how to include and exclude groups & how to use groups with in other groups.

TestNG Data Provider
Capture screenshot when TestNG Test method fails
0