TestNG Groups Attribute: In TestNg grouping, similar types of methods is an important feature. But using this feature, a user can be mentioned and assigned multiple methods into a named group. You can also execute those test methods which may belong from a group or multiple groups.
This feature helps us to separate the test methods into different sections or modules. Like we have done various types of testing on an application like sanity, integration, regression testing, etc. So by using the grouping feature, we can segregate test methods based on their functionalities or features that the test method verifies. This helps us to execute only a particular set of tests as, and when required because of that, we can be more productive.
Grouping in TestNG
- “Groups” is another attribute like alwaysRun, ignoreMissingDependencies. You can use these TestNG Groups attribute at the method and class level. By using this attribute, you can specify a method or class belongs to which group or groups.
- “Groups” takes values as a string array, that means you can mention multiple group name at a time.
- During execution, by using testng.xml, you can also decide which group should execute by using the include and which groups will not execute by using exclude tags. If you are not mentioning any groups, then it will run all groups.
TestNG Groups Syntax
We can mention a group name to a method or a class by following the below syntax:
For Single group: groups={“groupname”}
For Multiple Group: groups={“groupname1″,”groupname2”}
Let us see a simple program:
package GroupsExample; import org.testng.annotations.Test; public class GroupingExample1 { // A test method belongs to Group G1 @Test(groups = { “G1” }) public void G1Method1() { System.out.println(“G1Method1”); } // A test method belongs to Group G1 @Test(groups = { “G1” }) public void G1Method2() { System.out.println(“G1Method2”); } // A test method belongs to Group G2 @Test(groups = { “G2” }) public void G2Method1() { System.out.println(“G2Method1”); } // A test method belongs to Group G2 @Test(groups = { “G2” }) public void G2Method2() { System.out.println(“G2Method2”); } // A test method belongs to Group G3 @Test(groups = { “G3” }) public void G3Method1() { System.out.println(“G3Method1”); } // A test method belongs to Group G3 @Test(groups = { “G3” }) public void G3Method2() { System.out.println(“G3Method2”); } }
Create a testng.xml file run the above test class
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test thread-count="5" name="Test"> <classes> <class name="GroupsExample.GroupingExample1"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
In this xml file, we have not mentioned any group name. It will run all the group’s methods.
But if we want to run some specific groups methods, then we can do that by following the below method:
- By setting the attribute “enabled=false” for all the methods of the group which you don’t want to execute.
- By using the include/exclude tag
Example:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test thread-count="5" name="Test"> <groups> <run> <include name="G1"/> </run> </groups> <classes> <class name="GroupsExample.GroupingExample1"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
If you want to run the other group, then you need to add another <include> tag inside the <run> tag. Like below:
<run> <include name="G1"/> <include name="G2"/> </run>
In the same way, you can mention the group name which you don’t want to run by using the <exclude> tag, like below:
<groups> <run> <exclude name="G1"/> </run> </groups>
Interview Question:
If a method belongs to multiple groups and if you run those groups separately, then how many times that method will be executed?
Sample Program:
package GroupsExample; import org.testng.annotations.Test; public class MultipleGroups { // A test method belongs to Group G1 and G2 @Test(groups = { "G1", "G2" }) public void MethodsBelongToG1G2() { System.out.println("MethodsBelongToG1G2"); } // A test method belongs to Group G1, G2 and G3 @Test(groups = { "G1", "G2", "G3" }) public void MethodsBelongToG1G2G3() { System.out.println("MethodsBelongToG1G2G3"); } // A test method belongs to Group G1 @Test(groups = { "G1" }) public void MethodsBelongToG1() { System.out.println("MethodsBelongToG1"); } // A test method belongs to Group G2 @Test(groups = { "G2" }) public void MethodsBelongToG2() { System.out.println("MethodsBelongToG2"); } // A test method belongs to Group G3 @Test(groups = { "G3" }) public void MethodsBelongToG3() { System.out.println("MethodsBelongToG3"); } // A test method belongs to Group G1 and G3 @Test(groups = { "G1", "G3" }) public void MethodsBelongToG1G3() { System.out.println("MethodsBelongToG1G3"); } }
Xml File:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test thread-count="5" name="Test"> <groups> <run> <include name="G1"/> <include name="G2"/> <include name="G3"/> </run> </groups> <classes> <class name="GroupsExample.MultipleGroups"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
Output:
MethodsBelongToG1
MethodsBelongToG1G2
MethodsBelongToG1G2G3
MethodsBelongToG1G3
MethodsBelongToG2
MethodsBelongToG3
Note: You can see the MethodsBelongToG1G2G3() method is assigned to multiple groups, so we may think that it will execute three times, but it will execute once.
If we want to run only the first two group then we can mention like this:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test thread-count="5" name="Test"> <groups> <run> <include name="G1"/> <include name="G2"/> <exclude name="G3"/> </run> </groups> <classes> <class name="GroupsExample.MultipleGroups"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
How to Use Regular Expression With TestNG Groups?
We have seen how, by making TestNG Groups, we can test methods as per requirement, which gives us flexibility and saving times also. Similarly, if we use some prefix or suffix with the group name, then we can easily use the regular expression by which we can able to decrease the complexity also. Because with the help of regular expression, we can represent more TestNG Groups names through a single statement.
Let us see how we can do that:
package GroupsExample; import org.testng.annotations.Test; public class GroupRegularExp { /* * Generally smoke tests are subset of regression. Smoke tests are run for high level testing of * basic functionality. SO sometimes you need to run only smoke tests and sometimes regression. * Regression will include more or less everything. */ @Test(groups = { "Regression_Group1", "Smoke_Group1" }) public void Group1Method1() { System.out.println("Group1Method1"); } @Test(groups = { "Regression_Group1" }) public void Group1Method2() { System.out.println("Group1Method2"); } @Test(groups = { "Regression_Group2", "Smoke_Group2" }) public void Group2Method1() { System.out.println("Group2Method2"); } @Test(groups = { "Regression_Group2" }) public void Group2Method2() { System.out.println("Group2Method2"); } @Test(groups = { "Regression_Group3", "Smoke_Group3" }) public void Group3Method1() { System.out.println("Group3Method3"); } @Test(groups = { "Regression_Group3" }) public void Group3Method3() { System.out.println("Group3Method3"); } }
Now from the above class if we want to run all the smoke group, then we have to mention all the smoke group one by one like below :
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test thread-count="5" name="Test"> <groups> <run> <include name="Smoke_Group1" /> <include name="Smoke_Group2" /> <include name="Smoke_Group3" /> </run> </groups> <classes> <class name="GroupsExample.GroupRegularExp" /> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
But we can achieve the same TestNG Groups thing by using the regular expression:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test thread-count="5" name="Test"> <groups> <run> <include name="Smoke.*" /> </run> </groups> <classes> <class name="GroupsExample.GroupRegularExp" /> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
As you can see above, we have used the regular expression, so that in a single statement, we can mention all the smoke groups.
Note:
- TestNG uses regular expressions and not wildcats. Be aware of the difference (for example, “anything” is matched by.”*” — dot star — and not “*”).
- You can’t use regex in the class name attribute with TestNG xml. Regex can be used to include exclude tags and package tags.
- Regex is case sensitive means “Smoke” and “smoke” both are different things.
Different TestNG Group Annotation Use In Selenium Example Program?
package com.selenium.TestNG; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; public class GroupingTests { @BeforeGroups public void login() { System.out.println("Login Sucessfully"); } @AfterGroups public void logout() { System.out.println("Logout Sucessfully"); } @Test(groups={"Test1"}) public void FundTransfer() { System.out.println("Fund Transfer"); } @Test(groups={"Test1","Test2"}) public void search() { System.out.println("Search Sucessfully"); } @Test(groups={"Test2"}) public void bill() { System.out.println("Bill Generated"); } }
How TestNG DependenceonGroups Annotation in Selenium Example?
package com.selenium.TestNG; import org.testng.annotations.Test; public class TestNGDependenceonGroupsClass { @Test(groups="Regression") public void testCaseOne() { System.out.println("Im in testCaseOne - And in Regression Group"); } @Test(groups="Regression") public void testCaseTwo(){ System.out.println("Im in testCaseTwo - And in Regression Group"); } @Test(groups="Smoke Test") public void testCaseThree(){ System.out.println("Im in testCaseThree - And in Smoke Test Group"); } @Test(groups="Regression") public void testCaseFour(){ System.out.println("Im in testCaseFour - And in Regression Group"); } @Test(dependsOnMethods="testCaseThree",groups="Smoke Test") public void testCaseFive(){ System.out.println("Im in testCaseFive - And in Smoke Test Group"); } }
Source: link
Leave a Reply