”;
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 FilesApache Software Foundationapache-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_HOMEbin 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; //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 /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); @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-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"> <classpath> <pathelement location = "lib/testng-7.4.jar"/> </classpath> </taskdef>
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"> <classpath> <pathelement location = "lib/testng-7.4.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.
/work/testng/src/TestNGWithAnt$ ant
Verify the output.
test: [testng] [TestNG] Running: [testng] /work/testng/src/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
”;