TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • TestNG
  • Tools
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

TestNG

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:

  • P1.
  • Regression.
  • UI Tests/Database Tests / Sanity etc.
  • All tests.

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:

 

TestNG Test Groups Example

 

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

 

Related Posts

Tests TestNG Suite testng.xml

TestNG /

Run tests from TestNG Suite testng.xml file

Add TestNG library

TestNG /

Add the TestNG library to the project

new TestNG class

TestNG /

Create TestNG test class in Eclipse IDE.

TestNG Plugin IntelliJ

TestNG /

Enable TestNG in IntelliJ IDE

Installing Eclipse from Update Site

TestNG /

Install TestNG latest version from the update site

‹ Working with Assert in TestNG framework.› TestNG Dependencies

Recent Posts

  • Antivirus Products for Windows 11 PC
  • Install CodeBlocks IDE on Windows 11
  • Location Privacy Settings on Windows
  • Windows 11 Virus & Threat Protection
  • Download & Install Python on Windows 11
  • Cumulative Update for Windows 11 Version
  • Test execution speed in Selenium IDE
  • Check Windows 11 Free Upgrade
  • Download IntelliJ IDE on Windows 11
  • New MySQL Connection in Workbench

Back to Top

Automation Tutorials

  • JBehave Tutorial
  • Selenium Tutorial
  • TestNG Tutorial
  • JUnit Tutorial

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version