Thursday, August 22, 2019

TestNg Groups



One of the best features in TestNG is, it allows you to group the test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. Then TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set.  This gives you maximum flexibility in how you partition your tests and doesn’t require you to recompile anything if you want to run two different sets of tests back to back.

Example: In our  automation daily execution we will run few tests during sanity and more tests in functional or regression suite. In this case simply we can group the tests as “Sanity” which we run during Sanity testing and  remaining as “functional” or “regression” based on your requirement.

Groups are specified in your testng.xml file and can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.

Syntax:

@Test(groups = { "group name" })

Test Method 1:
       @Test(groups = { "smoke" })
       public void testMethod1() {

             System.out.println("Inside Smoke group");
       }


This test method we have grouped as “smoke”


Test Method 2:
       @Test(groups = { "functional" })
       public void testMethod2() {

             System.out.println("Inside Functional group");
       }
       

This test method we have grouped as “functional”


Test Method 3:
       @Test(groups = { "smoke", "functional" })
       public void testMethod3() {

             System.out.println("Inside Functional & Smoke group");
       }
       


This test method we have grouped as both “Smoke” & “functional”

Complete Test script:
package testCases;

import org.testng.annotations.Test;

public class TestCase_2 {

       @Test(groups = { "smoke" })
       public void testMethod1() {

             System.out.println("Inside Smoke group");
       }

       @Test(groups = { "functional" })
       public void testMethod2() {

             System.out.println("Inside Functional group");
       }

      
       @Test(groups = { "smoke", "functional" })
       public void testMethod3() {

             System.out.println("Inside Functional & Smoke group");
       }

}




testng.xml:

Below testing.xml file for to run only “Smoke” group:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite_1">
   <test name="Test">
  
   <groups>
     <run>
       <include name="smoke" />
     </run>
   </groups>
  
     <classes>
        <class name="testCases.TestCase_2"/>
     </classes>
   </test>
</suite>

Console Output:




TestNg Output:



Below testing.xml file for to run only “functional” group:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite_1">
   <test name="Test">
  
   <groups>
     <run>
       <include name="functional" />
     </run>
   </groups>
  
     <classes>
        <class name="testCases.TestCase_2"/>
     </classes>
   </test>
</suite>




Console Output:



TestNg Output:



Below testing.xml file for to run multiple groups. Here I have added “Smoke” and “functional” groups:



<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite_1">
   <test name="Test">
  
   <groups>
     <run>
     <include name="smoke" />
       <include name="functional" />
     </run>
   </groups>
  
     <classes>
        <class name="testCases.TestCase_2"/>
     </classes>
   </test>
</suite>
Console Output:



TestNg Output:



Groups Exclusion:

TestNG allows you to include groups as well as exclude them. You can ignore a group by using the <exclude> tag as shown below:

For example, it is quite usual to have tests that temporarily break because of a recent change, and you don’t have time to fix the breakage yet. However, you do want to have clean runs of your functional tests, so you need to deactivate these tests.

Below testing.xml is for exclude example. Here I have excluded “smoke” group:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite_1">
   <test name="Test">
  
   <groups>
     <run>
       <exclude name="smoke" />
       <include name="functional" />
     </run>
   </groups>
  
     <classes>
        <class name="testCases.TestCase_2"/>
     </classes>
   </test>
</suite>


Console Output:



TestNg Output:




No comments:

Post a Comment

If any suggestions or issue, please provide