TestNG Grouping Tests
In TestNG, we can group multiple test methods into a named group. You can execute a particular set of test methods belonging to a group or multiple groups. This feature allows the test methods to be segregated into different sections or modules. You can have a set of test that belong to sanity test whereas others may belong to regression tests. Apart from this you can also segregate the tests based on the functionalities or features that the test method verifies. This helps in executing only a particular set of tests as and when required.
We can assign single test to multiple groups i.e. multiple groups can contain the same test method in their group.
1.Test belongs to single group:
Below is the sample program:
import org.testng.annotations.Test; public class TestBelongsToSingleGroup { @Test(groups = {"sanity-group"}) public void testOne() { System.out.println("This test belongs to Sanity group"); } @Test public void testTwo() { System.out.println("This test NOT belongs to any group"); } @Test(groups = {"sanity-group"}) public void testThree() { System.out.println("This test belongs to Sanity group"); } }
Execute the above program using testng.xml file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="GroupSuite"> <test name="GroupTest"> <groups> <run> <include name="sanity-group"/> </run> </groups> <classes> <class name="TestBelongsToSingleGroup"/> </classes> </test> </suite>
Output for the above program is:
The above XML file contains only one test inside a suite. This contains the groups section defined by using the groups tag as shown in the code. The run tag represents the group that needs to be run. The include tag represents the name of the group that needs to be executed.
After executing the above xml file the given output had only two test methods result which are belongs to sanity-group and the one test is not executed which is not part of this group.
2.Test belongs to multiple groups:
We can assign a single test method to multiple groups. For example, you want to execute one script in sanity test and regression test. Then you can assign this test method to both the groups.
Below is the sample program:
import org.testng.annotations.Test; public class TestBlongsToMultipleGroups { @Test(groups = {"sanity-group"}) public void testOne() { System.out.println("This test belongs to sanity-group"); } @Test(groups = {"sanity-group","regression-group"}) public void testTwo() { System.out.println("This test belongs to sanity-group and regression-group"); } @Test(groups = {"regression-group"}) public void testThree() { System.out.println("This test belongs to regression-group"); } }
The above class contains three test methods. Two of the test methods belong to one group each, where as one of the methods belongs to two groups, sanity-group and regression-group respectively.
Execute the above program using testng.xml file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="GroupSuite"> <test name="GroupTest One"> <groups> <run> <include name="sanity-group"/> </run> </groups> <classes> <class name="TestBlongsToMultipleGroups"/> </classes> </test> <test name="GroupTest two"> <groups> <run> <include name="regression-group"/> </run> </groups> <classes> <class name="TestBlongsToMultipleGroups"/> </classes> </test> </suite>
The above testng XML suite contains two tests, each of them executing test methods belonging to a particular group.
Output for the above program is:
If we observe the output, testOne and testThree executes only once but the testTwo executes two times as this is belongs to two groups.
Including and Excluding groups:
TestNG also allows you to include and exclude certain groups from test execution. This helps in executing only a particular set of tests and excluding certain tests. A simple example can be when a feature is broken and you need to exclude a fixed set of tests from execution since these test will fail upon execution. Once the feature is fixed you can then verify the feature by just executing the respective group of tests.
Below is the sample program:
import org.testng.annotations.Test; public class IncludeExcludeGroup { @Test(groups={"include-group"}) public void testMethodOne() { System.out.println("Test method one belonging to a group."); } @Test(groups={"include-group"}) public void testMethodTwo() { System.out.println("Test method two belonging to a group."); } @Test(groups={"include-group","exclude-group"}) public void testMethodThree() { System.out.println("Test method three belonging to two groups."); } }
The above class contains three test methods that print a message onto console when executed. All the three methods belong to a group include-group whereas the testMethodThree method also belongs to the group exclude-group.
Execute the above program using testng.xml file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test"> <groups> <run> <include name = "include-group"/> <exclude name = "exclude-group"/> </run> </groups> <classes> <class name="IncludeExcludeGroup"/> </classes> </test> </suite>
The above XML contains a simple test in which the group include-group is included in the test using the include XML tag and the group exclude-group is being excluded from the test execution by using the exclude tag.
Output for the above program is:
From the test results TestNG executed two methods from the group include-group and excluded the third method that belonged to the group exclude-group, which was excluded from the test execution. If a test method belongs to both included and excluded group, the excluded group takes the priority and the test method will be excluded from the test execution. You can have as many include and exclude groups as you want while creating a test suite in TestNG.