TestNG tutorial for beginners

Here i will learn and teach you some basics of TestNG.

What is TestNG

TestNG is a testing framework which is used for different range of testing. e.g. Unit Testing, Integration Testing etc.

How to Write Tests

  1. Create test file where you will write business logic which needs to be tested.
  2. Use TestNG annotation to identify different method execution levels, Groups and Suites.
  3. Create build.xml or testng.xml file.
  4. In above file provide details of test class and group or suites.
  5. RUN TestNG.


TestNG eclipse example

Here i will teach you how to install TestNG eclipse plugin and use the same to create test and run them.

Note*: Copied from TestNG Site

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

Select Help / Software updates / Find and Install.
Search for new features to install.
New remote site.
For Eclipse 3.4 and above, enter http://beust.com/eclipse
For Eclipse 3.3 and below, enter http://beust.com/eclipse1
Make sure the check box next to URL is checked and click Next.
Eclipse will then guide you through the process.


Once plugin installed next step would be to create simple test file and run it.

Create Dynamic Web Project(TestNGWebProject) in Eclipse. To know how to create it please visit here. Once project created, create TestNG class using template.




In Next screen as shown above several options will be available. Here we can provide the package name, class name and different annotation that can be used. Also we can optionally provide XML suite file(TestNG.xml).

Note*: You need to add testng JAR from here.
FirstTest.java : Sysout has been added to identify sequence of different methods.

package com.jbt;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

public class FirstTest {
@Test(dataProvider = "dp")
public void f(Integer n, String s) {
System.out.println("Inside Data Provider");

public void beforeMethod() {
System.out.println("Before Method");

public void afterMethod() {
System.out.println("After Method");

public Object[][] dp() {
return new Object[][] { new Object[] { 1, "a" },
new Object[] { 2, "b" }, };

public void beforeClass() {
System.out.println("Before Class");

public void afterClass() {
System.out.println("After Class");

public void beforeTest() {
System.out.println("Before Test");

public void afterTest() {
System.out.println("After Test");

public void beforeSuite() {
System.out.println("Before Suite");

public void afterSuite() {
System.out.println("After Suite");



<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<class name="com.jbt.FirstTest"/>
</test> <!-- Test -->
</suite> <!-- Suite -->

How to Run Test

Till now we have created Test class and Suite XML file. Next step would be to create run configuration and run our test. To create run configuration go to Run >> Run Configuration. Here you will find TestNG option in left navigation. Right click same option and select New as shown below.




When new button is clicked you will get another window which will ask you different options like Class/ Method / Groups/ Package/ Suite. As we have already created suites for this newly created class(FirstTest.java) select the same(TestNG.xml) using Browse button.


Click  Apply and Run.

Output will be like below.


[TestNG] Running:

Before Suite
Before Test
Before Class
Before Method
Inside Data Provider
After Method
Before Method
Inside Data Provider
After Method
After Class
After Test
After Suite

Total tests run: 2, Failures: 0, Skips: 0

Here you can identify the sequence of test method execution.

This is it for this article. Will write more article once i learn more about TestNGG. 🙂

By | 2013-12-25T12:04:15+00:00 December 25th, 2013|Uncategorized|4 Comments

About the Author:


  1. Shrikant March 21, 2014 at 7:09 am - Reply

    Thanks Vivek & Jayshree , this site helped me code my first script using TestNG.

  2. subha August 12, 2014 at 9:54 pm - Reply

    Thanks. This site really helped me to start my first TestNG Tests.

  3. Saurabh December 31, 2014 at 9:40 am - Reply

    After suite is not printed .. when i run this program on my machine .. what could be the issue

  4. Neelam April 28, 2016 at 10:27 am - Reply

    Thank it helped me alot

Leave A Comment