TestNG Maven plug-ins

Table of Contents

Maven 2

Maven2 has changed quite a bit from the first release. Maven2 should support TestNG out of the box without the need to download any additional plugins (other than TestNG itself). In fact, other than specifying testng as one of your build dependencies, most people shouldn't really have to do any other configuration for the majority of cases.

Snapshot Build Updates

The latest current good version of the plugin that you want is 2.4-SNAPSHOT.

To get the apache ibiblio snapshot repository added to your Maven project you will need to add a section like the following to your pom.xml:

<pluginRepositories>
   <pluginRepository>
      <id>apache.snapshots</id>
      <url>http://people.apache.org/repo/m2-snapshot-repository/</url>
   </pluginRepository>
</pluginRepositories>

Surefire Documentation

The TestNG Surefire documentation can be found on Apache's web site.

Reference

Though this section will include a lot more TestNG specific features, the overall test plugin properties should be mostly covered on Maven's site.

Overview

Most of the configuration aspects with Maven will be centered around your pom.xml's build section. In here you define things like build output directories, location of test source code, as well as configure plugin parameters, like those you will need to get TestNG setup the way that you prefer. Following is a sample portion of this section in a pom.xml file.

<dependencies>
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>5.5</version>
  <scope>test</scope>
  <classifier>jdk15</classifier>
</dependency>
<dependencies>

<build>
..
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.4-SNAPSHOT</version>
      <configuration>
        <groups>functest,util</groups>
      </configuration>
    </plugin>
  </plugins>
</build>

The most notable section is the configuration element. Within the boundaries of this element you can define all of the configurable TestNG properties.

Configuration

Goals
GoalDescription
testCompiles and runs your tests
siteCreates your Maven generated site, which will include the TestNG report output.

Properties

PropertyOptional?Description
includesNoComma delimited list of regexp patterns to include source files with. Ie **/Test*.java
groupsYesComma delimited list of groups to be included in test. If left blank will run all tests by default.
excludeGroupsYesComma delimited list of groups that should not be included in test run.
suiteXmlFilesYesComma delimited list of file paths pointing to testng.xml suite files. (src/test-data/testng-core.xml,src/test-data/testng-functional.xml)

Warning: When defining suiteXmlFiles most of the other parameters are ignored.

threadCountYesNumber of threads to run for tests
parallelYesWhen using threads, whether or not to run them in parallel. Use tests to have each test run in its own thread or methods to have the methods invoked from different threads.

Java 1.4

In order to use javadoc style annotations you currently must run Maven with a java 1.4 jvm in order for them to be seen. If you try running your javadoc annotated tests in a 1.5 jvm they will most likely not be found. It is hoped in a future release this problem will be eliminated.

Sample Report

A sample surefire report with TestNG can be found here.


Maven TestNG Archetype (Martin Gilday)

Martin Gilday has added a new archetype for Maven2 users that should make it easier to get going with TestNG. You can find more about this in his blog entry here, but the basics for configuring it have been pasted below.

To create a project using the archetype you simply have to specify my repository and the archetype ID.

  mvn archetype:create -DgroupId=org.martingilday -DartifactId=test1 -DarchetypeGroupId=org.martingilday -DarchetypeArtifactId=testng-archetype
    -DarchetypeVersion=1.0-SNAPSHOT -DremoteRepositories=http://www.martingilday.org/repository/

Of course substitute in your own groupId and artifactId.

Don't forget to keep checking back at Martin's blog for more updates.



Maven 1 (by Andrew Glover)

The TestNG Maven plug-in is quite simple and consists of two goals and a series of optional properties.

Currently the 1.1 version of the plug-in is bundled with official releases of TestNG. To utilize the plug-in, copy the maven-testng-plugin-.jar to the $MAVEN_HOME/plugins directory.

For the latest version of the plug-in (1.2 as of 12/12/05), update your maven.repo.remote to include http://www.vanwardtechnologies.com/repository/ and then issue the following command: maven plugin:download. Maven will issue a series of questions, answer them as follows:

artifactId: maven-testng-plugin
groupId: testng
version: 1.2

Goals

Goal Description
testng Runs TestNG
testng:junit-report Creates a JUnit style report

Properties

Property Optional? Description
maven.testng.suitexml.name Yes XML file name- defaults to testng.xml
maven.testng.suitexml.dir Yes Directory where XML file lives. Defaults to ${basedir}/test/conf
maven.testng.output.dir Yes Default report directory. Defaults to ${maven.build.dir}/testng-output
maven.testng.source.dir Yes For 1.4- where test source is. Defaults to ${basedir}/test/java/
maven.testng.report.dir Yes Directory for JUnit reports. Defaults to ${maven.build.dir}/testngJunitReport

A sample JUnit sytle report generated via the TestNG maven plug-in can be found here.