TestNG @Test attributes - method description and grouping

description and droups

In framework and with growing number of automated test cases, it's hard to maintain what test cases need to be executed for functional, regression or sanity etc, 
To avoid this, TestNG came up with nice concept called grouping (aka groups), where we can provide the group names in each @Test method and can run the respective grouping testNG.xml to run only required test cases.

To understand this, refer to the below code output

package testng1;
import org.testng.annotations.Test;

public class TestNGAnnGroups {
      @Test(description="This is testcase1 desc", groups={"funct", "sanity"})
      public void test1()
      {
 System.out.println("This is method: test1");
      }
 
      @Test(description="This is testcase2 desc", groups={"funct"})
      public void test2()
      {
 System.out.println("This is method: test2");
      }
 
      @Test(description="This is testcase3 desc", groups={"sanity"})
      public void test3()
      {
 System.out.println("This is method: test3");
      }
}

we can assign one or more group names to each of the @Test methods, as shown above

and the testng.xml looks like




<suite name="Suite" parallel="none"> 
<test name="Test"> 
   <groups> 
      <run> 
          <include name="sanity"/>
          < exclude name="funct"/>
    </run>
    </groups>
    <classes>
      <class name="testng1.TestNGAnnGroups"/>
    </classes>
  </test>
</suite>

OutPut:

This is method: test3
Only 3rd method executed, as we included "sanity" but excluded "funct", 1st method din't execute

Let's change a bit in the groups tag of testng.xml,
 <groups>
     <run>
      <include name="sanity"/>
     </run>
  </groups>

OutPut:
This is method: test1
This is method: test3
Both 1st and 3rd method executed.

No comments:

Post a Comment