#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 →
Selenium Frameworks

TestNG introduction and Configuration with Eclipse

Installing TestNG

Install the TestNG plugin from the Eclipse Marketplace, which adds project templates, run configurations, and syntax support for TestNG annotations.

Creating a TestNG Class

public class LoginTest {
    @Test
    public void validLogin() {
        // WebDriver-based test logic
    }
}

Running and Configuring

Right-click the class and choose "Run As > TestNG Test," or create a testng.xml file to configure and run a broader suite of test classes together.

Adding TestNG Through Maven

The Eclipse plugin gives you menus and run configurations, but the TestNG library itself should be a project dependency so that the same tests also run on a build server. In a Maven project add this to pom.xml, using the latest stable version listed on Maven Central:

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

After saving, right-click the project and choose Maven, then Update Project, so Eclipse downloads the jar. When you write @Test, make sure the import is org.testng.annotations.Test and not the JUnit one; mixing them is a very common reason tests are silently skipped.

Annotation Order with a Worked Example

TestNG runs setup and teardown methods in a fixed order around each test. This class prints the sequence so you can see it in the Console view:

import org.testng.annotations.*;

public class LifecycleDemo {
    @BeforeClass  public void openBrowser()  { System.out.println("Before class"); }
    @BeforeMethod public void goToLogin()    { System.out.println("Before method"); }

    @Test(priority = 1)
    public void validLogin()   { System.out.println("Test: valid login"); }

    @Test(priority = 2, dependsOnMethods = "validLogin")
    public void logout()       { System.out.println("Test: logout"); }

    @AfterMethod  public void clearCookies() { System.out.println("After method"); }
    @AfterClass   public void closeBrowser() { System.out.println("After class"); }
}

The full order is @BeforeSuite, @BeforeTest, @BeforeClass, @BeforeMethod, the test, then the matching After annotations in reverse. Useful @Test attributes include priority, enabled = false, groups, timeOut and expectedExceptions.

A Practical testng.xml

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="RegressionSuite" parallel="classes" thread-count="2">
  <test name="LoginTests">
    <parameter name="browser" value="chrome"/>
    <groups>
      <run><include name="smoke"/></run>
    </groups>
    <classes>
      <class name="tests.LoginTest"/>
      <class name="tests.LifecycleDemo"/>
    </classes>
  </test>
</suite>

Right-click this file and choose Run As, then TestNG Suite. The parameter value reaches a method marked with @Parameters("browser"), which lets one suite file drive different browsers. Note that with the groups filter, only methods tagged groups = "smoke" will run. After a run, Eclipse shows a results tab, and HTML reports appear in the test-output folder (or under target when run through Maven).

Setup Problems and Fixes

  • No "Run As TestNG" option: the plugin is not installed or Eclipse needs a restart.
  • Tests not found: methods must be public, and class names in the XML must include the package.
  • Parallel tests interfere: a shared static WebDriver is the usual cause; give each thread its own driver.
  • Works in Eclipse but not in CI: point the Maven Surefire plugin at the same testng.xml.

A good habit for beginners is to keep one small suite file for smoke tests and a second one for the full regression run. You can then pick the right file from Eclipse during development, and the build server can run both at different stages. Keep test classes free of hard-coded browser names and URLs; pass them in through parameters or a config file so the same tests run anywhere without edits.

Ready to master real-world software testing?

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

Explore Course