Maven and TestNG
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.Maven and TestNG
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.
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>
Roger Keays is an artist, an engineer, and a student of life. He has no fixed address and has left footprints on 40-something different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, the proper use of semicolons, and finding good food.