Thursday, June 19, 2014

Test Case Grouping

Groups‘ is one more annotation of TestNG which can be used in the execution of multiple tests. Let’s say you have hundred tests of class vehicle and in it ten method of car, ten method of scooter and so on. You probably like to run all the scooter tests together in a batch. And you want all to be in a single test suite. With the help of grouping you can easily overcome this situation.

Steps to follow:
1.      Create two methods for Car, two methods for Scooter and one method in conjunction with Car & Sedan Car.
2.      Group them separately with using (groups = { ” Group Name” })
package automationFramework;

import org.testng.annotations.Test;

public class Grouping {
  @Test (groups = { "Car" })
  public void Car1() {
              System.out.println("Batch Car - Test car 1");
  }
  @Test (groups = { "Car" })
  public void Car2() {
              System.out.println("Batch Car - Test car 2");
  }
  @Test (groups = { "Scooter" })
  public void Scooter1() {
              System.out.println("Batch Scooter - Test scooter 1");
  }
  @Test (groups = { "Scooter" })
  public void Scooter2() {
              System.out.println("Batch Scooter - Test scooter 2");
  }
  @Test (groups = { "Car", "Sedan Car" })
  public void Sedan1() {
              System.out.println("Batch Sedan Car - Test sedan 1");
  }
}

3. Create a testng xml like this:

<suite name="Suite">
    <test name="Practice Grouping">
        <groups>
          <run>
                  <include name="Car" />
          </run>
      </groups>
      <classes>
          <class name="automationFramework.Grouping" />
      </classes>
    </test>
</suite>

4. Run the test by right click on the testng.xml file and select Run As > TestNG Suite.

Note: We have just call the group ‘Car’ from the xml and it also executed the test for Sedan Car, as we have mentioned the ‘Car’ as well while declaring the group of Sedan Car.
Clubbing of groups is also possible, take a look at the below xml:
<suite name="Suite">
   <test name="Practice Grouping">
      <groups>
         <define name="All">
                               <include name="Car"/>
                               <include name="Scooter"/>
       </define>
                  <run>
                               <include name="All"/>
                  </run>
       </groups>
       <classes>
            <class name="automationFramework.Grouping" />
      </classes>
   </test>
</suite>


You can see that we have created a new Group with the name ‘All’ and include other groups in it. Then simply called the newly created group for execution.

No comments:

Post a Comment