Getting Started with TestNG (Next Generation)

TestNG (Next Generation) is a testing framework that is influenced by both JUnit and NUnit, but has more powerful features and is easier to use. It is designed with a good set of capabilities that simplify testing requirements ranging from functional, regression, end-to-end, and more.

You should have prior knowledge of both Java and Eclipse in order to start using TestNG to its fullest potential.

Getting Started with TestNG

Step 1: Install the TestNG plug-in for Eclipse

If your Eclipse does not have an inbuilt TestNG plug-in, you can install it from Eclipse IDE. Select Help / Install New Software updates. Search for TestNg or directly enter the URL mentioned below.

For Eclipse 3.4 and above, enter

For Eclipse 3.3 and below, enter

Eclipse will then guide you through the rest of the process.

Once installation is complete, you can verify the TestNG is installed properly or not.


Step 2: TestNG Annotations

TestNG has given a set of annotations that allows to perform some Java logic before or after a certain point.

Here is a list of annotations that TestNG supports:

@BeforeSuiteThe annotated method will be run only once before all tests in this suite have run.
@AfterSuiteThe annotated method will be run only once after all tests in this suite have run.
@BeforeClassThe annotated method will be run only once before the first test method in the current class is invoked.
@AfterClassThe annotated method will be run only once after all the test methods in the current class have run.
@BeforeTestThe annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTestThe annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@BeforeGroupsThe list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroupsThe list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeMethodThe annotated method will be run before each test method.
@AfterMethodThe annotated method will be run after each test method.
@TestMarks a class or a method as a part of the test.
@DataProviderMarks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
@ListenersDefines listeners on a test class.
@ParametersDescribes how to pass parameters to a @Test method.

Step 3: Conduct your First Test using Annotations

Your first test should be conducted using these annotations. Try out a few and see if they are completing the task they need to complete.

package testng;

import org.testng.annotations.Test;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.AfterMethod;

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 Annotations


// test case 1


public void testCase1()

 {  System.out.println("@test1");  }

// test case 2


public void testCase2()

{ System.out.println("@test2"); }


public void beforeMethod()

{ System.out.println("@beforeMethod"); }


public void afterMethod()

{ System.out.println("@afterMethod"); }


public void beforeClass()

{ System.out.println("@beforeClass"); }  


public void afterClass()

{ System.out.println("@afterClass"); }


public void beforeTest()

{ System.out.println("@beforeTest"); }


public void afterTest()

{ System.out.println("@afterTest"); }


public void beforeSuite()

{ System.out.println("@beforeSuite"); }


Step 4: Invoke the TestNG Suite File – TestNG.XML

You can invoke TestNG in several different ways:

  1. With a testing.xml file
  2. With a build tool
  3. From the command line

Here, we will cover the invocation from testing.xml.

Below is the format of the testing.xml file:

<!DOCTYPE suite SYSTEM "" >

<suite name="Suite1" verbose="1" >

<test name="TestNGFirstTest" >


<class name=" TestngAnnotation " />




You have an option to specify package names instead of class names:

<!DOCTYPE suite SYSTEM "" >

<suite name="Suite1" verbose="1" >

<test name="Regression1"   >


<package name=" testng" />




STEP 5: TestNG Parameter Test (XML and @DataProvider)

Test methods can have parameters.  You may use any number of parameters on each of your test method, and you can instruct TestNG to pass the correct parameters with the @Parameters annotation. In the case of complex objects, you can use @DataProvider annotation.

5.1 Parameter Test with XML

This feature is used to initialize fields inside your classes with values that will then be used by your test methods.

Parameters passed in test methods are initialized with values specified in testng.xml. 

@Parameters({ "first-name" })

@Test public void parameterTest(String firstName){

System.out.println("Parameterised Value is : " + firstName);

assert "Neha".equals(firstName);


In this code, we specify that the parameter firstName of your Java method should receive the value of the XML parameter called first-name. 

This XML parameter is defined in testng.xml as below:

<!DOCTYPE suite SYSTEM "" >

<suite name="Suite1" >

<test name="TestNGFirstTest" >

<parameter name = “first-name” value = “Neha/>”


<class name=" parameterTest " />




5.2 Parameter Test with @DataProvider

There is another way to parameterize the test methods if you need to pass complex parameters.

A Data Provider is a method on your class that returns an array of array of objects.  This method is annotated with @DataProvider.

//This method will provide data to any test method that declares that its Data Provider is named "test1"

@DataProvider(name = "test1")

public Object[][] createData1() {

return new Object[][] {

{ "FName1", new Integer(36) },

{ "FName2", new Integer(37)},



//This test method declares that its data should be supplied by the Data Provider named "test1"

@Test(dataProvider = "test1")

public void verifyData1(String n1, Integer n2) {

System.out.println(n1 + " " + n2);


@Test method specifies its Data Provider with the dataProvider attribute.  This name must correspond to a method on the same class annotated with @DataProvider(name="...") with a matching name.

By default, the data provider will be searched for in the current test class or one of its base classes. If you want to put your data provider in a different class, your code should be:

public class StaticProvider {

@DataProvider(name = "test1")

public Object[][] createData1() {

return new Object[][] {

{ "FName1", new Integer(36) },

{ "FName2", new Integer(37)},




public class MyTest {

@Test(dataProvider = " test1", dataProviderClass = StaticProvider.class)

public void test(Integer n) {

// ...



STEP 6: Dependencies with annotations

You can use the attributes dependsOnMethods or dependsOnGroups, found on the @Test annotation.

// In this example, method1() is declared as depending on method method2(), which guarantees that method2() will always be invoked first.


public void method2() {}

@Test(dependsOnMethods = { "method2" })

public void method1() {}

You can also have methods that depend on entire groups:

//This example shows multiple groups and dependencies on group

@Test(groups = { "Group1" })

public void method2() {}

@Test(groups = { " Group2" })

public void method3() {}

@Test(dependsOnGroups = { " Group1" })

public void method1() {}

@Test(dependsOnGroups = { " Group2" })

public void method3() {}

@Test(dependsOnGroups = { " Group1" , “Group2”})

public void method4() {}

Below you can find the testng.xml to run the test with group dependencies

<!DOCTYPE suite SYSTEM "" >

<suite name="Suite1”>

<test name="Test1">



<include name="Group1"/>




<class name=" GroupTestClass " />



Neha Aggarwal

Neha Aggarwal

QA Automation Lead

Neha Aggarwal is a QA Automation Lead for 3Pillar Global. She has over 8 years of experience in software testing, with a focus on automation testing in several domains: telecom, financial lending, and the energy. She also has hands-on experience working in Automation framework development using Selenium Web Driver, SOAPUI as well as project planning and delivery.

Leave a Reply