TestNG Tutorials 28: Grouping Test Methods In TestNG
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