#2 TestNG in Selenium

#2 TestNG in Selenium

Saki Pham


What is TestNG?
Why Use TestNG with Selenium?
Install and Set up TestNG
Introduce some TestNG annotations
Run Testcase with TestNG


What is TestNG?

This blog will provide basic concepts of TestNG.
TestNG is an open-source test automation framework for Java. In TestNG, NG stands for "Next Generation".TestNG is inspired by JUnit and NUnit but it overcomes the constraints and disadvantages of JUnit and introduces some new functionalities that make it more powerful and easier to use.

Why Use TestNG in Selenium?

By using TestNG in Selenium, you can generate a proper format for the test results. These following are some features of TestNG that will answer why TestNG is recommended to use in selenium.
+ TestNG can generate a proper report, and you can quickly know how many test cases are passed, failed, and skipped.
+ In TestNG, annotations are easier to understand.
+ Multiple test cases can be grouped more easily by converting them into a testng.xml file.
+ The same test case can be executed multiple times without loops.

Install and Set up TestNG

Install TestNG plugin from update site
Select Help > Install New Software
Enter the below URL in "Work with:" field.
Link: https://testng.org/testng-eclipse-update-site/6.14.3/
Make sure the TestNG check box is checked and click Next. Eclipse will then guide you through the process.

Check “Always trust all content” and click Trust Selected.
Restart Eclipse to finish installing.

■ Check TestNG plug-in is installed successfully
Select Eclipse > About Eclipse > Installation Details
At the Installed Software tab > Enter “TestNG” in the search textbox and installed TestNG will appear.

■ Download TestNG libraries in the link below
Link: https://mvnrepository.com/artifact/org.testng/testng/6.14.3

Create folder “testNGLibraries” and copy & paste downloaded TestNG (.jar file) to it.
Right-clicking on TestNG (.jar file) > Build Path > Add to Build Path.

Introduce some TestNG annotations

List of TestNG annotations and how these work.

■ Sample code with TestNG annotations
Let's understand through the sample code below:

package selenium.webdriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class testNG_Demo {
  public void Testcase_01() {
	  System.out.println("    Testcase_01 is executed.");
  public void Testcase_02() {
	  System.out.println("    Testcase_02 is executed.");
  public void beforeMethod() {
	  System.out.println("   BeforeMethod is executed.");
  public void afterMethod() {
	  System.out.println("   AfterMethod is executed.");
  public void beforeClass() {
	  System.out.println("  BeforeClass is executed.");
  public void afterClass() {
	  System.out.println("  AfterClass is executed.");
  public void beforeTest() {
	  System.out.println(" BeforeTest is executed.");
  public void afterTest() {
	  System.out.println(" AfterTest is executed.");
  public void beforeSuite() {
	  System.out.println("BeforeSuite is executed.");
  public void afterSuite() {
	  System.out.println("AfterSuite is executed.");

When the above code is executed, the output will appear in the sequence as shown below.

Run Testcase with TestNG

Now we will run the code in the previous blog’s example code with TestNG.
See the previous blog here: #1 - Selenium with Java - Basic settings
Create a new class named “RunTestcaseWithTestNG” and copy the below code.

package selenium.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class RunTestcaseWithTestNG {
	// Declaring driver variable
	WebDriver driver;
	public void beforeClass() {
		// get project path
		String projectPath = System.getProperty("user.dir");
		// setting the driver executable
		System.setProperty("webdriver.chrome.driver", projectPath + "/browserDrivers/chromedriver");
		// Initiating and opening chrome browser
		driver = new ChromeDriver();
	public void TestCase_01_Open_Blogs_Page() {
		// open browser with desired URL
	public void TestCase_02_Open_Testing_Blogs_Page() {
		// click to "Testing" link in header nav
	public void afterClass() {
		// closing the browser

Instead of running test script through main() method, now we use TestNG to run test script directly.

The code will be run in the orders below:

  1. beforeClass method is run first because it is annotated with @BeforeClass.
    After code is run, Chrome browser will be opened.
  2. Next, Below two method is annotated with @Test so these methods are treated as two test cases.
    After code is run, the browser navigating to blogs website of SHIFT ASIA
    After code is run, “Testing" link in the header menu is clicked and navigating to testing page.
  3. Finally, after all methods with @Test annotation are run, afterClass is run.
    After code is run, Chrome browser will be closed.

The result is the same when running the code in the previous blog’s example. But when using TestNG we can generate a proper report, and you can quickly know how many test cases are passed, failed, and skipped.
In the below output, we observe that all the test cases run successfully without any failure.

Run test script through a .xml file
Create a new class named “RunDemoTestNG.xml” and copy the below code.

<?xml version="1.0" encoding="UTF-8"?>
<suite parallel="false" name="Demo test Suite">
  <test name="Sample Test">
      <class name="selenium.webdriver.RunTestcaseWithTestNG"/>

Right-click on code area-> Run As-> TestNG Suite
(or Right click on the “RunDemoTestNG.xml”-> Run As->TestNG Suite)

Output is the same when we run test script directly in class test.


In this blog provides basic concepts of TestNG and know how to install and set up TestNG.
After that, we know how to run test scrip with TestNG directly or run through a .xml file.
Thank you for reading and hoping that helps you guys.