You are currently viewing Tutorial on TestNG with Examples

Tutorial on TestNG with Examples

  • Post author:
  • Post category:TestNG
  • Post comments:0 Comments
  • Post last modified:February 24, 2024

1. Installation

TestNG can be easily added to your project using Maven or Gradle. For Maven, add the following dependency to your pom.xml:


For Gradle, add this to your build.gradle:

dependencies {
    testImplementation 'org.testng:testng:7.4.0'

2. Creating TestNG Test Cases

Let’s create a simple test case to demonstrate TestNG’s features.

import org.testng.annotations.Test;

public class TestNGExample {

    public void testAddition() {
        int result = Calculator.add(3, 5);
        Assert.assertEquals(result, 8, "Addition is incorrect");

3. Annotations

1. @Test

The @Test annotation is used to mark a method as a test method.

public void testMethod() {
    // Test code goes here

2. @BeforeMethod and @AfterMethod

These annotations are used to perform setup and teardown operations before and after each test method.

public void setup() {
    // Setup operations

public void teardown() {
    // Teardown operations

3. @BeforeClass and @AfterClass

These annotations are used to perform setup and teardown operations before and after the entire test class.

public void beforeClass() {
    // Setup operations before the class

public void afterClass() {
    // Teardown operations after the class

4. @BeforeSuite and @AfterSuite

These annotations are used to perform setup and teardown operations before and after the entire suite of tests.

public void beforeSuite() {
    // Setup operations before the suite

public void afterSuite() {
    // Teardown operations after the suite

4. Assertions

TestNG provides a set of assertion methods to validate expected results.

import org.testng.Assert;

Assert.assertEquals(actual, expected);

5. TestNG XML Configuration

TestNG allows you to configure tests using an XML file.

<!DOCTYPE suite SYSTEM "">
<suite name="MyTestSuite">
    <test name="MyTest">
            <class name="com.example.TestNGExample"/>

6. Parameterized Tests

TestNG supports parameterized tests using the @Parameters annotation.

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParameterizedTest {

    @Parameters({"param1", "param2"})
    public void testMethod(String param1, int param2) {
        // Test code with parameters

7. Groups

You can group test methods and include or exclude them during test runs.

@Test(groups = {"group1"})
public void testMethod1() {
    // Test code

@Test(groups = {"group2"})
public void testMethod2() {
    // Test code

8. Dependencies

Define dependencies between test methods using the dependsOnMethods attribute.

public void loginTest() {
    // Login test

@Test(dependsOnMethods = {"loginTest"})
public void dashboardTest() {
    // Dashboard test

9. Listeners

Listeners in TestNG allow you to customize the test execution process.

import org.testng.ITestListener;
import org.testng.ITestResult;

public class CustomListener implements ITestListener {

    public void onTestStart(ITestResult result) {
        System.out.println("Test started: " + result.getName());

    public void onTestSuccess(ITestResult result) {
        System.out.println("Test passed: " + result.getName());

    public void onTestFailure(ITestResult result) {
        System.out.println("Test failed: " + result.getName());

    // Other listener methods

To use this listener, add it to your test class:

public class MyTestClass {
    // Test methods

This concludes our basic tutorial on TestNG with examples. TestNG offers a wide range of features to make testing easier and more organized, making it a popular choice among developers for writing test cases in Java applications.

Leave a Reply