Monday, December 5, 2011

TestNG - Run your first test using ant

Let us write our First Test in TestNG and run as an ant task

Create Directory Tree Structure  as follows

testng(Base Directory)
    |
    +-------+-------------------------+---------------+------------+
    |          |                          |                 |           |
    src    lib                       build.xml  build    test-output/index.html
     |          |                                         |            |
    com testng-6.3.1.jar                  com           +-------+-----+---
      |                                                 |        index.html
     qa                                              qa
       |                                                 |
     OurFirstTestNGTest.java            OurFirstTestNGTest.class

Where as

OurFirstTestNGTest.java contains
=============================
package com.qa;

import org.testng.annotations.*;
import org.testng.*;

public class OurFirstTestNGTest {
    int testInt;

    @BeforeMethod
    public void setUp() {
        testInt = 0;
    }

    @Test
    public void addTest() {
        testInt++;
        Assert.assertEquals(testInt,1);
        System.out.println("Addition Test");
    }

    @Test
    public void subtractTest() {
        testInt--;
        Assert.assertEquals(testInt,-1);
        System.out.println("Subtract Test");
    }
}
=============================

build.xml contains
=============================
<project default="test">

 <path id="cp">
   <pathelement location="lib/testng-6.3.1.jar"/>
   <pathelement location="build"/>
 </path>

 <taskdef name="testng" classpathref="cp"
          classname="org.testng.TestNGAntTask" />

 <target name="test">
   <testng classpathref="cp">
     <classfileset dir="build" includes="**/*.class"/>
   </testng>
 </target>

</project>
=============================


Now to Compile a java class, use
#javac -classpath .:../lib/testng-6.3.1.jar -d ../build com/qa/OurFirstTestNGTest.java

To run using ant
#ant test

No comments:

Post a Comment