#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

TestNG Framework

What is TestNG?

TestNG ("Test Next Generation") is a Java testing framework inspired by JUnit, designed with more powerful features for organizing, configuring, and reporting on automated tests.

Key Capabilities

  • Flexible test configuration via annotations.
  • Grouping and prioritizing tests.
  • Data-driven testing via @DataProvider.
  • Parallel test execution.
  • Built-in HTML test reports.

Why It's Popular for API and UI Automation

TestNG integrates smoothly with tools like Rest Assured and Selenium, providing the structure (setup, teardown, assertions, reporting) needed to build a full automated test suite.

Annotation Order During a Run

TestNG calls configuration methods in a fixed order. Knowing it helps you put setup code in the right place.

AnnotationRunsTypical use
@BeforeSuiteOnce before the whole suiteLoad config, start reports
@BeforeTestBefore each <test> block in testng.xmlSet environment or base URI
@BeforeClassOnce per test classCreate shared objects
@BeforeMethodBefore every @TestOpen browser, reset data
@TestThe test itselfSteps and assertions
@AfterMethod / Class / Test / SuiteMirror of the above, in reverseClose browser, flush reports

A Test Class That Uses the Main Features

First, add the dependency to pom.xml, choosing the current stable version from Maven Central:

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>LATEST_STABLE_VERSION</version>
  <scope>test</scope>
</dependency>

Then a single class can show groups, dependencies, data providers and soft assertions:

public class CartTest {

    @BeforeMethod
    public void setUp() { /* open app, log in */ }

    @Test(groups = "smoke", priority = 1)
    public void addItem() {
        Assert.assertTrue(cart.add("pen"));
    }

    @Test(dependsOnMethods = "addItem")
    public void checkout() {
        Assert.assertEquals(cart.checkout(), "SUCCESS");
    }

    @DataProvider(name = "qty")
    public Object[][] qty() {
        return new Object[][] { {1, true}, {0, false}, {-2, false} };
    }

    @Test(dataProvider = "qty")
    public void quantityRules(int q, boolean valid) {
        Assert.assertEquals(cart.isValidQty(q), valid);
    }

    @Test
    public void pageDetails() {
        SoftAssert soft = new SoftAssert();
        soft.assertEquals(page.title(), "Cart");
        soft.assertTrue(page.hasTotal());
        soft.assertAll();          // report every failure together
    }
}

You can run this class from the IDE with the TestNG plugin, from the command line with mvn test once the Surefire plugin is configured, or through a testng.xml suite that picks groups such as smoke or regression. After every run, TestNG writes HTML and XML reports to the test-output or target/surefire-reports folder, and listeners can hook in to add screenshots or custom reports.

If addItem fails, TestNG marks checkout as skipped rather than failed, which makes the report easier to read. The data provider runs quantityRules three times, once per row.

TestNG or JUnit 5?

Both are mature and well supported. TestNG has long been popular in Selenium and Rest Assured projects because suite files, dependencies between tests and parallel settings are built in. JUnit 5 is the default in many Spring and plain Java projects and has its own extensions for parameterized and parallel tests. If your team already has a working framework in one of them, stay with it; switching rarely adds value.

Mistakes Seen in New TestNG Suites

  • Using priority to share state: priority only orders tests; use dependsOnMethods when one test truly needs another.
  • Forgetting assertAll(): a SoftAssert without it never fails the test.
  • Static fields in parallel runs: shared static drivers or variables cause random failures. Keep state per thread.
  • Too many dependencies: long chains mean one failure skips half the suite. Keep tests independent where you can.

Checklist: setup in the right annotation, one behaviour per test, groups for smoke and regression, data providers for repeated inputs, and listeners for screenshots and reports.

Ready to master real-world software testing?

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

Explore Course