07. TestNG - Grouping

TestNG - Grouping
Groups‘ Can be used in the execution of multiple tests. Let’s say you have hundred tests of class Sports and in it ten method of Cricket, ten method of Chess and so on. You probably like to run all the Cricket 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.

How to do it…
1) Create two methods for Cricket, two methods for Hocky and one method in conjunction with Cricket & Chess.
2) Group them separately with using  (groups = { ” Group Name” })
package Others;
import org.testng.annotations.Test;
public class TestNG_Annotations {

  @Test (groups = { "Cricket" })
  public void Car1() {
              System.out.println("Cricket - Test Cricket 1");
  }

  @Test (groups = { "Cricket" })
  public void Car2() {
              System.out.println("Cricket - Test Cricket 2");
  }

  @Test (groups = { "Hocky" })
  public void Scooter1() {
              System.out.println("Hocky - Test Hocky 1");
  }

  @Test (groups = { "Hocky" })
  public void Scooter2() {
              System.out.println("Hocky - Test Hocky 2");
  }

  @Test (groups = { "Cricket", "Chess" })
  public void Sedan1() {
              System.out.println("Chess - Test Chess 1");
  }

}
Testng.xml:
<suite name="Suite">
    <test name="Practice Grouping">
        <groups>
                                     <run>
                                                <include name="Cricket" />
                                     </run>
                        </groups>
            <classes>
                <class name="Others.TestNG_Annotations" />
            </classes>
    </test>
</suite>

Note: We have just call the group ‘Cricket’ from the xml and it also executed the test for Chess, as we have mentioned the ‘Cricket’ as well while declaring the group of Chess.
Output:
Cricket - Test Cricket 1
Cricket - Test Cricket 2
Chess - Test Chess 1
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="Cricket"/>
                      <include name="Hocky"/>
               </define>
              <run>
                      <include name="All"/>
              </run>
        </groups>
        <classes>
             <class name="Others.TestNG_Annotations" />
       </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. The output will be like this:
Output:
Cricket - Test Cricket 1
Cricket - Test Cricket 2
Hocky - Test Hocky 1
Hocky - Test Hocky 2
Chess - Test Chess 1




No comments:

Post a Comment