How to Use Testng With Eclipse

This tutorial will teach you how to setup and run TestNG on eclipse. What you need: 1.) Download and setup eclipse-java-helios 2.) Downl...

This tutorial will teach you how to setup and run TestNG on eclipse.

What you need:
1.) Download and setup eclipse-java-helios
2.) Download and extract TestNG ( in c:\java\testng-version
2.) Install TestNG on eclipse using this link (
a.) In eclipse select Help->Install New Software and enter the update url
3.) TestNG documentation here:

Working on eclipse:
1.) Create a new java project JavaHelpers.
2.) Create a new file build.xml (ant file)
3.) Create a new class TestNGDemo.
4.) Add Testng.jar to the project's build path by: right click project->Build Path->Configure Build Path->Select Libraries Tab->Add External Jar->Locate TestNg.jar to where we extract earlier (c:\java\testng.jar)
5.) This class, test add and sub mathematical operations.
package com.kbs.testng;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGDemo {
 public void sum() {
  Assert.assertEquals(7, 4 + 4);
 public void sub() {
  Assert.assertEquals(0, 4 - 4);
  * I am not a test.
 public void sumNotTest() {
  Assert.assertEquals(7, 4 + 4);
  * I am not a test.
 public void subNotTest() {
  Assert.assertEquals(0, 4 - 4);
The most important thing to notice is the @Test annotation, it tells us that, that method is a test. You can put the @Test annotation in the class level, which means all the method inside the class are test. Also note that if there's no @Test annotation on class level and on a method, it means it's not a test.

5.) To create a file which will tell the system of all the class which has test, right click the class->TestNG->Convert to TestNG->Next->Finish. And you will have a file testng.xml created in your project, which contains:
<suite name="Suite" parallel="none">
  <test name="Test" preserve-order="false">
      <class name="com.kbs.testng.TestNGDemo">
Note: You can add test classes here.
6.) To test a class simply: Right click the class->Run As->TestNG Test.
7.) To test the whole project: Right click testng.xml->Run As->TestNG Suite.
8.) And this is how it should look like:
9.) If you want to do it the ant way, here's the build script:

<project name="JavaHelpers" default="" basedir=".">
 <!-- set global properties for this build -->
 <property name="program_name" value="JavaHelpers" />
 <property name="package_name" value="JavaHelpers" />
 <property name="src" value="src" />
 <property name="build" value="build" />
 <property name="testng.home" value="C:/java/testng-6.2" />
 <property name="testng.jar" value="testng-6.2.jar" />
 <property name="" value="testng-output" />

 <path id="compile.boot.path">
  <fileset dir="${testng.home}">
   <include name="${testng.jar}" />
  <fileset dir="${java.home}/lib">
   <include name="rt.jar" />

 <target name="testng">
  <taskdef name="testng" classname="org.testng.TestNGAntTask">
   <classpath location="${testng.home}/${testng.jar}" />
  <mkdir dir="${}" />
  <testng outputdir="${}" classpath="${basedir}/${build}/classes">
   <xmlfileset dir="${basedir}" includes="testng.xml" />

 <!-- Compile Source -->
 <target name="compile">
  <mkdir dir="${build}/classes" />
  <javac destdir="build/classes" target="1.6" source="1.6">
   <bootclasspath refid="compile.boot.path" />
   <src path="${src}" />
   <include name="**/*.java" />
After a successful run it will create a folder: testng-output in your project's root directory.


java-getting-started 4728069689308262082

Post a Comment Default Comments


Anonymous said...

Perfect! thanks so much!!!! it worked!

Anonymous said...

Its working fine.
Thank you very much.

Raluca Duduman said...

hello, at step 1: what version of eclipse should I install? Eclipse IDE for Java Developers ?

thank you,

Anonymous said...

Hi Suditu, yes in this tutorial I've used: "Eclipse IDE for Java Developers".