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 Exampletest.java

package main;

import org.testng.annotations.Test;

public class ExampleTest {

	@Test
	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">
    	<classes>
            <class name="main.ExampleTest" />
        </classes>
	</test>
</suite>

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: https://ant.apache.org/bindownload.cgi

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">
      <classpath>
         <pathelement location="lib/testng-6.8.5.jar"/>
      </classpath>
   </taskdef>

	<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" />
   </fileset>
   
   <pathelement location="${testdir}" />
   <pathelement location="${srcdir}" />
   
   <path refid="classpath.base" />
   </path>
   
   <target name="clean" >
      <delete verbose="${full-compile}">
         <fileset dir="${testdir}" includes="**/*.class" />
      </delete>
   </target>
   
   <target name="compile" depends="clean">
      <javac srcdir="${srcdir}" destdir="${testdir}" verbose="${full-compile}">
         <classpath refid="classpath.test"/>
      </javac>
   </target>
   
   <target name="test" depends="compile">
      <testng outputdir="${testdir}" classpathref="classpath.test"> 
         <xmlfileset dir="${configdir}" includes="testng.xml"/> 
      </testng>
   </target>
   
</project>

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

2015-10-13_1520[1]
TestNG documentation can be found on http://testng.org/doc/documentation-main.html.

The whole project can be downloaded from https://www.dropbox.com/s/1jorkbdt6c8ae5v/TestNGExample.zip.

Similar Posts from the author: