TestNG Interview Questions & Answers


TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit but designed to be more powerful and flexible. It supports annotations, parallel execution, data-driven testing, and reporting.

  • Annotation-based testing
  • Parallel test execution
  • Data-driven testing using DataProvider
  • Flexible test configuration
  • Dependency management
  • Detailed reporting


Annotations define how methods should behave in a test. Common ones include:

  • @Test – marks a test method
  • @BeforeMethod / @AfterMethod
  • @BeforeClass / @AfterClass
  • @BeforeSuite / @AfterSuite
  • @BeforeMethod: runs before each test method
  • @BeforeClass: runs once before all test methods in a class
TestNGJUnit
Supports parallel executionLimited parallel support
Supports dependency testingNo dependency feature
Supports DataProviderLimited parameterization
More flexible annotationsFewer annotations
Better reportingBasic reporting

testng.xml is a configuration file used to control test execution in TestNG.

It helps to:

  • Define test suites
  • Group tests
  • Run tests in parallel
  • Pass parameters
  • Control execution order

Example:

<suite name="Suite">
  <test name="Test">
     <classes>
        <class name="tests.LoginTest"/>
     </classes>
  </test>
</suite

Groups are used to categorize test methods.

Example:

@Test(groups = "smoke")
public void loginTest() {
}

You can run only specific groups using testng.xml.

It allows a test method to depend on an entire group of tests.

Example:

Groups are used to categorize test methods.

Example:

@Test(groups = "smoke")
public void loginTest() {
}

You can run only specific groups using testng.xml.

Dependency means one test method depends on another test method.

Example:

@Test
public void login() {
}

@Test(dependsOnMethods = "login")
public void dashboard() {
}

If login() fails, dashboard() will be skipped.

Hard Assert

  • Stops execution immediately if assertion fails.
Assert.assertEquals(actual, expected);

Soft Assert

  • Continues execution even after assertion failure.
SoftAssert sa = new SoftAssert();
sa.assertEquals(actual, expected);
sa.assertAll();

Assertions are used to validate expected and actual results.

Common assertions:

  • assertEquals()
  • assertTrue()
  • assertFalse()
  • assertNull()

Parameterization allows passing values to test methods from testng.xml.

Example:

<parameter name="browser" value="chrome"/>
@Parameters("browser")
@Test
public void launch(String browser) {
}
ParametersDataProvider
Values from XMLValues from Java method
Used for simple dataUsed for multiple datasets
Less flexibleMore flexible

Using parallel attribute in testng.xml.

Example:

<suite name="Suite" parallel="tests" thread-count="2">

It is used to run a test multiple times.

Example:

@Test(invocationCount = 5)
public void testMethod() {
}

Priority controls execution order of test methods.

Example:

@Test(priority = 1)
public void login() {
}

@Test(priority = 2)
public void logout() {
}

Lower priority executes first.

Test methods execute in alphabetical order by method name.

Timeout defines maximum execution time for a test.

Example:

@Test(timeOut = 3000)
public void test() {
}

Test fails if execution exceeds 3 seconds.

Used to skip a test method.

Example:

@Test(enabled = false)
public void skipTest() {
}
@BeforeTest@BeforeSuite
Runs before test tagRuns before entire suite
Executes multiple timesExecutes once

Two ways:

  1. enabled = false
  2. Using SkipException

Example:

throw new SkipException("Skipping test")

Listeners are interfaces that modify TestNG behavior during execution.

Common listeners:

  • ITestListener
  • ISuiteListener

Used for:

  • Logging
  • Screenshots
  • Reporting

It is a listener interface used to track test execution events.

Methods include:

  • onTestStart()
  • onTestSuccess()
  • onTestFailure()

TestNG automatically generates reports inside the test-output folder.

Main reports:

  • index.html
  • emailable-report.html

@Factory is used to create multiple instances of test classes dynamically.

@BeforeTest@BeforeMethod
Runs before all test methods in a testRuns before each test method
Executes onceExecutes multiple times

Yes. TestNG is widely used with Selenium for:

  • Test execution
  • Reporting
  • Assertions
  • Parallel testing
  • Data-driven testing

Using IRetryAnalyzer.

Example:

public class Retry implements IRetryAnalyzer {
  int count = 0;

  public boolean retry(ITestResult result) {
     if(count < 2) {
        count++;
        return true;
     }
     return false;
  }
}

It ensures the method executes even if dependent methods fail.

Example:

@Test(alwaysRun = true)

It defines the number of threads used during parallel execution.

Example:

thread-count="3"