TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

TestNG Groups

TestNG Groups with Testng Tutorial, Introduction, Testng Installation, Features of Testng, Suite Test, Testng Exception Test, Group Test, Plug with Ant, Plug with Eclipse, etc.

<< Back to TESTNG

TestNG Groups

TestNG Groups allow you to perform groupings of different test methods. Grouping of test methods is required when you want to access the test methods of different classes.

Not only you can declare the methods within a specified group, you can also declare another group within a specified group. Thus, TestNG can be asked to include a certain set of groups while excluding another set of groups.

It provides you maximum flexibility by partitioning your test methods in groups and does not require recompilation of test cases if you run your two different sets of test cases back to back.

Groups are specified in the testng.xml file with <groups> tag. Groups can be specified either in the <suite> tag or <test> tag. If the <groups> tag is specified inside the <suite> tag, then it is applied to all the <test> tags of XML file. If the <groups> tag is specified within a particular <test> folder, then it is applied to that particular <test> tag only.

Let's understand the concept of TestNG Groups through an example:

Test cases within Groups

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

Step 1: Open the Eclipse.

Step 2: We create three java projects, i.e., Personal_loan.java, Home_loan.java, and Car_loan.java.

Personal_loan.java

package com.TheDeveloperBlog;
import org.testng.annotations.Test;
public class Personal_loan
{
 @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");
 }
}

Home_loan.java

package com.TheDeveloperBlog;
import org.testng.annotations.Test;
public class Home_loan 
{
@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");
}
}

Car_loan.java

package com.TheDeveloperBlog;
import org.testng.annotations.Test;
public class Car_loan 
{
@Test
public void WebLoginCarLoan()
{
System.out.println("Web Login Home Loan");
}
@Test
public void MobileLoginCarLoan()
{
System.out.println("Mobile Login Home Loan");
}
@Test(groups= {"SmokeTest"})
public void APILoginCarLoan()
{
System.out.println("API Login Home Loan");
}
}

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

Step 3: Now, we create a testng.xml file where we configure the classes that we have created and add new tag <groups>. We want to execute those test cases which have a group "SmokeTest".

<?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.TheDeveloperBlog.Personal_loan"/>
</classes>
</test> <!-- Test -->
<test name="Home Loan">
<classes>
<class name="com.TheDeveloperBlog.Home_loan"/>
</classes>
</test> <!-- Test -->
<test name="Car Loan">
<classes>
<class name="com.TheDeveloperBlog.Car_loan"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Output

TestNG Groups

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

The testng.xml would look 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.TheDeveloperBlog.Personal_loan"/>
 <class name="com.TheDeveloperBlog.Home_loan"/>
  <class name="com.TheDeveloperBlog.Car_loan"/>
  </classes>
  </test> <!-- Test -->
 </suite> <!-- Suite -->

Tests belonging to multiple Groups

Step 1: Open the Eclipse.

Step 2: We create a java project named as "Groups.java".

Groups.java

package com.TheDeveloperBlog;
import org.testng.annotations.Test;
public class Groups {
@Test(groups= {"Group A"})
public void testcase1() 
{
System.out.println("Test case belonging to Group A");
}
@Test(groups= {"Group A","Group B"})
public void testcase2() 
{
System.out.println("Test case belonging to both Group A and Group B");
}
@Test(groups= {"Group B"})
public void testcase3() 
{
System.out.println("Test case belonging to Group B");
}
}

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

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

testng.xml file

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

Step 4: Run the testng.xml file by clicking right click on the testng.xml file.

Output

TestNG Groups

Including/Excluding Groups

Step 1: Open the Eclipse.

Step 2: We create a new java project.

Groups.java

package com.TheDeveloperBlog;
import org.testng.annotations.Test;
public class Groups 
{
@Test(groups= {"Include Group"})
public void test_case1() 
{
System.out.println("This is test case 1");
}
@Test(groups= {"Include Group"})
public void test_case2() 
{
System.out.println("This is test case 2");
}
@Test(groups= {"Exclude Group"})

public void test_case3() 
{
System.out.println("This is test case 3");
}
}

Step 3: We will create the testng.xml file.

testng.xml file

<?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.TheDeveloperBlog.Groups"/>
</classes>
 </test> <!-- Test -->
 </suite> <!-- Suite -->

Step 4: Run the testng.xml file.

Output

TestNG Groups

Using Regular Expressions

We can also use regular expressions with TestNG Groups.

Let's understand through an example:

Step 1: Open the Eclipse.

Step 2: We create a java project named as "Regular_Expression.java".

Regular_Expression.java

package com.TheDeveloperBlog;
import org.testng.annotations.Test;
public class Regular_Expression {
@Test(groups= {"Include test case1"})
public void test_case1() 
{
System.out.println("This is test case 1");
}
@Test(groups= {"Include test case2"})
public void test_case2() 
{
System.out.println("This is test case 2");
}
@Test(groups= {"Exclude test case3"})
public void test_case3() 
{
System.out.println("This is test case 3");
}
}

Step 3: Now we create the testng.xml file to configure the above 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.TheDeveloperBlog.Regular_Expression"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Step 4: Run the testng.xml file.

Output

TestNG Groups

Groups in Groups

We can also specify a group within another group. The groups which are defined in another groups are known as Meta Groups.

Let's understand through an example:

Step 1: Open the Eclipse.

Step 2: We create a java project named as "Groups_in_Groups".

Groups_in_Groups.java

package com.TheDeveloperBlog;
import org.testng.annotations.Test;
public class Groups_in_Groups 
{
 @Test(groups= {"Smoke"})
 public void test1()
 {
	 System.out.println("test1");
 }
 @Test(groups= {"Regression"})
 public void test2()
 {
	 System.out.println("test2");
 }
 @Test
 public void test3()
 {
	 System.out.println("test3");
 }}

Step 3: Now we create a testng.xml file where we configure the above class.

testng.xml file

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

In the above xml file, we define a new group within another group named as "Group 1" and we have include those test cases which are tagged with "Smoke" and "Regression".

Step 4: Run the testng.xml file.

Output

TestNG Groups
Next TopicTestNG Annotations




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf