TestNG Showcasing – Suites

Although supported in JUnit 4, in the earlier JUnit 3 it was only TestNG that made the Suites hierarchy possible.

To gather the specifics of Suites we would need a config xml file that for our case would look like:

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="Suites test">
  <test name="testingSuites">
    <classes>
       <class name="com.dimitrisli.testng.suite.TestNGSuiteClass2" />
       <class name="com.dimitrisli.testng.suite.TestNGSuiteClass1" />
    </classes>
  </test>
</suite>

Since we are using Maven as our build management tool, it’s good to know that the Surefire plugin is TestNG-enabled needing the following structure to specify the TestNG related config file:

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

For our example we have two test Classes:

package com.dimitrisli.testng.suite;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGSuiteClass1 {

	@Test
	public void method1(){
		System.out.println("From TestNGSuiteClass1 test case");
		Assert.assertTrue(true);
	}
}

and

package com.dimitrisli.testng.suite;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGSuiteClass2 {

	@Test
	public void method1(){
		System.out.println("From TestNGSuiteClass2 test case");
		Assert.assertTrue(true);
	}
}

After executing mvn package we notice the following in the output:

Running TestSuite
From TestNGSuiteClass2 test case
From TestNGSuiteClass1 test case

We have just intervened in the Maven build lifecycle enhancing the test phase to consider our TestNG suite and to run the unit tests accordingly.

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