Groups in TestNG

In this section we will discuss on the Groups in TestNG. TestNG is an opensource Testing framework in which you can perform unit testing, Functional Testing integration testing etc. To Test multiple type of testing you need to group the Test case based on the type of Testing. TestNG support the grouping feature through which you can achieve the above scenario.

Let’s understand in detail.

Why you need to group the Test when you can run the Test based on Priority?

Sometime this is required to run only unit Testing Testcase and some other time only Functional Testcase. Also, you need to run both unit testing and functional Testing but need to exclude the Integration Testing.

One way to achieve this scenario is, make a different Test class file for each type of Testing.

The disadvantage of this approach is Code redundancy. If you will make different Test class for different Testing type then you may need to create same method, variable in each Test class. This leads to repetition of the code which is not a good approach to proceed.

Another way is, create a single test class and make group for each test based on the functional, unit and integration testing. Then configure  the testing.xml file suite test to include or exclude the Test whichever is required. If you want to run only functional testing group, then you need to include only functional Test Group. If you need to run both unit test and function test but nor integration test, then you need to include both Unit test and Functional Test and exclude Integration test.

See the below Example

Example:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestingLPoint">
<test name="testngGroup">
<groups>
<run>
<include name="UnitTest" />
<include name="PerformanceTest" />
<exclude name="sanityTest" />

</run>
</groups>
<classes>
<class name="testinglpoint.TestCase1" />
<class name="testinglpoint.TestCase2" />
<class name="testinglpoint.TestCase3" />

</classes>
</test>
</suite>
Testcase -1:
package testinglpoint;
import org.testng.annotations.Test;
public class TestCase1 {
	@Test (groups = { "UnitTest", "PerformanceTest" })
	public void loginTest(){
		System.out.println("Logged in successfully");
	}
}
Testcase -2:
package testinglpoint;
import org.testng.annotations.Test;
public class TestCase2 {
	@Test (groups = { "PerformanceTest" })
	public void composeMail(){
		System.out.println("Mail Sent");
	}
}
Testcase-3:
package testinglpoint;
import org.testng.annotations.Test;
public class TestCase2 {
	@Test (groups = { "sanityTest" })
	public void composeMail(){
		System.out.println("Mail Sent");
	}
}

Explanation:

In the example, we have included UnitTest and PerformanceTest group and excluded sanityTest group. So if we run the above code, then only Test case under smokeTest and functionalTest will be executed i.e. Testcase1 and Testcase2.

Now we have understood the importance of Groups in TestNG.

Leave a Reply

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