Ant and TestNG

1. Install Java (JDK) into folder without spaces ie. C:\Java
2. Download Ant and install to C:\Ant. Set C:\Ant\bin in your path.
3. Use TestNG jar file from TestNG instead of one from Eclipse. Current version as of this writing is testng-6.8

4. Create a directory structure in a parent folder called

5. Populuate /bin with the class files of your tests
6. Populate /lib with all the .jar files from selenium web driver and any other external jar files required to run your tests.
7. Populate /src with the java files of your tests
8. Place the testng-6.8.jar file in the root of the parent folder

9. Create a build.xml file at the root of the parent folder

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. -->
<project basedir="." default="build" name="anttest2">
    <property name="project.path" value="." />
<property name="src" value="${project.path}/src" />
<property name="bin" value="${project.path}/bin" />
<<property name="testngxmldir" value="${project.path}" />

		<path id="class.path">
        <pathelement location="bin"/>
        <pathelement location="lib/apache-mime4j-0.6.jar"/>
        <pathelement location="lib/bsh-1.3.0.jar"/>
        <pathelement location="lib/cglib-nodep-2.1_3.jar"/>
        <pathelement location="lib/commons-codec-1.6.jar"/>
        <pathelement location="lib/commons-collections-3.2.1.jar"/>
        <pathelement location="lib/commons-exec-1.1.jar"/>
        <pathelement location="lib/commons-io-2.2.jar"/>
        <pathelement location="lib/commons-jxpath-1.3.jar"/>
        <pathelement location="lib/commons-lang3-3.1.jar"/>
        <pathelement location="lib/commons-logging-1.1.1.jar"/>
        <pathelement location="lib/cssparser-0.9.9.jar"/>
        <pathelement location="lib/guava-14.0.jar"/>
        <pathelement location="lib/hamcrest-core-1.3.jar"/>
        <pathelement location="lib/hamcrest-library-1.3.jar"/>
        <pathelement location="lib/htmlunit-2.12.jar"/>
        <pathelement location="lib/htmlunit-core-js-2.12.jar"/>
        <pathelement location="lib/httpclient-4.2.1.jar"/>
        <pathelement location="lib/httpcore-4.2.1.jar"/>
        <pathelement location="lib/httpmime-4.2.1.jar"/>
        <pathelement location="lib/ini4j-0.5.2.jar"/>
        <pathelement location="lib/jcommander-1.29.jar"/>
        <pathelement location="lib/jetty-websocket-8.1.8.jar"/>
        <pathelement location="lib/jna-3.4.0.jar"/>
        <pathelement location="lib/jna-platform-3.4.0.jar"/>
        <pathelement location="lib/json-20080701.jar"/>
        <pathelement location="lib/junit-dep-4.11.jar"/>
        <pathelement location="lib/nekohtml-1.9.17.jar"/>
        <pathelement location="lib/netty-3.5.7.Final.jar"/>
        <pathelement location="lib/opencsv-2.3.jar"/>
        <pathelement location="lib/operadriver-1.3.jar"/>
        <pathelement location="lib/phantomjsdriver-1.0.3.jar"/>
        <pathelement location="lib/protobuf-java-2.4.1.jar"/>
        <pathelement location="lib/sac-1.3.jar"/>
        <pathelement location="lib/selenium-java-2.33.0.jar"/>
        <pathelement location="lib/selenium-java-2.33.0-srcs.jar"/>
        <pathelement location="lib/serializer-2.7.1.jar"/>
        <pathelement location="lib/testng-6.8.jar"/>
        <pathelement location="lib/xalan-2.7.1.jar"/>
        <pathelement location="lib/xercesImpl-2.10.0.jar"/>
        <pathelement location="lib/xml-apis-1.4.01.jar"/>
 <!-- Compile targets-->
<target name="test">
    <echo message="Compiling test Source..." />
    <mkdir dir="${bin}" />
    <javac classpathref="class.path" destdir="${bin}" encoding="UTF-8" optimize="off" 
    debug="on" failonerror="true" srcdir="${src}" />
    <jar destfile="${testJarFile.path}" basedir="${bin}" />

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

<target name="runTest" depends="test">
<echo message="Running Tests..." />
    <testng classpathref="class.path" outputDir="${project.path}/test-output">
        <xmlfileset dir="${testngxmldir}" includes="testng.xml"/>


10. Create a testng.xml file at the root of the parent folder

< ?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "" > 
 <suite name="Suite1" verbose="1" > 
   <test name="Regression1"   >  
		<package name="testng" /> 

11. to run the test at a command prompt: ant runTest OR ant test runTest


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s