Categories: testing

TestNG Groups Example

In this post we will learn about how to use TestNG groups testing feature using @BeforeGroups, @AfterGroups and group exclusion.

TestNG allows us to group several tests together. You can group certain tests based on what behavior/aspect they are actually testing. You may have a scenario where few tests belong to a certain group(say database) and other ones belong to other group(say security) and yet another one belong to other group(say UI). With this approach you may decide to execute only certain group of test and skip other ones(let’s say there was a regression on database related code, so we prefer to only execute database related tests). Several Tests can belong to a group and a test can be part of several groups. Let’s begin.


@BeforeGroups, @AfterGroups

Method annotated with @BeforeGroups gets executed only once for a group before any of the test of that group executes. Method annotated with @AfterGroups gets executed only once for a group only after all of the tests of that group finished execution.

Let’s see this with a simple example:

package com.websystique.testng;

import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class TestNGGroupExample {

 @BeforeGroups("security")
 public void setUpSecurity() {
  System.out.println("setUpSecurity()");
 }
 
 @AfterGroups("security")
 public void tearDownSecurity() {
  System.out.println("tearDownSecurity()\n");
 }

 @BeforeGroups("database")
 public void setUpDatabase() {
  System.out.println("setUpDatabase()");
 }
 
 @AfterGroups("database")
 public void tearDownDatabase() {
  System.out.println("tearDownDatabase()\n");
 }
 
 @BeforeGroups(value="ui")
 public void setUpUI() {
  System.out.println("setUpUI()");
 }
 
 @AfterGroups(value="ui")
 public void tearDownUI() {
  System.out.println("tearDownUI()\n");
 }

 @Test(groups= "database")
 public void testInsert(){
  System.out.println("testInsert()");
 }
 
 @Test(groups= "database")
 public void testUpdate(){
  System.out.println("testUpdate()");
 }

 @Test(groups= "database")
 public void testDelete(){
  System.out.println("testDelete()");
 }

 @Test(groups= "security")
 public void accessHomePage() {
  System.out.println("accessHomePage()");
 }
 
 @Test(groups= "security")
 public void accessAdminPage() {
  System.out.println("accessAdminPage()");
 }

 @Test(groups= "ui")
 public void openConfirmationDialog() {
  System.out.println("openConfirmationDialog()");
 }

 @Test(groups= "ui")
 public void openFileDialog() {
  System.out.println("openFileDialog()");
 }
}

Run above tests class using mvn clean test or TestNG eclipse plugin.

Following is the output of above test run:

setUpSecurity()
accessAdminPage()
accessHomePage()
tearDownSecurity()

setUpUI()
openConfirmationDialog()
openFileDialog()
tearDownUI()

setUpDatabase()
testDelete()
testInsert()
testUpdate()
tearDownDatabase()

PASSED: accessAdminPage
PASSED: accessHomePage
PASSED: openConfirmationDialog
PASSED: openFileDialog
PASSED: testDelete
PASSED: testInsert
PASSED: testUpdate

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


===============================================
Default suite
Total tests run: 7, Failures: 0, Skips: 0
===============================================

It’s clear from above output that @BeforeGroups method was called before any of the test execution in that group. Similarly @AfterGroups method was called after all test executed in that group.

Group Exclusion:

Let’s assume that we only want to run security and database related tests and not the ones related to ui. We can exclude the ‘ui’ group from execution using testNG configuration xml file.

Create an XML file testng.xml (or with any other name) with below content in src/test/resources(if you are using maven) or in any other place in your project.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
 
    <test name="backend">
        <groups>
            <run>
             <exclude name="ui" />
            </run>
        </groups>
        <classes>
            <class name="com.websystique.testng.TestNGGroupExample" />
        </classes>
    </test>
 
</suite>

Now , if you are using maven based project, you can configure maven-surefire-plugin in pom.xml. Following is the pom.xml for this project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.websystique.testng</groupId>
 <artifactId>TestNGAnnotationsExample</artifactId>
 <version>1.0.0</version>
 <packaging>jar</packaging>

 <name>TestNGAnnotationsExample</name>

 <dependencies>
  <dependency>
   <groupId>org.testng</groupId>
   <artifactId>testng</artifactId>
   <version>6.9.4</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
     <suiteXmlFiles>
      <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
     </suiteXmlFiles>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Note how we have provided the path to testng.xml configuration file created above. Perform mvn clean test. You should see following output:[Alternatively, if you have already installed TestNG eclipse plugin, right click the testng.xml file->RunAs->TestNG Suite]

setUpSecurity()
accessAdminPage()
accessHomePage()
tearDownSecurity()

setUpDatabase()
testDelete()
testInsert()
testUpdate()
tearDownDatabase()


===============================================
tests
Total tests run: 5, Failures: 0, Skips: 0
===============================================

You can see that ‘ui’ related test methods and corresponding beforeGroups/afterGroups didn’t run.

That’s it.

References

Share
Published by

Recent Posts

Spring Boot + AngularJS + Spring Data + JPA CRUD App Example

In this post we will be developing a full-blown CRUD application using Spring Boot, AngularJS, Spring Data, JPA/Hibernate and MySQL,…

8 years ago

Spring Boot Rest API Example

Spring Boot complements Spring REST support by providing default dependencies/converters out of the box. Writing RESTful services in Spring Boot…

8 years ago

Spring Boot WAR deployment example

Being able to start the application as standalone jar is great, but sometimes it might not be possible to run…

8 years ago

Spring Boot Introduction + hello world example

Spring framework has taken the software development industry by storm. Dependency Injection, rock solid MVC framework, Transaction management, messaging support,…

8 years ago

Secure Spring REST API using OAuth2

Let's secure our Spring REST API using OAuth2 this time, a simple guide showing what is required to secure a…

8 years ago

AngularJS+Spring Security using Basic Authentication

This post shows how an AngularJS application can consume a REST API which is secured with Basic authentication using Spring…

8 years ago