Site icon TestingDocs.com

TestNG Test Groups

Introduction

In this post, we will go through TestNG test Groups. In the TestNG framework, we can group multiple test methods into one group. We can execute the test methods that belong to the group. For example, we can have the following groups and segregate the @Test methods like:

First things first, we look into an example of how to group test methods in the TestNG framework. Let’s go through a code example test class that has 4 methods that belong to 2 groups as shown in the code example.

Code Example

package com.testingdocs.testng.sample;

import org.testng.annotations.Test;

public class GroupsExample 
{
  @Test(groups="group1")
   public void testMethod1() {  
     System.out.println("TestingDocs >> In testMethod1");
   }  
   
   @Test(groups="group2")
   public void testMethod2() {  
    System.out.println("TestingDocs >> In testMethod2");
   }  
   
   @Test(groups="group1")
   public void testMethod3() {  
    System.out.println("TestingDocs >> In testMethod3");
   }  
   
   @Test(groups="group2")
   public void testMethod4() {  
    System.out.println("TestingDocs >> In testMethod4");
   }

}

 

If we run as TestNG test class all the methods that belong to both the group
executes and displays the following output.
TestingDocs >> In testMethod1
TestingDocs >> In testMethod2
TestingDocs >> In testMethod3
TestingDocs >> In testMethod4

PASSED: testMethod1
PASSED: testMethod2
PASSED: testMethod3
PASSED: testMethod4

===============================================
Default test
Tests run: 4, Failures: 0, Skips: 0
===============================================

Now, we will try to run the test methods that belong to a particular group, lets say “group1”. In addition, let’s see how we could achieve that. We can achieve this in many ways using IDE, by using an XML file, etc.

Create an XML file with instructions to run only the group1 test methods. The sample XML file is as shown below:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="GroupExample">
  <test name="TestingDocsTest">
   <groups>
      <run>
        <include name="group1"/>
      </run>
    </groups>
    <classes>
       <class name="com.testingdocs.testng.sample.GroupsExample" />
    </classes>
  </test>
</suite>

 

As  above we have used the group1 in the groups node. Furthermore, we can name
this file by any name for example “groupsExample.xml”. Also, now run this file
as testNG suite file and notice that only the @test methods that belong group1
group would be executed as shown below:

 

 

Output:

TestingDocs >> In testMethod1
TestingDocs >> In testMethod3

===============================================
GroupExample
Total tests run: 2, Failures: 0, Skips: 0
===============================================

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

 

Exit mobile version