TestNG - Plug with ANT


In this chapter, we will demonstrate how to run TestNG using ANT. Let's follow the steps given below:

Step 1: Download Apache Ant

Download the latest version of Apache Ant

OSArchive Name

Step 2: Set Ant Environment

Set the ANT_HOME environment variable to point to the base directory location, where ANT libraries are stored on your machine. Let’s assume we've stored the Ant libraries in the folder apache-ant-1.8.4 folder.

WindowsSet the environment variable ANT_HOME to C:\Program Files\Apache Software Foundation\apache-ant-1.8.4
LinuxExport ANT_HOME=/usr/local/apache-ant-1.8.4
MacExport ANT_HOME=/Library/apache-ant-1.8.4

Append Ant compiler location to System Path as follows:

WindowsAppend the string %ANT_HOME\bin at the end of the system variable, Path.
LinuxExport PATH=$PATH:$ANT_HOME/bin/
MacNot required.

Step 3: Download TestNG Archive

Download the required jar files from://www.testng.org.

OSArchive name

Step 4: Create Project Structure

  • Create a folder TestNGWithAnt in C:\>TestNG_WORKSPACE.

  • Create a folder src in C:\>TestNG_WORKSPACE>TestNGWithAnt.

  • Create a folder test in C:\>TestNG_WORKSPACE>TestNGWithAnt.

  • Create a folder lib in C:\>TestNG_WORKSPACE>TestNGWithAnt.

  • Create MessageUtil class in C:\>TestNG_WORKSPACE>TestNGWithAnt>src folder.

* This class prints the given message on console.

public class MessageUtil {

   private String message;

   //@param message to be printed
   public MessageUtil(String message){
      this.message = message; 

   // prints the message
   public void printMessage(){
      return message;

   // add "Hi!" to the message
   public String salutationMessage(){
      message = "Hi!" + message;
      return message;
  • Create TestMessageUtil class in C:\>TestNG_WORKSPACE>TestNGWithAnt>src folder.

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestMessageUtil {
   String message = "Manisha";	
   MessageUtil messageUtil = new MessageUtil(message);

   public void testPrintMessage() {	
      System.out.println("Inside testPrintMessage()");     

   public void testSalutationMessage() {
      System.out.println("Inside testSalutationMessage()");
      message = "Hi!" + "Manisha";
  • Copy testng-6.8.jar in C:\>TestNG_WORKSPACE>TestNGWithAnt>lib folder.

Create ANT build.xml

First, we need to define the TestNG Ant task as follows:

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

Then, we'll be using <testng> task in Ant to execute our TestNG test cases.

The build.xml file is as follows:

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

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

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

   <property name="testdir" location="test" />
   <property name="srcdir" location="src" />
   <property name="libdir" location="lib" />
   <property name="full-compile" value="true" />
   <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="${srcdir}" includes="testng.xml"/> 

Run the following Ant command.


Verify the output.

   [testng] [TestNG] Running:
   [testng]   C:\TestNG_WORKSPACE\TestNGWithAnt\src\testng.xml
   [testng] Inside testPrintMessage()
   [testng] Manisha
   [testng] Inside testSalutationMessage()
   [testng] Hi!Manisha
   [testng] ===============================================
   [testng] Plug ANT test Suite
   [testng] Total tests run: 2, Failures: 0, Skips: 0
   [testng] ===============================================

Total time: 1 second