Maven Surefire plugin & TestNG

In this post we will learn how to execute TestNG tests with Maven using maven-surefire-plugin. This post also goes over explaining TestNG XML configuration file (aka Suite, testng.xml) , it’s purpose, it’s content, it’s location in your project and how to use it for your tests [using Eclipse plugin or Maven]. Let’s begin.


What is the Purpose of Configuration file?

TestNG tests can be configured using annotations on your test classes and methods. But to get full support for groups, suites, exclusion approach and many more, XML configuration file is indispensable. This file mainly represent TestNG Suite and is usually named as testng.xml but you can name it anything you want. You can even have several such configuration files(i.e. several Suites) which can configure and execute certain sets of test based on certain condition.

How does it look like?

TestNG configuration XML file is based on testng dtd. Below are few examples of sample testng.xml file containing some commonly used elements related to testng configuration.

1) Execute all tests :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
    
    <test name="full">
        <packages>
            <package name="com.websystique.*" />
        </packages>
    </test>

</suite>

2) Execute all tests but not the ones within group ui :

<?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>
        <packages>
            <package name="com.websystique.*" />
        </packages>
    </test>

</suite>

3) Execute all tests but not the ones within group ui [using classes instead of packages]:

<?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.TestCalculator" />
        </classes>
    </test>

</suite>

4) Execute the tests separated in different categories(front-end, back-end);

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
    
    <test name="front-end">
        <packages>
            <package name="com.websystique.testng.frontend.*" />
        </packages>
    </test>

    <test name="back-end">
        <packages>
            <package name="com.websystique.testng.backend.*" />
        </packages>
    </test>

</suite>

5) Execute the tests separated in different categories(front-end, back-end); Run both front-end and back-end in parallel. Note that tests within one category(front-end e.g.) will still be executed in single thread.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests" parallel="tests" thread-count="2">
    
    <test name="front-end">
        <packages>
            <package name="com.websystique.testng.frontend.*" />
        </packages>
    </test>

    <test name="back-end">
        <packages>
            <package name="com.websystique.testng.backend.*" />
        </packages>
    </test>

</suite>

Where to put testng.xml in your project?

For Maven Project : If you are using maven based project, recommended place to put this file is in src/test/resources folder.

Within IDE: In case you are running your tests directly from within Eclipse using TestNG Eclipse plugin , put the xml file wherever you want, and then right click on file and Run As–>TestNG Suite.

Maven Surefire Plugin & TestNG

For maven based project, the popular approach to run the tests is using maven-surefire-plugin which is used by-default whenever test goal is executed [ with ‘mvn test’ / ‘mvn install’ e.g.]. You can configure this plugin in pom.xml to provide some configuration information like the location of test artifacts [testng.xml] and other properties.

<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>

Additionally, surefire-plugin provides option to include the conditions(defining group, excluding groups, thread-count, parallelism and many more directly with plugin configuration in pom.xml. So you have choice where to put those information(in pom.xml or in suite testng.xml).

Sample pom.xml with maven-surefire-plugin will look like:

<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>

That’s it.

References