All IT Courses 50% Off
Selenium Tutorials

TestNG Groups

Include, Exclude in Selenium

In this tutorial we will learn 

  1. TestNG Groups
  2. TestNG Groups Syntax
  3. How to use Regular Expression with TestNG Groups

TestNG Groups

The similar type of methods is an important feature in TestNG grouping. But using this feature, multiple methods can be mentioned and assigned into a named group by a user. You can also execute those test methods which belong from a group or multiple groups. This TestNG group feature helps us to separate the test methods into different modules or sections. Like we tested of various types on an application like regression, sanity, integration, etc. So by using the grouping feature, we can segregate the test methods based on their features or functionalities that the test method verifies. It helps us to execute a particular set of tests when required which are more productive. TestNG groups will provide the maximum flexibility by partitioning your test methods in groups which does not require any recompilation of test cases if you run back to back two different sets of test cases.

Grouping in TestNG

  • Like alwaysRun and ignoreMissingDependencies “Groups” is another attribute in TestNG. These TestNG Groups attribute are seen at the metod and class level. By using this attribute, you can specify to which group or groups this method or class belongs to. 
  • “Groups” takes the values as a string array, so that you can mention multiple group name at a time.
  • By using textng.xml during execution time, you can decide which group should execute by using include and which groups should not execute by using exclude tags. It will run all groups if you won’t mention any groups.

TestNG Groups Syntax

Using below syntax, we can mention a group name to a method or a class. 

All IT Courses 50% Off

For Single Group: groups= {“groupname”}

For Multiple Group: groups= {“groupname1”, “groupname2”}

Let us consider a simple program

First case: When <groups> tag is defined inside the <suite> tag.

Step 1: Launch the Eclipse

Step 2: We create three java projects, i.e. HouseLoan.java, CarLoan.java, PersonalLoan.java

PerosnalLoan.java

PersonalLoan.PNG

package com.testing;

import org.testng.annotations.Test;

public class PersonalLoan {

@Test(groups= {"SmokeTest"})  
public void WebLoginPersonalLoan()  
{  
    System.out.println("Web Login Personal Loan");  
}  
@Test  
public void MobileLoginPersonalLoan()  
{  
    System.out.println("Mobile Login Personal Loan");  
}  
@Test  
public void APILoginPersonalLoan()  
{  
    System.out.println("API Login Personal Loan");  
} 

}

HouseLoan.java

HouseLoan.PNG

package com.testing;

import org.testng.annotations.Test;

public class HomeLoan {
@Test  
public void WebLoginHomeLoan()  
{  
  System.out.println("Web Login Home Loan");  
}  
@Test(groups= {"SmokeTest"})  
public void MobileLoginHomeLoan()  
{  
  System.out.println("Mobile Login Home Loan");  
}  
@Test  
public void APILoginHomeLoan()  
{  
  System.out.println("API Login Home Loan");  
} 


}

CarLoan.java

CarLoan.PNG

package com.testing;

import org.testng.annotations.Test;

public class HomeLoan {
@Test  
public void WebLoginHomeLoan()  
{  
  System.out.println("Web Login Home Loan");  
}  
@Test(groups= {"SmokeTest"})  
public void MobileLoginHomeLoan()  
{  
  System.out.println("Mobile Login Home Loan");  
}  
@Test  
public void APILoginHomeLoan()  
{  
  System.out.println("API Login Home Loan");  
} 


}

In the above test case, we provide SmokeTest as group name to three cases of three different classes.

Step 3: Now, we create a textng.xml file where we configure the classes and add to the new tag <groups>. We will execute those test cases which have “SmokeTest” as a group.Right-click on Project folder > New and select ‘File

textngxml.PNG

In New file wizard, give file name as ‘testng.xml‘ and click on Finish button.

testng.PNG

It will add testng.xml file under the project folder

 Now add the below given code in your testng xml file.

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
<suite name="test_suite">  
<groups>  
<run>  
<include name="SmokeTest"/>  
</run>  
</groups>  
<test name="Personal Loan">  
<classes>  
<class name="com.testing.PersonalLoan"/>  
</classes>  
</test> <!-- Test -->  
<test name="Home Loan">  
<classes>  
<class name="com.testing.HomeLoan"/>  
</classes>  
</test> <!-- Test -->  
<test name="Car Loan">  
<classes>  
<class name="com.testing.CarLoan"/>  
</classes>  
</test> <!-- Test -->  
</suite> <!-- Suite -->

Right-click on testng.xml > Select Run As > Click TestNG Suite

runas.PNG

Output:

console.PNG

Second Case: When <groups> tag defined inside the tag.

The testng.xml file looks like

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
<suite name="test_suite">  
<test name="Loan">  
<groups>  
<run>  
<include name="SmokeTest"/>  
</run>  
</groups>  
 <classes>  
 <class name="com.testing.PersonalLoan"/>  
 <class name="com.testing.HomeLoan"/>  
  <class name="com.testing.CarLoan"/>  
  </classes>  
  </test> <!-- Test -->  
 </suite> <!-- Suite --> 

Tests belonging to Multiple Groups

Step 1: Launch the Eclipse

Step 2: Create a Project Name as “Groups.java”

package com.testing;

import org.testng.annotations.Test;

public class Groups {
@Test(groups= {"Group X"})  
public void testcase1()   
{  
System.out.println("Test case belonging to Group X");  
}  
@Test(groups= {"Group X","Group Y "})  
public void testcase2()   
{  
System.out.println("Test case belonging to both Group X and Group Y");  
}  
@Test(groups= {"Group Y"})  
public void testcase3()   
{  
System.out.println("Test case belonging to Group Y");  
} 

}

In the above code, we define two groups, i.e., Group X and Group Y. The testcase1() is tagged with a ‘Group X, testcase2 is tagged with two groups ‘Group X and ‘Group Y, and testcase3() is tagged with a ‘Group Y.

Step 3:  Create the testng.xml file to configure the Groups class.

Testng.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
<suite name="test_suite">  
<test name="Group X">  
<groups>  
<run>  
<include name="Group X"/>  
</run>  
</groups>  
<classes>  
<class name="com.testing.Groups"/>  
</classes>  
</test> <!-- Test -->  
<test name="Group Y">  
<groups>  
<run>  
<include name="Group Y"/>  
</run>  
</groups>  
<classes>  
<class name="com.testing.Groups"/>  
</classes>  
</test> <!-- Test -->  
</suite> <!-- Suite --> 

Step 4: Right-click on testng.xml > Select Run As > Click TestNG Suite

Output:

newtestng.PNG

Including and Excluding Groups

Step 1: Launch the Eclipse

Step 2: Create a new project as TestGroups.java

package com.testing;

import org.testng.annotations.Test;

public class TestGroups {
@Test(groups= {"Include Group"})  
public void testcase1()   
{  
System.out.println("This is test case 1");  
}  
@Test(groups= {"Include Group"})  
public void testcase2()   
{  
System.out.println("This is test case 2");  
}  
@Test(groups= {"Exclude Group"})  
  
public void testcase3()   
{  
System.out.println("This is test case 3");  
} 
}

Step 3:  Create the testng.xml file to configure the TestGroups class.

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
<suite name="test_suite">  
<test name="Include and Exclude Group">  
<groups>  
<run>  
<include name="Include Group"/>  
<exclude name="Exclude Group"/>  
</run>  
</groups>  
<classes>  
<class name="com.testing.TestGroups"/>  
</classes>  
 </test> <!-- Test -->  
 </suite> <!-- Suite --> 

Step 4: Right-click on testng.xml > Select Run As > Click TestNG Suite

Output:

testng3.PNG

Using Regular Expression with TestNG Groups

With the help of regular expression in TestNG, we can represent more TestNG Groups names through single statement. We can easily use the regular expression to decrease the complexity also by using some prefix or suffix with the group name.

Step 1: Launch the Eclipse

Step 2: Create a new project as “GroupRegularExpression.java”

GroupRegularExpression.java

package com.testing;

import org.testng.annotations.Test;

public class GroupRegularExpression {
  
@Test(groups= {"Include test case1"})  
public void testcase1()   
{  
System.out.println("This is test case 1");  
}  
@Test(groups= {"Include test case2"})  
public void testcase2()   
{  
System.out.println("This is test case 2");  
}  
@Test(groups= {"Exclude test case3"})  
public void testcase3()   
{  
System.out.println("This is test case 3");  
}  
}

Step 3:  Create the testng.xml file to configure the GroupRegularExpression class.

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
<suite name="test_suite">  
<test name="Including test cases">  
<groups>  
<run>  
<include name="Include.*"/>  
</run>  
</groups>  
<classes>  
<class name="com.testing.GroupRegularExpression"/>  
</classes>  
</test> <!-- Test -->  
</suite> <!-- Suite --> 

Step 4: Right-click on testng.xml > Select Run As > Click TestNG Suite

Output:

testng4.PNG

Note:

  • TestNG uses regular expressions and not wildcats. For example, “anything” is matched by.”*”(dot star) not as “*”.
  • With TestNG xml you can’t use regex in the attribute class name. Regex is used to include exclude tags and package tags.
  • Regex is case sensitive. For example “Smoke” and “smoke” both are not same.

Groups in Groups

In TestNG you can also specify a group within another group. The groups which are defined in another groups is called Meta Groups.

Step 1: Launch the Eclipse

Step 2: Create a new project as “GroupsInGroups.java”

GroupsInGroups.java

package com.testing;

import org.testng.annotations.Test;

public class GropusInGroups {
@Test(groups= {"Smoke"})  
public void testgroup1()  
{  
    System.out.println("testgroup1");  
}  
@Test(groups= {"Regression"})  
public void testgroup2()  
{  
    System.out.println("testgroup2");  
}  
@Test  
public void testgroup3()  
{  
    System.out.println("testgroup3");  
}

}

Step 3:  Create the testng.xml file to configure the GroupsInGroups class.

Testng.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
<suite name="test_suite">  
<test name="GroupsInGroups">  
<groups>  
<define name="Group 1">  
<include name="Smoke"/>  
<include name="Regression"/>  
</define>  
<run>  
<include name="Group 1"/>  
</run>  
</groups>  
<classes>  
<class name="com.testing.GroupsInGroups"/>  
</classes>  
</test> <!-- Test -->  
</suite> <!-- Suite -->

In above xml file, we define a new group named “Group Test” within another group and include those test cases which are tagged with “Smoke” and “Regression”.

Step 4: Right-click on testng.xml > Select Run As > Click TestNG Suite

Output:

group1.PNG

Include and Exclude Test Cases

In TestNG we have a feature of enabling and disabling the test cases. From getting executed we need to disable a set of test cases.

In TestNG, we can disable the test cases in two ways:

  1. You can disable the test case in @ Test annotation
  2. You can disable the test case in XML file.

TestNG @Test enable parameter

By using @Test annotation and assigning “False” value to the enable attribute you can disable the test cases from execution.

Let’s consider below example to understand the above scenario

package enable;

import org.testng.annotations.Test;

public class TestEnable {
@Test     
// First test case
    public void test()                                            
    {  
        System.out.println("Software Testing Tutorials!!");  
    }  

      
    @Test(enabled=false)  
 // Second test case 
    public void testing()                                           
    {  
        System.out.println("Automation Testing");  
    }

}

In the above code, we created two test cases, i.e., test() and testing(), and we disable the second test case by assigning the False value to the enable attribute. On disabling the second test case, the only first test case will run.

When you run the above code it will display the output as

enable.PNG

In the above output, we observe that only the first test case run without any failure.

Disable the enable attribute in the XML file.

Step 1: Launch the Eclipse

Step 2: Create a new project as “testing1.java”

testing1.java

package enable;

import org.testng.annotations.Test;

public class testing1 {
@Test  
    public void WebLoginPersonalLoan()  
    {  
        System.out.println("Web Login Personal Loan");  
    }  
      
    @Test  
    public void MobileLoginPersonalLoan()  
    {  
        System.out.println("Mobile Login Personal Loan");  
    }

}

testing2.java

package enable;

import org.testng.annotations.Test;

public class testing2 {
@Test  
public void WebLoginCarLoan()  
{  
System.out.println("Web Login Car Loan");  
}  
@Test  
public void MobileLoginCarLoan()  
{  
System.out.println("Mobile Login Car Loan");  
}  
@Test  
public void APILoginCarLoan()  
{  
System.out.println("API Login Car Loan");  
} 


}

In above examples testing1.java contains the test cases of Personal loan while testing2.java contains the test cases of a car loan.

Step 3: If we want to disable the MobileLoginPersonlaLoan test case in personal loan module, then we need to add the <method> tag in xml file which has access to all the methods of a class.

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
<suite name="loan_department">  
  <test name="Personal_loan">  
  <classes>  
  <class name="enable.testing1"/>  
  </classes>  
  </test> <!-- Test -->  
  <test name="Car_loan">  
  <classes>  
  <class name="enable.testing2">  
  <methods>  
  <exclude name = "MobileLoginCarLoan"/>  
  </methods>  
  </class>  
  </classes>  
  </test>  
</suite> <!-- Suite --> 

Step 4: Right click on the testng.xml file > Run As and then click on the TestNG Suite.

output.PNG

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button