Monday, December 5, 2011

TestNG - Run your First Test

From TestNG official site, 

"TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use".

It was voted for over JUnit because of better reporting and executing capabilities from Testing team's perspective.

From deployment point of view, TestNG is just a jar file which can be copied onto a local machine and can be used by putting it in classpath. TestNG can be downloaded @ http://testng.org/doc/download.html

Let us write our First Test in TestNG.

Create Directory Tree Structure  as follows

testng(Base Directory)
    |
    +-------+-----+------------------------------+---------+
    |          |     |                                  |           |
    src    lib  testng.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");
    }
}
=============================


testng.xml contains
============================
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="test">
  <test name="test">
    <classes>
       <class dir="build" name="com.qa.OurFirstTestNGTest" />
    </classes>
  </test>
</suite>
============================

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

and to run the tests, use
#java -classpath .:build:./lib/testng-6.3.1.jar org.testng.TestNG testng.xml

No comments:

Post a Comment