TestNG Ant Task

You define the TestNG ant task as follows:

<taskdef resource="testngtasks"
         classpath="testng.jar"/>
This task runs TestNG tests and is always run in a forked JVM.  It accepts the following attributes:
 
Attribute Description Required
annotations Either the string "JDK" or "Javadoc". Defines which kind of annotations are used in these tests. If you use "Javadoc", you will also need to specify "sourcedir". No. Defaults to "JDK" if you're using the JDK 5 jar and to "Javadoc" if you're using the JDK 1.4 jar.
classfilesetref A reference to a FileSet structure of the test classes to be run.  
classpath A PATH-like structure for the tests to be run.  
classpathref A reference to a PATH-like structure for the tests to be run.  
delegateCommandSystemProperties Pass the command line properties as system properties. No. Defaults to false
dumpCommand Print the TestNG launcher command. No. Defaults to false
enableAssert Enables JDK 1.4 assertion. No. Defaults to true
failureProperty The name of a property to set in the event of a failure. It is used only if the haltonfailure is not set. No.
haltonfailure Stop the build process if a failure has occurred during the test run. No. Defaults to false  
haltonskipped Stop the build process if there is at least on skipped test. No. Default to false
groups The list of groups to run, separated by spaces or commas.
excludedgroups The list of groups to exclude, separated by spaces or commas
jvm The JVM to use, which will be run by Runtime.exec() java
listeners A comma or space-separated list of fully qualified classes that are TestNG listeners (for example org.testng.ITestListener or org.testng.IReporter) No.
outputdir Directory for reports output. No. Defaults to test-output.
skippedProperty The name of a property to set in the event of a skipped test. It is used only if the haltonskipped is not set. No.
sourcedir A PATH-like structure for JDK 1.4 tests (using JavaDoc-like annotations)  
sourcedirref A reference to a PATH-like structure for JDK 1.4 tests (using JavaDoc-like annotations).  
suiteRunnerClass A fully qualified name of a TestNG starter.

No.  Defaults to org.testng.TestNG

parallel The parallel mode to use for running the tests - either methods or tests No - if not present, parallel mode will not be selected
threadCount The number of threads to use for this run. Ignored unless the parallel mode is also specified 1
testJar Path to a jar containing tests and a suite definition.  
timeOut The maximum time out in milliseconds that all the tests should run under.  
useDefaultListeners Whether the default listeners and reporters should be used. Defaults to true.
workingDir The directory where the ant task should change to before running TestNG.  
xmlfilesetref A reference to a FileSet structure for the suite definitions to be run.  
suitename Sets the default name of the test suite, if one is not specified in a suite xml file or in the source code No. Defaults to "Ant suite"
testname Sets the default name of the test, if one is not specified in a suite xml file or in the source code No. defaults to "Ant test"

One of attributes classpath, classpathref or nested <classpath> must be used for providing the tests classpath.

One of the attributes xmlfilesetref, classfilesetref or nested <xmlfileset>, respectively <classfileset> must be used for providing the tests.

Note: if using JDK 1.4 one of the attributes sourcedir, sourcedirref or the nested <sourcedir> must be provided.

Note: using <classfileset> doesn't automatically adds the test classes to your classpath: you might need to repeat these classes in the classpath for the task to work.

Nested Elements

classpath

The <testng> task supports a nested <classpath> element that represents a PATH-like structure.

bootclasspath

The location of bootstrap class files can be specified using this PATH-like structure - will be ignored if fork is not set.

xmlfileset

The suite definitions (testng.xml) can be passed to the task with a FileSet structure.

classfileset

TestNG can also run directly on classes, also supplied with a FileSet structure.

sourcedir

A PATH-like structure for JDK 1.4 tests using Javadoc annotations.

jvmarg

Additional parameters may be passed to the new VM via nested <jvmarg> elements. For example:

<testng>
   <jvmarg value="-Djava.compiler=NONE" />
   <!-- ... -->
</testng>

sysproperty

Use nested <sysproperty> elements to specify system properties required by the class. These properties will be made available to the virtual machine during the execution of the test. The attributes for this element are the same as for environment variables:

<testng>
   <sysproperty key="basedir" value="${basedir}"/>
   <!-- ... -->
</testng>

will run the test and make the basedir property available to the test.

reporter

An inner <reporter> element is an alternative way to inject a custom report listener allowing the user to set custom properties in order to fine-tune the behavior of the reporter at run-time.
The element has one classname attribute which is mandatory, indicating the class of the custom listener. In order to set the properties of the reporter, the <reporter> element can contain several nested <property> elements which will provide the name and value attributes as seen below:

<testng ...>
   ...
   <reporter classname="com.test.MyReporter">
      <property name="methodFilter" value="*insert*"/>
      <property name="enableFiltering" value="true"/>
   </reporter>
   ...
</testng>
public class MyReporter {

  public String getMethodFilter() {...}
  public void setMethodFilter(String methodFilter) {...}
  public boolean isEnableFiltering() {...}
  public void setEnableFiltering(boolean enableFiltering) {...}
  ...
}
You have to consider though that for the moment only a limited set of property types are supported: String, int, boolean, byte, char, double, float, long, short.

env

It is possible to specify environment variables to pass to the TestNG forked virtual machine via nested <env> elements. For a description of the <env> element's attributes, see the description in the exec task.

Examples

Suite xml

<testng classpathref="run.cp"
        outputDir="${testng.report.dir}"
        sourcedir="${test.src.dir}"
        haltOnfailure="true">
 
   <xmlfileset dir="${test14.dir}" includes="testng.xml"/>
</testng>

Class FileSet

<testng classpathref="run.cp"
		outputDir="${testng.report.dir}"
		haltOnFailure="true"M verbose="2">
	<classfileset dir="${test.build.dir}" includes="**/*.class" />
</testng>