Hello Folks,
In this post, we will learn about grouping of test methods in TestNG.
“Group” is meant for categorisation of similar items. In a supermarket, you see how they separate different types of vegetables to make it easier for customers to locate them (As shown in above pic).
Similar concept we apply for test cases. We group test cases based on modules name, suite name etc. We group test cases in Smoke , Sanity, Regression suites etc and whenever required we execute those suites. If you need to execute test cases of module “UserAccess”, you find out test cases which belong to group “UserAccess” and run it. You can decide easily what to run and not to run.
TestNG provides you rich options to group your scripts. We will explore those concepts in this post.
Grouping in TestNG:
- “groups” is an attribute provided by TestNG which can be used for @Test annotated methods, Configuration methods and at class level. By using it, you can specify to which group/groups , this class or methods belong to.
- “groups” attribute is a String array. It means you can tag multiple groups to a method or class.
- You can specify which groups to include, exclude in testng xml explicitly using include and exclude tags.
- If you do not mention anything, by default it will run for all groups.
Syntax:
groups = {“G1”} ==> Single group
groups = {“G1″,”G2″,”G3”} ==> MUltiple groups
In this post, we will see grouping of test methods. Groping of configuration methods and classes will be in upcoming posts.
Example:
Let’s create a TestNG class containing test methods of three groups as:
Create a testng.xml as below:
If you run above xml , you will get below output:
You will find it as normal execution output.
Now consider, you just need to execute test methods belong to group “G1”. You have two options to do this:
1. Mark all methods of remaining groups (G2,G3 in this case) as “enabled=false”.
2. Only include/exclude desired group/s in testng.xml
Making changes at multiple places in code is not good and also it is not easy. If future you may need to change it as well. TestNG makes it easy for you and provide an option to include or exclude group through testng xml.
Update above testng xml as below:
Note: We are passing group information at test level. We can pass it as suit level as well. Which we will see in upcoming posts.
Now run it. You will see methods belong to Group G1 only got executed.
If you need to run test methods from group G1 and G2 then, you need to add another include tag as below:
Output:
Let’s consider a scenario in which you have multiple groups and you need to run all test methods except group G2. Suppose you have 10 groups and you need to execute 9 groups. So writing 1 exclude tag is better than writing 9 include tags.
Run above xml and you will see TestNG excludes methods of G2.
More about TestNG in upcoming posts. Stay tuned.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium
Hi Team,
I have big doubt over here in TestNG , we can extends the class ?
then we can mention as group to run the class one by one ?
Depends on which class extends you.