Why is TestNG Awesome? Advantages of Integrating it with Selenium

Basically, TestNG is a user – friendly automation framework that overcomes the drawbacks and limitations of Java Unit, thus introducing the entire new set of features which makes it more powerful and useful.

This automated testing framework helps in elimination of numerous limitation of the older framework. It facilitates the developer the ability to write more flexible and powerful tests.
TestNG Framework

  • TestNG is a testing framework designed for unit testing. Today it is used for every kind of testing.
  • Initially, it is developed to simplify a broad range of testing, no matter from system testing or Unit testing.
  • It is an open source framework which is inspired from the Java platform (JUnit) and NET platform (NUnit).
  • It can be used for any testing, be it API testing or UI testing.
  • It is introduced as a brand new automated framework whose functionalities make it more powerful and easier to use.

Features of TestNG

  • Maintains flexible runtime configuration.
  • Guards easy going plug-in API.
  • Supports Multi threaded testing.
  • Upholds parallel testing, group testing, partial testing and load testing.
  • Supports annotations of JUnit and have some more annotations to make testing easier.
  • Separates compile-time test code from run-time configuration/data info.
  • Supports testing integrated classes.
  • TestNG supports the ‘dependence’ method which is not available in JUnit.

How to Download or Install TestNG for a project
Before performing this step by step process, do not forget to visit these prerequisites to install selenium IDE.
In order to download TestNG plug-in, all you need to have is a selenium IDE and Active internet connection. But, if selenium IDE is already installed in your system, then follow these simple steps:
Step 1: Download and install the Java Software Development Kit (JDK)
Step 2: Download “Eclipse IDE for Java Developers”. You choose 32 bit or 64 bit as per your requirement.
Step 3: You can download the Selenium Java Client Driver. There would be different languages, choose only the Java from the options.
Step 4: Configure Eclipse IDE with Web Driver.
TestNG Basic Annotations
Annotations are a new feature in Java provided by JDK 5.0 version. Annotations can be used to describe metadata in java programs. To describe any information about the coding part, annotations are used. Following is the list of annotations usually used in TestNG:

Sr. No. Annotation Description
1. @BeforeSuite This annotation will always be executed before all tests are run inside a TestNG.
2. @AfterSuite This annotation will always be executed after all tests are run inside a TestNG.
3. @BeforeClass This annotation will always be executed before the first test method in a class is called on.
4. @AfterClass This annotation will always be executed after the first test method in a class is called on.
5. @BeforeTest Executed before any test method associated with the class inside <test> tag is run.
6. @AfterTest This annotation will always be executed before any test method associated with the class inside <test> tag is run.
7. @BeforeGroups This annotation will always be executed before the first test method associated with these groups is called on.
8. @Aftergroups This annotation will always be executed shortly after the first test method associated with these groups is called on.
9. @BeforeMethod This annotation will always be executed before each test method.
10. @AfterMethod This annotation will always be executed after each test method.
11. @DataProvider This annotation must return object [ ], it marks a method as supplying data for a test method.
12. @Factory This annotation will return the object and marks a method as a factory that will be used by TestNG as test classes.
13. @Listeners This annotation defines the listeners on a test class.
14. @parameters This annotation describes how parameters can be passed to a test method.
15. @Test This annotation marks a class as a part of the test.

Execution Procedure of Methods in TestNG with Annotations

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 TestngAnnotation {
// Case 1
public void testCase1() {
System.out.println(“Case I”);
// Case 2
public void testCase1() {
System.out.println(“Case II”);
public void beforeMethod() {
System.out.println(“Before Method Annotation”);
public void afterMethod() {
System.out.println(“After Method Annotation”);
public void beforeClass() {
System.out.println(“Before Class Annotation”);
public void afterClass() {
System.out.println(“After Class Annotation”);
public void beforeTest() {
System.out.println(“Before Test Annotation”);
public void afterTest() {
System.out.println(“After Test Annotation”);
public void beforeSuite() {
System.out.println(“Before Suite Annotation”);
public void afterSuite() {
System.out.println(“After Suite Annotation”);
Creating XML file in C:\>TestNG for executing annotations
<?xml version = “1.0” encoding = “UTF-8”?>
<!DOCTYPE suite SYSTEM “http://demo” >
<suite name = “Suite1”>
<test name = “test1”>
<class name = “Annotation”/>
Test case compilation using javac
C:\TestNG>javac Annotation.java
Before Suite Annotation
Before Test Annotation
Before Class Annotation
Before Method Annotation
Case I
After Method Annotation
Before Method Annotation
Case II
After Method Annotation
After Class Annotation
After Test Annotation
After Suite Annotation
Total tests run: 1, Failures: 0, Skips: 0

Why use TestNG with Selenium?
Selenium tests alone cannot generate a proper format for the test results. However, with the help of TestNG, you can generate the proper format for the test results.

Read also :  10 Best Automation Testing Tools of 2018

TestNG is widely used by other selenium users because of its features and feasibility. Moreover, it is associated with several advantages through which a user can be very comfortable to leverage TestNG.
Following are the points why you need to use TestNG with selenium:

  • You don’t have to write a single line of code to generate a proper format for the test results as they are generated by default. The report usually includes a number of test cases runs or a number of test cases that failed or passed.
  • If you want to control the test executions, then you have access to multiple annotations which can control how the test can be run.
  • You can run your tests in arbitrarily big threads pools. You can simply group multiple test cases and convert them into an XML file and then set the priority of test case in a specific order.
  • It provides easy and flexible test configuration in which you don’t have to write a code to modify any test methods.
  • Using it you can create data-driven frameworks. You can use the @DataProvider annotation to support data-driven testing.
  • If you want to run your tests based on the parameters, TestNG supports parameter feature which will make your test cases or test suite very flexible. You can easily pass the parameter at runtime so that based on the requirement you can pass the parameter and you don’t have to modify your existing test.
  • If you have multiple classes to execute in the test suite, you can directly right click and run the complete test suite.
  • It is supported by a variety of tools and plugins. Also, it supports multiple IDE such as Webstorm, IntelliJ or NetBeans.
  • You can simply group the test cases with the use of TestNG. Suppose you have four categories of smoke, recreation, functional and non-functional. You can group all of these test cases and can run them individually.

Advantages of TestNG over JUnit                     
app testing

  • It gives an option to run out same test case with multiple sets of test data.
  • It provides default reports which are not provided if you are using JUnit.
  • It has a higher level of customization which enables the user to customize test case execution so that the user can run his script when something happens.
  • Test cases can also be grouped easily as compared to JUnit.
  • The annotations used in TestNG are very simple and easy to understand.