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 @

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.class

Where as contains

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

public class OurFirstTestNGTest {
    int testInt;

    public void setUp() {
        testInt = 0;

    public void addTest() {
        System.out.println("Addition Test");

    public void subtractTest() {
        System.out.println("Subtract Test");

testng.xml contains
<!DOCTYPE suite SYSTEM "" >
<suite name="test">
  <test name="test">
       <class dir="build" name="" />

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

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