TestNG - Plug With ANT

Advertisements


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

Step 1: Download Apache Ant

Download Apache Ant

OSArchive name
Windowsapache-ant-1.8.4-bin.zip
Linuxapache-ant-1.8.4-bin.tar.gz
Macapache-ant-1.8.4-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. For example, We've stored Ant libraries in apache-ant-1.8.4 folder on various Operating Systems as follows:

OSOutput
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 for different OS:

OSOutput
WindowsAppend the string ;%ANT_HOME\bin to the end of the system variable, Path.
Linuxexport PATH=$PATH:$ANT_HOME/bin/
Macnot required

Step 3: Download TestNG Archive

Download http://www.testng.org.

OSArchive name
Windowstestng-6.8.jar
Linuxtestng-6.8.jar
Mactestng-6.8.jar

Step 4: Create Project Structure

  • Create folder TestNGWithAnt in C:\ > TestNG_WORKSPACE

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

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

  • Create 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;

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

   // prints the message
   public void printMessage(){
      System.out.println(message);
      return message;
   }   

   // add "Hi!" to the message
   public String salutationMessage(){
      message = "Hi!" + message;
      System.out.println(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);
	   
    @Test
    public void testPrintMessage() {	
        System.out.println("Inside testPrintMessage()");     
        Assert.assertEquals(message,messageUtil.printMessage());
    }

    @Test
    public void testSalutationMessage() {
        System.out.println("Inside testSalutationMessage()");
        message = "Hi!" + "Manisha";
        Assert.assertEquals(message,messageUtil.salutationMessage());
    }
}
  • 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">
    <classpath>
      <pathelement location="lib/testng-6.8.jar"/>
    </classpath>
  </taskdef>

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

The C:\ > TestNG_WORKSPACE > TestNGWithAnt >\ build.xml is as follows:

<project name="TestNGTest" default="test" basedir=".">
<!-- Define <testng> task -->
  <taskdef name="testng" classname="org.testng.TestNGAntTask">
    <classpath>
      <pathelement location="lib/testng-6.8.jar"/>
    </classpath>
  </taskdef>
   <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" />
      </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="${srcdir}" includes="testng.xml"/> 
    </testng>
   </target>
</project>

Run the following ant command.

C:\TestNG_WORKSPACE\TestNGWithAnt>ant

Verify the output.

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

BUILD SUCCESSFUL
Total time: 1 second


Advertisements
Advertisements