DEV Community

Arvind Choudhary
Arvind Choudhary

Posted on

TestNG testing framework - Introduction

TestNG logo

What is a Testing Framework?

A testing framework is a set of guidelines or rules used for creating and designing test cases. A framework is comprised of a combination of practices and tools that are designed to help QA professionals test more efficiently.

Popular available options:

  1. JUnit
  2. NUnit
  3. TestNG

Why TestNG?

TestNG is a testing framework inspired by JUnit and NUnit but introduces some new functionalities that make it more powerful and easier to use, such as:

  • Annotations.
  • Run your tests in arbitrarily big thread pools with various policies available (all methods in their thread, one thread per test class, etc…).
  • Test that your code is multithread safe.
  • Flexible test configuration.
  • Support for data-driven testing (with @DataProvider).
  • Support for parameters.
  • Powerful execution model (no more TestSuite).
  • Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc…).
  • Embeds BeanShell for further flexibility.
  • Default JDK functions for runtime and logging (no dependencies).
  • Dependent methods for application server testing. TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc…

Annotations in TestNG



Assertions in TestNG are a way to verify that the expected result and the actual result match or not.

  • Expected Result: expected outcome at a point in time while testing application.
  • Actual Result: actual outcome at a point in time while testing application.


Enter fullscreen mode Exit fullscreen mode

Types Of Assertions

Hard Assertion: Hard Asserts are those asserts that stop the test execution when an assert statement fails, and the subsequent assert statements are therefore not validated.
Soft Assertions: In soft asserts, the subsequent assertions keep on running even though one assert validation fails, i.e., the test execution does not stop.


# Hard Assert
# Soft Assert
SoftAssert softassert = new SoftAssert();
softassert.assertEquals(actualResult, expecetedResult);
Enter fullscreen mode Exit fullscreen mode

Example Usage


Enter fullscreen mode Exit fullscreen mode


only @test annotated methods are called test should hold test scripts, whereas @BeforeMethod/@AfterMethod methods should be used for setting up prerequisites & cleaning up for each tests.

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Runner {
    public void beforeTest(){
        System.out.println("method to be executed before test");

    public void test(){
        System.out.println("test method");

    public void afterTest(){
        System.out.println("method to be executed after test");

Enter fullscreen mode Exit fullscreen mode


method to be executed before test
test method
method to be executed after test
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
Enter fullscreen mode Exit fullscreen mode

Few useful URLs:

TestNG webpage
Javatpoint tutorial

Top comments (0)