TestNG Tutorials 31: Multiple Groups Name For Test And Configuration Methods in TestNG

Hello Folks,

In previous posts, we have learnt about:

Grouping test methods in TestNG

Grouping configuration methods in TestNG

We know grouping is categorization of test scripts. Let’s ask a question to yourself:

Is it possible that a test script may fall in more than one categories? Obviously Yes.

So how can we achieve same thing in TestNG? Answer is “groups attribute”.

As we have discussed , “groups” attribute is an array. Let’s see in actual code of TestNG:

Since “groups” attribute is an array, now you can pass multiple values which will be group names. If we mention 2 group names, it means that particular test method or configuration method will belong to both groups. It is “And” not “OR”.

Let’ see an example:

A TestNG class containing multiple test methods belonging to multiple groups:

TestNG XML to run methods belonging to G1:

Output:

You can see in output, methods belonging to G1 (Not only G1) are executed.

Now, here comes an interview question.

What will happen if we include multiple groups? Will same methods will run multiple times for each group names as a method”MethodsBelongToG1G2G3″ belongs to G1, G2 and G3?

Answer is No. TestNG behaves intelligently here. If will not same method multiple times for each group. It will execute once.

TestNG XML for all groups:

Output:

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

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning