Saturday, June 30, 2018

TestNG Groups with Example

Groups in TestNG:-

TestNG allows us to run test cases in grouping, It means using groups multiple test cases execution is possible.So this is provide flexibility to divide the test cases and run it.

For example if we have two sets of test cases, one is for Regression testing and second is for Smoke testing so we can divide it by using groups and run both types of testing back to back.

Steps:-

1.    First use groups attribute with @Test annotation. Syntax is:

                  @Test(groups = { “Regression” })

                Note: I have taken Regression just as example.

2.    Also mention <groups> tag in testing.xml file. Syntax is:

           <groups>
                 <run>
                    <include name=”Regression”/>
                </run>
           </groups>


So now run only those test cases which have Regression in @Test annotation.


Sample Code:

package blogPkg;

import org.testng.annotations.Test;

public class GroupsTest {

      @Test(groups = { "Regression" })
      public void groupsdemo() {

            System.out.println("This testcase is use for Regression Testing");

      }

      @Test(groups = { "Smoke" })
      public void groupsdemo1() {

            System.out.println("This testcase is use for Smoke Testing");

      }

      @Test(groups = { "Regression" })
      public void groupsdemo2() {

            System.out.println("This is the second testcase for Regression Testing");

      }

      @Test(groups = { "Smoke" })
      public void groupsdemo3() {

            System.out.println("This is the second testcase for Smoke Testing");

      }

      @Test()
      public void groupsdemo4() {

            System.out.println("This testcase without any grouping");

      }

}


Output:
This testcase is use for Regression Testing
This testcase second testcase for Regression Testing

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0


Note: If you want to run both types of test case then in testing.xml file mention both Regression and Smoke in include tag. Like


Syntax of testing.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
 
   <groups>                 
   <run>                      
    <include name="Regression" />
    <include name="Smoke" />                       
   </run>                     
   </groups>    
 
    <classes>
       <class name="blogPkg.GroupsTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->


You can see here as per our above sample code only groups of Regression test cases have run. So this is the good feature of TestNG . Any time if your team want to perform only i.e. Regression or Smoke testing then you can grouping it as per requirement and run it.


                        



                                                                        TestNG Annotations with Example




No comments:

Post a Comment