Groups attribute in TestNG

  1. Include groups at test tag level
  2. Exclude groups at test tag level

Include groups at test tag level


Let us now see how to group @Tests using groups attribute in TestNG.

Let us create ‘LoginUser’ class under below package

Add 5 @Test methods

Add groups={“smoke”} for first and third @Test, see below

Let all 5 @Test methods fall into sanity group

Right click src/main/java > TestNG > Convert to TestNG

The xml gets created

Change the name to “LoginTest” as shown below

Let us now add groups tag at test level, see below

Next, we will open <run> tag within <groups> tag

Next, we will open <include /> tag within <run> tag, see below

Now, some of the @Tests are smoke tests and some of them are sanity tests 

Let’s say we want to execute only smoke tests. 

To do that, we can simply mention name=”smoke” as shown below

Save the xml.

Right click xml > Run as > TestNG Suite

Notice below that only 2 @Tests are executed. These 2 tests are ‘smoke’ tests 

This is how we use the <include> tag inside the xml.

Exclude groups at test tag level

We can also exclude the groups. So below will exclude all the @Tests that have groups=”sanity”

Save xml and run

Notice above, none of the test got executed. The reason being, all the @Tests are sanity tests and hence all of them got excluded

Let us remove “sanity” from first @Test

Save the class.

Now let us run the xml again. Notice below, this time the first @Test gets executed, since this test does not have “sanity” group. All the “sanity” tests got excluded viz they did not run

This is how we use the <exclude> tag inside the xml.

Thanks.