Maven and TestNG

Posted by Roger Keays, 8 November 2006, 9:58 AM

I'm fairly new to maven, but so far it has really impressed me with its simplicity, productivity gains and IDE independence. So I was really looking forward to porting some unit tests to TestNG, running mvn test and watching in awe as maven found my tests, ran them and generated a pretty html report of the test results. Unfortunately, it didn't turn out quite like that.

It would seem there are some unfortunate bugs in surefire (maven's test runner) and these extended my five minute job into six hours of bewilderment - with occasional profanities. Anyway, to cut a long story short, here is a list of problems I encountered using TestNG with maven, and the ultimate solution I settled for.

The Problems

A Solution

Fortunately, maven can run ant scripts and the testng jars include some ant tasks for executing testng 'natively'. We can configure surefire not to run the test cases and use the <testng> ant task instead. Also, the <junitreport> task can be used to generate a similar html report to that generated by the surefire-report plugin.

<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>

<!-- this ant script runs testng natively -->
<execution>
<id>testng</id>
<phase>test</phase>
<configuration>
<tasks>
<taskdef resource="testngtasks" classpath="testng.jar"
classpathref="maven.test.classpath"/>
<testng classpathref="maven.test.classpath"
outputdir="target/test-reports">
<xmlfileset dir="src/test/suites" includes="*.xml"/>
</testng>
<junitreport todir="target/test-reports">
<fileset dir="target/test-reports">
<include name="**/*.xml" />
</fileset>
<report format="noframes" todir="target/test-reports" />
</junitreport>
</tasks>
</configuration>
<goals><goal>run</goal></goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-junit</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
</plugin>

<!-- disable surefire plugin (too many problems!) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

Of course you'll need to make your project dependent on TestNG also:

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

References

[1] http://jira.codehaus.org/browse/MSUREFIRE-134
[2] http://jira.codehaus.org/browse/MSUREFIREREP-6
[3] http://jira.codehaus.org/browse/MSUREFIRE-172

Comment posted by: Binil Thomas on 8 November 2006, 10:37 PM

Hi Roger,

Thanks for the informative post.
Do you know a way to configure a TestNG listener when running the tests from Surefire?

Thanks,
Binil

Comment posted by: Roger Keays on 9 November 2006, 5:06 PM

Hey Binil,

I don't know if that can be done with Surefire, but it's another one that can be done with the ant task by using the listeners attribute.

Comment posted by: Tomek on 5 March 2007, 10:14 PM

maven-surefire-plugin ver. 2.3 fixes bug nr 1
the other two should be fixed in 2.4

thx for this post
Tomek
http://kaczanowscy.pl/tomek

Comment posted by: Daren on 14 May 2008, 3:51 PM

Thanks for the great post!  I can't believe we're halfway through 2008 and the surefire/TestNG combination is still so unstable!  I've tried every version combination of surefire and TestNG and have lost all hope of getting it to work.  This Ant workaround is just what the Doctor ordered.

Thanks! - Daren

Add a comment

Please visit http://www.ilikespam.com/blog/maven_and_testng to add your comments.