How Can I Specify a Class Wide Group on a TestNG Test Case?


Group test is a new innovative feature in TestNG, which doesn’t exist in JUnit framework. It permits you to dispatch methods into proper portions and perform sophisticated groupings of test methods

Not only can you declare those methods that belong to groups, but you can also specify groups that contain other groups. Then, TestNG can be invoked and asked to include a certain set of groups (or regular expressions), while excluding another set.

Group tests provide maximum flexibility in how you partition your tests and doesn't require you to recompile anything if you want to run two different sets of tests back to back. Even group can me mentioned at class level so all tests will get executed of the classes those are part of the same group.

Groups are specified in your testng.xml file using the <groups> tag. It can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.

Now, let's take an example to see how group test works.

Approach/Algorithm to solve this problem

  • Step 1: Create a TestNG classes − NewTestngClass

  • Step 2: Write 3 different @Test method in the class − NewTestngClass

  • Step 3: Mark each @Test with group as mentioned below in the code file.

  • Step 4: Write a group name to the class at the top just before the class name as mentioned in program section.

  • Step 5: Now create the testNG.xml as given below to execute @Test those are having group name as GlobalGroup. It is class level group name.

  • Step 6: Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

  • Step 7: As per given configuration, it will execute all 3 testcases of NewTestngClass.

Example

The following code to show how to run testing groups:

src/ NewTestngClass.java

import org.testng.annotations.Test;

@Test(groups = { "GlobalGroup" })
public class NewTestngClass {

    @Test(groups = { "unit", "integration" })
    public void testCase1() {
        System.out.println("in test case 1 of NewTestngClass");
    }
    @Test(groups = { "integration" })
    public void testCase2() {
        System.out.println("in test case 2 of NewTestngClass");
    }
    @Test(groups = { "unit" })
    public void testCase3() {
        System.out.println("in test case 3 of NewTestngClass");

    }
}  

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases.

It is very handy when limited tests are needed to execute rather than full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
<groups>
         <run>
            <include name = "GlobalGroup" />
         </run>
      </groups>
      <classes>
    <class name = "NewTestngClass" />
</classes>
   </test>
</suite>

Output

in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
in test case 3 of NewTestngClass
===== Invoked methods
    newTest.testCase1()[pri:0, instance:newTest@7946e1f4]
    newTest.testCase2()[pri:0, instance:newTest@7946e1f4]
    newTest.testCase3()[pri:0, instance:newTest@7946e1f4]
=====

===============================================
suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

Updated on: 18-Aug-2023

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements