TestNG Tutorial

TestNG Tutorial

TestNG Tutorial

In this TestNG tutorial, we will take a detailed look at the TestNG framework, its usage and its advantages.This tutorial is written for any automation testing professional, who would like to have a robust understanding of TestNG

TestNG is an open source automated testing framework; where NG means Next Generation. TestNG framework has been accepted widely replacing JUnit because of its powerful features.

TestNG was created by programmer called Cedric Beust. It is much more powerful and easier to do as it provides many more annotations, features like:

  • Parameterization
  • Test reporting that provides effective test result format (HTML Reports)  that can be readily shared with stakeholders or client
  • Dependency testing,
  • Parallel execution,
  • Prioritisation
  • Test cases can be grouped
  • Support for Data Driven Testing using Data-providers

In a nutshell, it designed to cover different types & levels of testing like Unit testing, regression, end-to-end testing, etc.

In this TestNG tutorial, we are going to take a look at annotations, using TestNG, setting up priority in TestNG with examples.

Annotations in TestNG

TestNG makes use of annotations to specify the stages of execution of test cases. JVM (Java run time) identifies these annotations and takes appropriate action.


public void closewebapp()




@AfterTest is a TestNG annotation, which indicates to the runtime engine about the action (s) to be taken after all the test cases are executed. In this case, a method namely closewebapp() will be called.

This provides a lot of flexibility in the execution of test cases. Complete list of TestNG annotations is provided below:

TestNG Annotations

Writing Tests using TestNG framwork

Writing a test using TestNG is a 3-step procedure as shown below:

Using TestNG Framework

I will provide a step by step description of the steps to be followed to use TestNG.

Step 1

Create a new Java project with name “MyFirstTestNGProject” in Eclipse as shown below:

TestNG Tutorial Step 1

TestNG Tutorial Step 2

Click on Next and Click Finish button.

Step 2

Create a new package – Right click on the project name and select new package. Provide a package name “unitTest” and click on Finish button. This will add package under your project “MyFirstTestNGProject” as shown below:

TestNG Tutorial Step 3

Your project and package will be displayed in eclipse project explorer window as below –

TestNG Tutorial Step 4

Step 3

In this step, we will add TestNG library to our project. First of all, Add TestNG library to your project. Go to Project -> Properties -> Java Build Path

Click Add Library button -> Select “TestNG” from Add Library

Click next and finish.

TestNG Tutorial Step 5

TestNG Tutorial Step 6

Once you click on ‘Finish’, you will see the following screen:

TestNG Tutorial Step 7

Click on ‘Ok’ button.

Step 4

In this step, we are going to create a class. To do so, just follow the steps stated below:

Right Click on the package “unitTest” and go to New -> Other -> TestNG as shown below:

TestNG Tutorial Step 8











Clicking on ‘Other…’ will open the wizard window as shown below:


TestNG Tutorial Step 9










Click on Next button and add class name “HomePage” on the screen which appears as below:

TestNG Tutorial Step 10










Click on Finish button. This will add new class in your package under your project as shown below:-

TestNG Tutorial Step 11





Note that by default @Test annotation appears and a method with name “f” .This is basic structure of TestNG class and later we can modify it as per need.

Step 5

In this step, we will start writing the test script for verifying title of ‘Google’.

We will be demonstrating usage of the following annotations –

@BeforeMethod : Annotates methods will run before each test method.

@AfterMethod:  Annotates methods will run after each test method.

@Test: Marks the class or method as part of the test .Once we mention @Test annotation we do not need to write main method in our code.

First, we start writing our own code by renaming the method under @Test annotation as “verifyTitle”.

And include 2 more methods namely “startingUp” and “wrapUp” to initiate and close test under @BeforeTest and @AfterTest annotations however we observe red cross marks at left side of the code window as below:

TestNG Tutorial Step 12









The red cross X is shown because we have not added the right libraries. To do so, we need to just click over the red cross & click on the import packages for before test and after test annotations that shows as possible solutions in eclipse:

TestNG Tutorial Step 13







Once done these two lines of code will be added to your code:

TestNG Tutorial Step 14



Inside “startingUp” method we will include startup activities like setting system property for webdriver, initializing it to chromedriver , navigating to URL and maximizing the window.

Also note we declare the webDriver at class level –

TestNG Tutorial Step 15








Inside wrapUp method we will mention closing activities for test such as closing or quitting the browser:

TestNG Tutorial Step 16




And finally we will write the logic to verify the page title and assert it to true or false inside “verifyTitle” method under @Test annotation:

TestNG Tutorial Step 17




Now, your code should look as follows:

package unitTest;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

public class HomePage {

WebDriver driver;

//@BeforeMethod defines this method has to run before every @Test methods


public void startingUp()


System.setProperty(“webdriver.chrome.driver”, “D:/chromedriver.exe”);

driver = new ChromeDriver();





public void verifyTitle()


String title = driver.getTitle();

System.out.println(“Current title is : ” +title);

Assert.assertEquals(title, “Google”);


//@AfterMethod defines this method has to run after every @Test methods


public void wrapUp()





Step 6

Run the test script as TestNG. Right click on the “HomePage” Class -> Run As -> TestNG Test:

TestNG Tutorial Step 18











Step 7

In case it fails or it is still a Java class, first you need to convert it to TestNG Class as shown below:

TestNG Tutorial Step 19












It will prompt you for refactoring the class in order to generate testng.xml file as below:

TestNG Tutorial Step 20













Click Finish and you will observe testng.xml file has been added to your project:

TestNG Tutorial Step 21





Double click on testng.xml to open the file and click on source tab:

TestNG Tutorial Step 22









Now Right Click on this xml file and Run As -> TestNG Suite:

TestNG Tutorial Step 23










The output of this test case run will be as shown below:

TestNG Tutorial Step 24

Click on the T Report view to view the results of running the class “HomePage”:

TestNG Tutorial Step 25

Step 8

To view the test execution HTML Report generated by TestNG

Right Click on project folder “MyFirstTestNGProject” and click Refresh.Folder with name as “test-output” will be created under your project:

TestNG Tutorial Step 26











This is how your project explorer window in eclipse would look like:

TestNG Tutorial Step 27





Expand test-output folder and double click on emailable-report.html:

TestNG Tutorial Step 28


Report will look like this –

(Clicked on Passed methods to show up in right hand side test result pane)

What’s Next?

This was a detailed step-by-step TestNG tutorial. You can go and read more Selenium tutorials published on this blog.

Become an Automation tester with Selenium?

Techcanvass offers Automation Testing and Business Analysis certification courses. We are an IIBA endorsed education provider (EEP), iSQI Germany Authorized Training as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

One of the key features of our courses is the Job readiness package, as shown below:

CP-SAT Certification Training