The goal of this exercise is to run TestNG tests through Apache Ant.

First you have to have a sample TestNg project at your hand.
If you don’t have any, you can create the sample using the instructions below, or you can download it from using the link from the bottom of the post.
Create a simple class

package main;

import org.testng.annotations.Test;

public class ExampleTest {

	public void testA() throws Exception {

		System.out.println("Test finished.");


then you have to have the appropriate testNG.xml
it should contain the following:

<suite name="Example Test Suite" verbose="1" allow-return-values="true">
	<test name="Example tests">
            <class name="main.ExampleTest" />

You have to have the appropriate library files included in your project, the bare minimum libraries are the testng and the jcommander jars.
Now comes the trick.

Then You have to download Apache Ant:

To make it easier to use you have add the install path to the environment.
In windows you have to add the INSTALL_PATH\bin to the PATH system variable.
In Linux you can set it with PATH=$PATH:$INSTALL_PATH/bin/

Ant uses a file called build/xml for the build instructions.

<property name="configdir" location="config" /> 

– set the config folder, it is a good practice to use an separate folder for saving test suites.

<property name="testdir" location="test" /> 

– test dir, where the test code and reports will be saved

<property name="srcdir" location="src" /> 

– test source code location

<property name="libdir" location="lib" /> 

– java libraries

<property name="full-compile" value="false" /> 

– if you want you can use this parameter to set a more detailed compile report

<pathelement location="lib/testng-6.8.5.jar"/> 

– should be the location of the TestNG library

The other important thing in Ant files is the target.
Target is a section of ant script, which can start up multiple other scripts. With targets you can configure lots of different tasks and build configurations.
In our example the default target is test. It means that the test target will be run if there is no target explicitly specified.

<project name="TestNGTest" default="test" basedir=".">

   <!-- Define <testng> task -->

   <taskdef name="testng" classname="org.testng.TestNGAntTask">
         <pathelement location="lib/testng-6.8.5.jar"/>

	<property name="configdir" location="config" />
  	<property name="testdir" location="test" />
	<property name="srcdir" location="src" />
  	<property name="libdir" location="lib" />
	<property name="full-compile" value="false" />
   <path id="classpath.base"/>
   <path id="classpath.test">
   <fileset dir="${libdir}">
      <include name="**/*.jar" />
   <pathelement location="${testdir}" />
   <pathelement location="${srcdir}" />
   <path refid="classpath.base" />
   <target name="clean" >
      <delete verbose="${full-compile}">
         <fileset dir="${testdir}" includes="**/*.class" />
   <target name="compile" depends="clean">
      <javac srcdir="${srcdir}" destdir="${testdir}" verbose="${full-compile}">
         <classpath refid="classpath.test"/>
   <target name="test" depends="compile">
      <testng outputdir="${testdir}" classpathref="classpath.test"> 
         <xmlfileset dir="${configdir}" includes="testng.xml"/> 

to run the project you just have to call ant from the root folder, as it is illustrated on the following screenshot.


TestNG documentation can be found on

The whole project can be downloaded from

Posted By Tihomir Turzai

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>