TestNG Tutorial and Example – Getting Started


TestNG requires JDK 5 or higher.

TestNG Download and installation

Download the current release version of TestNG from here http://testng.org/doc/download.html, unpacking the zip distribution to get TestNG decompression file, which includes the below sub files:

  • Testng-5.14.1.jar(please add to your project directly, you may also not be able to successfully build the codes only with this jar, because TestNG official decided that the release did not include all external jar files in order to keep the size down.)
  • Doc(TestNG tutorial material).
  • Example codes.
  • Testng source codes.
  • Readme.

As a beginner, I highly recommend you start and write TestNG from example codes and docs(best materials).

For the Eclipse plug-in, we suggest using the update site:

  • For Eclipse 3.4 and above, enter http://beust.com/eclipse.
  • For Eclipse 3.3 and below, enter http://beust.com/eclipse1.

Just a simple test code using TestNG

In TestNG, the developer would not need to extend any specific classes or enforce any naming conventions(You probably still remember the test cases name which need be started with “test” in Junit 3), after using TestNG, we just use the annotation @Test to signal to any methods of the class. There have two test methods in following illustrates.

package com.asjava;

import org.testng.annotations.*;

public class TestNGSimpleTest {
    int testInt;

    public void setUp() {
        testInt = 0;

    public void addTest() {
        assert (testInt == 1);
        System.out.println("add test");

    public void subtractTest() {
        assert (testInt == -1);
        System.out.println("subtract test");

Note: The method setUp() will be invoked before any test methods are run.

Run Test with Command

Before you can run the testsw with command, you must configure TestNG using a special XML file, conventionally named testng.xml.

Here is Configuration file for TestNG

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite 1">
  <test name="testNG test1">
       <class name="com.asjava.TestNGSimpleTest" />

To run the test, please compile the class TestNGSimpleTest and then invoke TestNG with the following command:

java -ea -classpath .;testng-5.14.1.jar org.testng.TestNG testng.xml

Run TestNG Test with IDE

If you use IDEA as your development tool, please navigate to Tools->Run, then add unit test to TestNG configuration.

Here is screenshot result that run TestNG testcase:

Run TestNG test with Ant

Once you have compiled your test class into the build directory, you can invoke to run your test with command line ant task, here is ant project xml:

<project default="TestNGSimpleTest">

 <path id="main_cp">
   <pathelement location="lib/testng-5.14.1.jar"/>
   <pathelement location="build"/>

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

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


Rate article
Add a comment

Your email address will not be published. Required fields are marked *