The Maven Cookbook
7.5. Configuring TestNG Tests
Consider the following TestNG unit test which declares the parameter "databaseHostname" using the @Parameters annotation.
package org.sonatype.mcookbook;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
@Test
public class SeriousComponentTest {
@Parameters({"databaseHostname"})
@Test
public void testSeriousness(String databaseHostname) throws Exception {
assert SeriousComponent.testSeriousness("SAD");
assert SeriousComponent.testSeriousness("SERIOUS");
assert SeriousComponent.testSeriousness("CRAZY");
assert !SeriousComponent.testSeriousness("FUNNY");
System.out.println( "Database Hostname: " + databaseHostname );
}
}
This "databaseHostname" parameter can be configured by using the systemProperties configuration parameter of the Maven Surefire plugin. The following POM passes in a value of "testDb01" for the "databaseHostname" test parameter.
<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-config</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>testng-config</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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<property>
<name>databaseHostname</name>
<value>testDb01</value>
</property>
</systemProperties>
</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>
Running the test phase will execute the unit test passing the value from the POM to the unit test as a system property.
$ mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building testng-config
[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] Nothing to compile - all classes are up to date
[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-config/target/test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /Users/Tim/Library/Code/sonatype/
maven-cookbook/mcookbook-examples/unit/testng-config/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Database Hostname: testDb01
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.358 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 13:32:44 CST 2009
[INFO] Final Memory: 17M/80M
[INFO] ------------------------------------------------------------------------
For more information about TestNG parameters, see the TestNG project documentation here: http://testng.org/doc/documentation-main.html#parameters.