Get started with TestNG

We wrote this post for testers who want to get started with TestNG. You will learn about some popular annotations used in TestNG. We also show you how to parameterize your automated checks.

TestNG is a testing framework influenced from JUnit and NUnit but has some great features which makes it more powerful and easier to use. Some basic knowledge of automated checking using Java and Selenium is a pre-requisite to follow this post. You should have Java, Eclipse and Selenium installed. We estimate this post will take you about 60 minutes to read and try out on your own.

Getting started with TestNG

1. Install TestNG plug-in for Eclipse
2. Some important TestNG annotations
3. Select an application to test
4. Writing test using TestNG framework
5. Running the test

STEP 1: Install 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.

Note: If you are wondering about the URL beust, Cédric Beust is the creator of the TestNG framework.

Install TestNG

Once you have installed TestNg, add it to your Project by going to your project Properties/Add Library and select TestNG Library which you added earlier

Add TestNG Library

STEP 2: Some important TestNG Annotations
There are many TestNG annotations. Below are a list of annotations which we will be using in our test

  • @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.
  • @AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
  • @BeforeMethod: The annotated method will be run before each test method.
  • @AfterMethod: The annotated method will be run after each test method.
  • @DataProvider: Marks 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.

STEP 3: Select an application to test
Since we love Chess here at Qxf2 Services, we will use to automate our test using TestNG framework. We will parameterize the login functionality and assert that the username is displayed properly once we Login.

STEP 4: Writing test using TestNG framework
We will write a Selenium test using TestNG framework to check the login functionality of using multiple id.

Test case to Parameterize the Login functionality of using TestNG annotation "@DataProvider".
Some other annotations like @BeforeTest to setup driver, @AfterTest to teardown the driver and @BeforeMethod to navigate to the test url are used.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Chess {
	public static WebDriver driver;
	public static void Test_Login(String Username, String Password) throws InterruptedException{
		String userId= driver.findElement(By.xpath("//div[@class='user-nick']/h2")).getText();
		Assert.assertEquals(userId, Username);
	@DataProvider //Supplies data for the test method
	public Object[][] LoginData() throws IOException{
		Object[][] data = new Object[3][2];
		//First row data
		//Second row data
		//Third row data
        return data;
	@BeforeMethod //Method will run before executing every test
	  public void launchChess() {
		// Launch the url
	 @BeforeTest //Method will run before any test method belonging to the class
	  public void setUp() {
		  //Initialize driver
		 driver= new FirefoxDriver();
		 //Implicit wait for 10 sec for element to load
		 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
	  @AfterTest //Method will run after executing all test method belonging to the class
	  public void tearDown() {
		  //Close all the driver Instance

STEP 5: Running the test
You can run the TestNG test by clicking RunAs and selecting TestNG Test. Once the Tests runs you can view the result as shown below
Run Test
In case you have multiple tests to be run, you can use testng.xml to run the Test. Below is a sample testng.xml which has one test name. You can create multiple tests and add it to testng.xml to run it. The “class name” in the testng.xml should be your “PackageName.ClassName”.

<!DOCTYPE suite SYSTEM "">
<suite name="My Sample Suite">
<test name="ChessTest">	
 <class name="Test.Chess" ></class>

There you have it. A crash course on getting started with TestNG. Happy automated checking!

4 thoughts on “Get started with TestNG

Leave a Reply

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