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

OS Archive Name
Windows apache-ant-1.10.10-bin.zip
Linux apache-ant-1.10.10-bin.tar.gz
Mac apache-ant-1.10.10-bin.tar.gz

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.

OS Output
Windows Set the environment variable ANT_HOME to C:\Program Files\Apache Software Foundation\apache-ant-1.10.10
Linux Export ANT_HOME=/usr/local/apache-ant-1.10.10
Mac Export ANT_HOME=/Library/apache-ant-1.10.10

Append Ant compiler location to System Path as follows −

OS Description
Windows Append the string %ANT_HOME\bin at the end of the system variable, Path.
Linux export PATH=$PATH:$ANT_HOME/bin/
Mac Not required.

Step 3: Download TestNG Archive

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

OS Archive name
Windows testng-7.4.jar
Linux testng-7.4.jar
Mac testng-7.4.jar

Step 4: Create Project Structure

  • Create a folder TestNGWithAnt in /work/testng/src.

  • Create a folder src in /work/testng/src/TestNGWithAnt.

  • Create a folder test in /work/testng/src/TestNGWithAnt.

  • Create a folder lib in /work/testng/src/TestNGWithAnt.

  • Create MessageUtil class in /work/testng/src/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 /work/testng/src/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-7.4.jar in /work/testng/src/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-7.4.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-7.4.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.

/work/testng/src/TestNGWithAnt$ ant

Verify the output.

   [testng] [TestNG] Running:
   [testng]   /work/testng/src/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
Kickstart Your Career

Get certified by completing the course

Get Started