Skip to content

TestNG Framework Tutorial

TestNG( Next Generation)  is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. The major idea and design goal of TestNG is to cover a wider range of testing categories: unit, functional, end-to-end, etc., with more powerful and easy-to-use functionalities.

It builds on the strengths of these frameworks while introducing additional features such as flexible test configuration, powerful annotations, and the ability to run tests in parallel, making it suitable for a wide range of testing requirements.

TestNG vs JUnit

The basic comparison of TestNG and JUnit framework are as follows:

 

 
JUnit TestNG
Annotations Basic Rich and flexible
Dependency Management Not supported Fully supported
Parallel Execution Limited Comprehensive
Built-in Reporting No Yes

Supported IDEs

TestNG is supported via plug-ins by each of the major IDEs

  • Eclipse
  • IntelliJ IDEA
  • Apache NetBeans.

TestNG supports various types of testing, including unit, integration, functional, and end-to-end testing.

The Maven build system supports TestNG. Jenkins continuous integration server has built-in support for TestNG and can track and chart test results over time. Most Java code coverage tools, such as Cobertura, work seamlessly with TestNG. TestNG is widely used in Web automation using Selenium Webdriver.

Setting Up TestNG

Prerequisites

  • Java Development Kit (JDK): Download and install the latest JDK from Oracle.
  • IDE: Install a Java IDE like Eclipse or IntelliJ IDEA.
  • TestNG Plugin:
    In Eclipse: Go to Help -> Eclipse Marketplace, search for TestNG, and install it.
    In IntelliJ IDEA: TestNG is built-in; you only need to add it as a library.

Install the TestNG

Download TestNG

TestNG Basics

TestNG Features

TDD Cycle

TestNG Annotations

TestNG annotations control the flow and behavior of test execution. These annotations are essential for writing flexible and efficient test cases.

Advantages of Using Annotations

  • Simplifies test configuration and setup.
  • Allows modular execution with controlled dependencies.
  • Enhances readability and maintainability of test scripts.

First TestNG Test

 

Enable/Disable Test

 

Testing Exceptions

Parameters from testng.xml

TestNG Groups

TestNG Dependencies

@DataProvider Annotation

 

TestNG Assert Class ( Hard Assert )

A Hard Assert means:

  • If the assertion fails, the test method stops execution immediately.

  • Remaining code in that test will not execute.

Common Methods in TestNG Assert class:

Method Description
Assert.assertEquals(actual, expected) Checks if values are equal
Assert.assertNotEquals(actual, expected) Checks values are not equal
Assert.assertTrue(condition) Passes if condition is true
Assert.assertFalse(condition) Passes if condition is false
Assert.assertNull(object) Passes if object is null
Assert.assertNotNull(object) Passes if object is not null
Assert.fail() Forces test failure

SoftAssert

 

Hard Assert Soft Assert
Stops test immediately Continues execution
Uses Assert class Uses SoftAssert class
No assertAll() needed Requires assertAll()

Custom Asserts

TestNG Listeners

TestNG Listeners provide hooks to capture test events.

Take a screenshot when Assert fails

Run TestNG tests programatically