TestNG Showcase – Groups

Groups is a very nice versatile functionality that JUnit 4 is still missing. TestNG gives you the flexibility through specified groups to gather up and run buckets of unit tests grouped in their own separate scenarios. Someone could group for instance integration tests, database related tests, framework tests, the possibilities are endless.

In our example we would need a TestNG XML config file first, making all the necessary introductions:

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="Groups Suite">
  <test name="groups suite test">
  	<groups>
      <run>
        <include name="group2"/>
        <include name="group3"/>
      </run>
    </groups>
    <classes>
       <class name="com.dimitrisli.testng.groups.TestNGGroupsTesting" />
    </classes>
  </test>
</suite>

The referenced TestNG Class looks like this:

package com.dimitrisli.testng.groups;

import org.testng.annotations.Test;

public class TestNGGroupsTesting {

	@Test(groups="group3")
	public void method1(){
		System.out.println("inside method1 of group 3");
	}
	
	@Test(groups="group1")
	public void method2(){
		System.out.println("inside method2 of group 1");
	}
	
	@Test(groups="group2")
	public void method3(){
		System.out.println("inside method3 of group 2");
	}
	
	@Test(groups="group2")
	public void method4(){
		System.out.println("inside method4 of group 2");
	}
	
	@Test(groups="group1")
	public void method5(){
		System.out.println("inside method5 of group 1");
	}
}

Running just that class as a TestNG test would yield:

inside method1 of group 3
inside method2 of group 1
inside method3 of group 2
inside method4 of group 2
inside method5 of group 1
PASSED: method1
PASSED: method2
PASSED: method3
PASSED: method4
PASSED: method5

which are all the unit tests in order of appearance. This is because we didn’t take yet the xml config file into consideration. Since we are using Maven as a building tool specifying the config file takes place in the maven Surefire TestNG-enabled plugin that looks like this:

<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-surefire-plugin</artifactId>
  			<version>2.11</version>
  			<configuration>
  				<suiteXmlFiles>
  					
  					<suiteXmlFile>testngGroups.xml</suiteXmlFile>
  					
  				</suiteXmlFiles>
  			</configuration>
  		</plugin>

We’ve just shaped the testing phase of Maven to consider the TestNG groups in this special manner. Running mvn package produces the following among the output:

inside method1 of group 3
inside method3 of group 2
inside method4 of group 2

which is the grouping in the order we desired and have specified in the config file.

An interesting alternative or override is the Surefire plugins configuration to assist group references within the POM:

<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-surefire-plugin</artifactId>
  			<version>2.11</version>
  			<configuration>
  				<suiteXmlFiles>
  					
  					<suiteXmlFile>testngGroups.xml</suiteXmlFile>
  					
  				</suiteXmlFiles>
<groups>group2</groups>
  			</configuration>
  		</plugin>

If we do so, then we override the grouping specified in the config file having as output this time:

inside method3 of group 2
inside method4 of group 2

You can find this code in this Github repository.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s