Testng Groups – Include & Exclude (Example)

TestNG is a Testing framework that covers different types of test designs like unit, functional, end-to-end, UI and integration tests.

You can run single or multiple packages (package here means to encapsulate a group of classes in a proper director format) by creating XML and running it through maven.

TestNG Groups with Example

We use groups in Testng when,

  • We don’t want to define test methods separately in different classes (depending upon functionality) and
  • At the same time want to ignore (not execute) some test cases as if they do not exist in the code.
  • So to carry out this we have to Group them. This is done by using the “include” and “exclude” mechanisms supported in testNG.

In the example below, we have shown the syntax of how to use groups in the XML file.

@Test (groups = { "bonding", "strong_ties" })

Here we are using 2 group names i.e. “bonding” and “strong_ties” (these are the logical names that can be altered as per your wish).

<groups> tag defines the starting of groups in XML.

Customize your XML to pick the mentioned group from the test classes. Below mentioned is the syntax of how to declare groups in an XML file e.g.

 <groups>		
   <run>		
    <include name="bonding" />		
   </run>		
  </groups>		

So, let us assume that there are 10 test methods in a class.

Out of them,

  • 6 methods are tagged in “bonding” group and
  • 4 are in “strong_ties” group

Moving forward, we are going to set maven/Java path and use the Eclipse IDE to demonstrate the usage of groups using XML files in Java based maven project.

Create XML for TestNG with Tags

  • XML (Extensible Markup Language) file in the Maven framework contains the information of one or more tests and is defined by the tag <suite>.
  • Test information in XML is represented by tag <test> and can contain one or more TestNG classes.
  • A Java class that contains @Test annotation above test methods is defined as TestNG methods.

Multiple tags are used in a sequence to build a working testNG xml like <suite>, <test> and <class>

  • First is <suite> tag, which holds a logical name that defines full information to testNG reported to generate execution report.
  • Second is <test name=”Guru 99 Smoke Test Demo”>, note it is logical name which holds the information of test execution report like pass, fail, skip test cases and other information like total time for execution and group info
  • Third is <class name=”com.group.guru99.TC_Class1” />, com.group.guru99 is the package used, and Test Class name is TC_Class1.
<?xml version="1.0" encoding="UTF-8" ?>	
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">	
 	<suite name="Suite">	
		<test name="Guru 99 Smoke Test Demo">	
			<groups>	
				<run>	
   					 <include name="strong_ties" />	
        		</run>	
       		</groups>	
			<classes>	
					<class name="com.group.guru99.TC_Class1" />	
           	</classes>	
		</test>	
  </suite>	

We will be using this XML for the upcoming video given in the sections below.

“exclude” or “include” in test XML

Suppose you are finding the usage of group mechanism complex then testNG XML facilitate the functionality to exclude/include a test.

Exclude Tag:  Syntax for exclude tag <exclude name="${TEST_CASE_NAME}" />
Include Tag:  Syntax for include tag <include name="${TEST_CASE_NAME}" />

Note: We can include/exclude multiple test cases once at a time, which also works with Groups.

Run TestNG Group, include, exclude code (video demo)

Explanation of the Java Code and XML with the group, exclude and include the tag in XML.

Test Scenario: Launch Guru99 demo Banking site, verify few things’s on the login page after that, enter credentials, and re-verify a few new things on the application when logged in.

Run TestNG Group, Include, Exclude Code

Note: Each step that you code should be declared in separate methods, but when executed, it will execute test methods depending upon the entries in the XML file.

  • Method 1: Initialize Browser and launch URL (tc01LaunchURL())
  • Method 2: Verify Login Page Heading (tc02VerifyLaunchPage())
  • Method 3: Enter userName and Password on login form (tc03EnterCredentials())
  • Method 4: Verify the presence of Manager ID on User Dashboard (tc04VerifyLoggedInPage())
  • Method 5: Verify few more links on User DashBoard (tc05VerifyHyperlinks())

Code for our scenario:

package com.group.guru99;	

import java.util.concurrent.TimeUnit;	

import org.openqa.selenium.By;	
import org.openqa.selenium.WebDriver;	
import org.openqa.selenium.firefox.FirefoxDriver;	
import org.testng.Assert;	
import org.testng.annotations.Test;	

public class TC_Class1 {	
    public static final WebDriver webDriver = new FirefoxDriver();;	

    String launchPageHeading = "//h3[text()='Guru99 Bank']";	
    final String userName_element = "//input[@name='uid']", password_element = "//input[@name='password']",	
            signIn_element = "//input[@name='btnLogin']";	
    final String userName_value = "mngr28642", password_value = "ydAnate";	
    final String managerID = "//td[contains(text(),'Manger Id')]";	
    final String newCustomer = "//a[@href='addcustomerpage.php']", fundTransfer = "//a[@href='FundTransInput.php']";	

    /**	
     * This test case will initialize the webDriver	
     */	
    @Test(groups = { "bonding", "strong_ties" })	
    public void tc01LaunchURL() {	
        webDriver.manage().window().maximize();	
        webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);	
        webDriver.get("http://www.demo.guru99.com/V4/");	
    }	

    /**	
     * Will check the presence of Heading on Login Page	
     */	
    @Test(groups = { "bonding" })	
    public void tc02VerifyLaunchPage() {	
        Assert.assertTrue(webDriver.findElement(By.xpath(launchPageHeading)).isDisplayed(),	
                "Home Page heading is not displayed");	
        System.out.println("Home Page heading is displayed");	
    }	

    /**	
     * This test case will enter User name, password and will then click on	
     * signIn button	
     */	
    @Test(groups = { "bonding", "strong_ties" })	
    public void tc03EnterCredentials() {	
        webDriver.findElement(By.xpath(userName_element)).sendKeys(userName_value);	
        webDriver.findElement(By.xpath(password_element)).sendKeys(password_value);	
        webDriver.findElement(By.xpath(signIn_element)).click();	
    }	

    /**	
     * This test case will verify manger's ID presence on DashBoard	
     */	
    @Test(groups = { "strong_ties" })	
    public void tc04VerifyLoggedInPage() {	
        Assert.assertTrue(webDriver.findElement(By.xpath(managerID)).isDisplayed(),	
                "Manager ID label is not displayed");	
        System.out.println("Manger Id label is displayed");	
    }	

    /**	
     * This test case will check the presence of presence of New customer link	
     * And FundTransfer link in Left pannel	
     */	
    @Test(groups = { "bonding" })	
    public void tc05VerifyHyperlinks() {	
        Assert.assertTrue(webDriver.findElement(By.xpath(newCustomer)).isEnabled(),	
                "New customer hyperlink is not displayed");	
        System.out.println("New customer hyperlink is displayed");	

        Assert.assertTrue(webDriver.findElement(By.xpath(fundTransfer)).isEnabled(),	
                "Fund Transfer hyperlink is not displayed");	
        System.out.println("Fund Transfer hyperlink is displayed");	
    }	

}	

Please Note: The credentials are only valid for 20 days, so if you try to run code on your local machine, you might face invalid credentials error.

Explanation of Code:

As mentioned above, we have created 5 test cases for performing each action in independent methods.

For every method, we have associated a group parameter with some value.

Basically, these are the name of the differentiating groups i.e. “strong_ties” & “bonding”.

  • First and Third methods are tagged to “bonding”, “strong_ties” which means if XML is updated in any of the group, this Test Case will run.
  • The second method is only tagged to “bonding” group it means that if XML is updated with bonding group. Only in that case this test case will run.
  • Fourth Test case is tagged to strong_ties group, which means this test case will only run if XML is updated with strong_ties group name.
  • Last but not the least fifth test case is attached to bonding group, which means this test case will only run if XML is updated with bonding group name.

So overall, we have 4 scenarios;

Scenario 1: We want to run all test cases irrespective of the group name. In this case, we will remove Group tag from running XML.

Scenario 2: We want to run a few tests that are related only to either of the groups i.e. strong_ties or bonding. Please refer:

  • In this video, Group parameter is commented from running XML. Hence, you will see all test cases were executed.
  • In continuation to the video, now we have included group name in XML, you can see only test cases specific to that group is only running.

Scenario 3: We are using Exclude mechanism to exclude the test case. Please refer the video

  • You see that we have used exclude few test case (tc02) by writing their name in running XML. In the final result, mentioned test cases did not run.

Scenario 4: Last, we are using the include test mechanism to include the test cases (tc01LaunchURL, tc03EnterCredentials, and tc05VerifyHyperlinks). Please refer the video

In this video, you will see that test cases which are mentioned in XML are only running during the test execution.

Please download the code from the code for the above example-

Download the above Code

Conclusion

We have learned here a relatively new way of running test cases using XML in the Maven project.

We started by providing a brief introduction to testNG and continued with the full technical specification of Groups, exclude and include.