Scroll to top

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 a 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
For Eclipse 3.3 and below, enter
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 a 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 a template.


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. : Sysout has been added to identify the 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 a run configuration and run our test. To create run configuration go to Run >> Run Configuration. Here you will find TestNG option in the 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( select the same(TestNG.xml) using Browse button.


Click  Apply and Run.

The 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. 🙂

Author avatar


  1. Shrikant

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

  2. subha

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

  3. Saurabh

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

  4. Neelam

    Thank it helped me alot

Post a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.