Built.io joins Software AG! Read all about the news.

Built.io Blog

What is TestNG?


TestNG is a Java testing framework, inspired by JUnit and NUnit. It overcomes the limitations and drawbacks of JUnit and introduces a whole new set of features, making TestNG more powerful and user-friendly. The NG stands for Next Generation, signifying the new functionalities that TestNG brings to the table. From simple unit testing to complex integrated testing, it is designed to simplify all your testing requirements such as functional testing, regression, end-to-end, and more.


Before we learn how to use TestNG, let’s take a look at some of the advantages of using this testing framework:

  • Simplified annotations  |  Annotations have been simplified, making it easier for testers to understand them.
  • Results in HTML | It generates reports in HTML.
  • Multiple test-cases at once | Using TestNG Suite (testing.xml), which is essentially an xml file, testers can execute a number of test cases at a time. In this xml file, users need to mention the number of classes and test-cases they wish to execute.
  • Runs failed test-cases | TestNG can also be used to run failed test-cases. This is one of most important advantages of TestNG over JUnit.
  • Allows grouping | TestNG allows supports grouping. Using this feature, testers can group test-cases without too much effort, which was not possible with JUnit.
  • Allows running on multiple browsers | TestNG allows testers to execute and run one script in multiple browsers.
  • Parametric testing | Most of the time, testers have to execute a large number of varied tests, mainly due to the nature of their business logic. However, this lengthy process can be eliminated as parametric testing allows you to run the same test a number of times by simply changing the values. It allows you to pass parameters to your test methods in two ways: parameter and @dataprovider.
  • Bypassing or ignoring test-cases | This feature is useful if you do not want execute a particular testcase(s). In such instances, TestNG, with the help of annotation @Test(enabled = false), allows you to disable or bypass the particular test-case(s).
  • Reporter class (it generates logs | In TestNG, with the help of the reporter class, users can log messages for the test. Let’s say you are running a test-case, and you want to log related information. This could be just surface or in-depth information, depending on what you want.
  • Expected exceptions | It allows you to trace the exception handling of the code. While writing a code, there may be situations where testers want to verify if an exception is being presented when executed. This method will give you the details of the exceptions that are expected to be presented by that particular method. Use this method with @Test annotation.
  • Dependent On Method | TestNG supports the ‘dependence’ method. We can have dependence as an attriubute of a method, for example, if one method is dependent on another. This is not available in JUnit.
  • TestCase priority | We can execute the test cases in a particular order. To accomplish this, we define the order in @Test Annotation.

Advantages of TestNG Over JUNIT

  • TestNG supports group concept, while JUnit does not support this.
  • Parameter Testing is possible in TestNG, but it cannot be done in JUnit.
  • TestNG provides special annotations, such as @Before/AfterSuite and @Before/AfterTest.
  • In TestNG, it is possible to change the method name. JUnit, however, does not allow this.
  • TestNG supports the ‘dependence’ method. This is not available in JUnit.

Steps to installing TestNG for a project

  • Go to eclipse.
  • Click on Help.
  • Select Eclipse Marketplace.
  • In Find section, search for TestNG.
  • Install TestNG for Eclipse.
  • Right-click on your project, navigate to Build Path, and then click on Add Libraries.
  • Select TestNG and click on next, and then finish.

Basic Annotations and Execution process

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 Aanotations {
    // test case 1
    public void testCase1() {

    // test case 2
    public void testCase2() {

    public void beforeMethod() {

    public void afterMethod() {

    public void beforeClass() {

    public void afterClass() {

    public void beforeTest() {

    public void afterTest() {

    public void beforeSuite() {

    public void afterSuite() {

How To Run Suite (.xml file)

  1. Open Eclipse
  2. Right click on Testng.xml → Runas → TestNG suiteORType the following command in the command prompt: java -cp bin;jars/* org.testng.TestNG testng.xml

When we run the testng.xml file, we will get the following output:













PASSED: testCase1

PASSED: testCase2

Note: Consider every @test as a testcase.

Using TestNG you can perform all types of tests by simply integrating it with Jenkins, Ant, and Maven. By using Ant with TestNG you can convert TestNG reports into XSLT reports. Performing parallel tests in two different machines together is possible by integrating TestNG with selenium grid. Moreover, TestNG’s flexibility makes it the best choice for large test suites.

Popular Posts

Subscribe to our blog