The Maven Cookbook
7.3. Running TestNG Tests
To use TestNG, you will need to add TestNG as a dependency in your project's POM. Since the TestNG unit test shown in this section uses Java 5 annotation, you will also need to configure the Maven Compiler plugin to target Java 5. The following POM shows the minimum required configuration for running TestNG tests with Java 5 annotations.
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.sonatype.mcookbook</groupId> <artifactId>testng-tests</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>testng-tests</name> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>5.10</version> <classifier>jdk15</classifier> <scope>test</scope> </dependency> </dependencies> </project>
You would then put your TestNG unit test classses in the
${basedir}/src/test/java
directory. The following
TestNG test uses Java 5 annotations to mark a class and a method as a
TestNG test. The following TestNG unit test duplicates the JUnit test
shown in Section 7.2, “Running JUnit Tests”, and it tests the
SeriousComponent class.
Example 7.3. TestNG Test for the SeriousComponent
package org.sonatype.mcookbook; import org.testng.annotations.Test; @Test public class SeriousComponentTest { @Test public void testSeriousness() throws Exception { assert SeriousComponent.testSeriousness("SAD"); assert SeriousComponent.testSeriousness("SERIOUS"); assert SeriousComponent.testSeriousness("CRAZY"); assert !SeriousComponent.testSeriousness("FUNNY"); } }
There is no other configuration necessary to have Maven execute
any TestNG tests it locates under ${basedir}/src/test/java
which match
the pattern *Test.java
. Running mvn
test will execute your TestNG unit tests.
$ mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building testng-tests
[INFO] task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered
resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to /Users/Tim/Library/Code/sonatype/
maven-cookbook/mcookbook-examples/unit/testng-tests/target/classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered
resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to /Users/Tim/Library/Code/sonatype/
maven-cookbook/mcookbook-examples/unit/testng-tests/target/test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /Users/Tim/Library/Code/sonatype/
maven-cookbook/mcookbook-examples/unit/testng-tests/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.292 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Thu Nov 26 08:24:02 CST 2009
[INFO] Final Memory: 17M/80M
[INFO] ------------------------------------------------------------------------
Once the TestNG unit tests have been executed, you will have a
TestNG HTML report under
${basedir}/target/surfire-reports/index.html
. To
view a report which displays statistics about which tests failed and
which tests passed, load this HTML page in a web browser.