#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
TestNG & Maven

What is TestNG

Overview

TestNG is an open-source testing framework for Java, built to overcome limitations of older frameworks like JUnit, with richer annotations and configuration options.

Core Features

  • Annotation-based test lifecycle control (setup, execution, teardown).
  • Support for grouping related tests together.
  • A configuration file (testng.xml) to control which tests run and how.
  • Built-in assertions and detailed pass/fail reporting.

Typical Use

Testers write test methods annotated with @Test, organize them into suites via testng.xml, and run the suite to get a structured report of results.

Annotations and the Order They Run In

TestNG decides when each method runs based on its annotation. Knowing the order helps you place setup and cleanup code at the right level:

  1. @BeforeSuite: once, before anything in the suite, for example starting a report.
  2. @BeforeTest: before each <test> block in testng.xml.
  3. @BeforeClass: once per test class, for example launching a browser.
  4. @BeforeMethod: before every single test method, for example opening the login page.
  5. @Test: the test itself.
  6. @AfterMethod, @AfterClass, @AfterTest and @AfterSuite mirror the setup steps in reverse.

A test class using groups, priorities and a data provider

import org.testng.Assert;
import org.testng.annotations.*;

public class LoginTest {

    @BeforeClass
    public void openBrowser() {
        System.out.println("Launch browser");
    }

    @Test(priority = 1, groups = "smoke")
    public void validLoginShowsDashboard() {
        String title = login("admin", "correct-password");
        Assert.assertEquals(title, "Dashboard", "Page title after login");
    }

    @Test(priority = 2, groups = "regression", dataProvider = "badLogins")
    public void invalidLoginShowsError(String user, String pass) {
        String title = login(user, pass);
        Assert.assertEquals(title, "Login", "User should stay on the login page");
    }

    @DataProvider(name = "badLogins")
    public Object[][] badLogins() {
        return new Object[][] {
            {"admin", "wrong"},
            {"", "secret"},
            {"unknown", "admin"}
        };
    }

    @AfterClass
    public void closeBrowser() {
        System.out.println("Close browser");
    }

    private String login(String user, String pass) {
        // In a real project this would drive the UI through a page object
        return "admin".equals(user) && "correct-password".equals(pass) ? "Dashboard" : "Login";
    }
}

The data provider feeds three rows into one test method, so TestNG reports three separate results. Adding a new invalid case is just one more line of data.

Controlling Runs with testng.xml

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Web Suite" parallel="classes" thread-count="2">
  <test name="Smoke checks">
    <groups>
      <run>
        <include name="smoke"/>
      </run>
    </groups>
    <classes>
      <class name="tests.LoginTest"/>
      <class name="tests.SearchTest"/>
    </classes>
  </test>
</suite>

This file runs only tests in the smoke group and runs two classes at the same time. Switching the include to regression gives a full run without touching any Java code. With Maven, the Surefire plugin can point to this file so mvn test runs the same suite on a CI server.

Hard Asserts, Soft Asserts and Common Pitfalls

A normal Assert stops the test at the first failure. A SoftAssert records failures and keeps going, which is handy for checking many fields on one page. You must call softAssert.assertAll() at the end; without it, the test passes even when checks failed.

  • Using priority to express dependency. Priority only sets order. If test B needs test A to succeed, use dependsOnMethods so B is skipped, not failed, when A breaks.
  • Sharing one WebDriver in parallel runs. Threads will fight over the same browser. Keep a driver per thread, often with ThreadLocal.
  • Vague assertion messages. Add the third message argument so reports explain what was expected.
  • Heavy setup in @BeforeMethod. Logging in before every test is slow; move shared setup to class level when tests do not change that state.
  • Ignoring the reports. TestNG writes HTML and XML results into the output folder after each run. Check failed and skipped counts there, and rerun only the failures using the generated testng-failed.xml file.

JUnit 5 now covers many of the same needs. TestNG remains popular in Selenium projects because of its suite XML, groups, dependencies and built-in parallel options.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course