SoftwareTestPilot
28 TestNG Q&A

TestNG Interview Questions & Answers (1970)

28 real TestNG interview questions with senior-SDET answers and Java code — annotations, execution order, DataProvider, groups & priority, parallel runs, listeners, retry analyzers, and testng.xml suites.

  • 3 min read
  • Difficulty: Mixed (Easy → Medium)
  • Freshers → 5+ yrs
  • Updated July 1970
  • Avinash Kamble

1. TestNG Basics

Easy Very Common 1 min read

Q1.What is TestNG?

TestNG is a Java testing framework inspired by JUnit but designed for a wider range of test categories — unit, integration, end-to-end, and functional. It's the default runner for enterprise Selenium suites in 2026.

Medium Very Common 1 min read

Q2.TestNG vs JUnit — key differences?

FeatureTestNGJUnit 5
Data providersNative @DataProvider@ParameterizedTest
GroupsYesTags only
DependenciesdependsOnMethodsNot supported
ParallelNative (XML)Native (properties)
ListenersRich setExtension model
Medium Very Common 1 min read

Q3.How do you add TestNG to a Maven project?

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>7.10.2</version>
  <scope>test</scope>
</dependency>
Medium Very Common 1 min read

Q4.What is a testng.xml file?

The suite configuration file. Defines which classes/methods run, in which order, parallel mode, groups, and parameters.

<suite name="Regression" parallel="tests" thread-count="4">
  <test name="Chrome">
    <parameter name="browser" value="chrome"/>
    <classes><class name="tests.LoginTest"/></classes>
  </test>
</suite>
Medium Very Common 1 min read

Q5.How do you run a single test method?

mvn test -Dtest=LoginTest#validLogin
# or from testng.xml, use <methods><include name="validLogin"/></methods>
Confidence check

If you can confidently answer the TestNG Basics questions above, you're well prepared for this section of your interview. Move on, or rehearse the trickier ones aloud with our AI mock interviewer.

2. Annotations & Lifecycle

Medium Very Common 1 min read

Q6.List the main TestNG annotations.

  • @Test, @BeforeSuite/@AfterSuite
  • @BeforeTest/@AfterTest
  • @BeforeClass/@AfterClass
  • @BeforeMethod/@AfterMethod
  • @DataProvider, @Parameters, @Listeners, @Factory
Medium Very Common 1 min read

Q7.What's the execution order of the annotations?

@BeforeSuite → @BeforeTest → @BeforeClass → @BeforeMethod → @Test → @AfterMethod → @AfterClass → @AfterTest → @AfterSuite.

Medium Very Common 1 min read

Q8.Difference between @BeforeTest and @BeforeMethod?

@BeforeTest runs once per <test> block in the XML. @BeforeMethod runs before every single @Test method — used for per-test setup like opening a browser.

Medium Very Common 1 min read

Q9.How do you skip a test conditionally?

@Test
public void featureFlagged() {
  if (!Config.isEnabled("beta")) throw new SkipException("Beta off");
  // ...
}
Medium Very Common 1 min read

Q10.What is a soft assertion?

SoftAssert soft = new SoftAssert();
soft.assertEquals(title, "Home");
soft.assertTrue(banner.isDisplayed());
soft.assertAll(); // required, else failures are swallowed

Soft assertions collect failures and report all of them at the end.

Medium Very Common 1 min read

Q11.How do you use @Test attributes?

@Test(priority = 1, groups = {"smoke"}, timeOut = 5000,
      dependsOnMethods = "login", enabled = true,
      description = "Add to cart")
public void addToCart() { /* ... */ }
Medium Common 1 min read

Q12.How do groups work?

@Test(groups = {"smoke", "regression"})
public void t1() {}
<groups><run><include name="smoke"/></run></groups>
Confidence check

If you can confidently answer the Annotations & Lifecycle questions above, you're well prepared for this section of your interview. Move on, or rehearse the trickier ones aloud with our AI mock interviewer.

3. Groups, Priority & Dependencies

Medium Common 1 min read

Q13.How do you set test priority?

@Test(priority = N) — lower N runs first. Ties are alphabetical. Don't rely on priority for real dependencies; use dependsOnMethods.

Easy Common 1 min read

Q14.What does dependsOnMethods do?

Ensures a prerequisite test passes before dependents run. If the parent fails, dependents are marked SKIPPED, not failed.

Medium Common 1 min read

Q15.How do you retry failed tests?

public class Retry implements IRetryAnalyzer {
  int count = 0, max = 2;
  public boolean retry(ITestResult r) { return count++ < max; }
}
@Test(retryAnalyzer = Retry.class) public void flaky() {}
Medium Common 1 min read

Q16.What is IAnnotationTransformer used for?

To programmatically apply annotations at load time — e.g. attach a Retry analyzer to every @Test without editing each class.

Medium Common 1 min read

Q17.How do you set a global timeout?

Per test: @Test(timeOut = 5000). Suite-wide: <suite time-out="5000"> in testng.xml.

Medium Common 1 min read

Q18.How do you handle expected exceptions?

@Test(expectedExceptions = IllegalArgumentException.class,
       expectedExceptionsMessageRegExp = "id.*required")
public void invalidInput() { service.create(null); }
Confidence check

If you can confidently answer the Groups, Priority & Dependencies questions above, you're well prepared for this section of your interview. Move on, or rehearse the trickier ones aloud with our AI mock interviewer.

4. DataProvider & Parameterization

Medium Common 1 min read

Q19.What is @DataProvider?

@DataProvider(name = "logins")
public Object[][] logins() {
  return new Object[][]{ {"a@x.com","p1"}, {"b@x.com","p2"} };
}
@Test(dataProvider = "logins")
public void login(String email, String pwd) { /* ... */ }
Medium Common 1 min read

Q20.How do you read test data from Excel/CSV?

Read the file inside the @DataProvider and return Object[][]. Popular libs: Apache POI (Excel), OpenCSV (CSV), Jackson (JSON).

Medium Occasional 1 min read

Q21.Difference between @Parameters and @DataProvider?

@Parameters reads static values from testng.xml (like browser name). @DataProvider supplies dynamic in-code datasets and drives multiple iterations of one test.

Medium Occasional 1 min read

Q22.How do you pass parameters from testng.xml?

<parameter name="browser" value="chrome"/>
@Test @Parameters("browser")
public void open(String browser) { /* ... */ }
Confidence check

If you can confidently answer the DataProvider & Parameterization questions above, you're well prepared for this section of your interview. Move on, or rehearse the trickier ones aloud with our AI mock interviewer.

5. Parallel, Listeners & XML

Medium Occasional 1 min read

Q23.How do you run tests in parallel?

<suite name="R" parallel="methods" thread-count="8">

Values: tests, classes, methods, instances. Combine with a ThreadLocal<WebDriver> to keep browser instances isolated.

Medium Occasional 1 min read

Q24.What is a Listener?

An interface that hooks into TestNG events. Implement ITestListener to add screenshots on failure, Extent Report entries, or Slack notifications.

@Listeners(ScreenshotListener.class)
public class BaseTest {}
Easy Occasional 1 min read

Q25.What is @Factory?

Creates test class instances at runtime — useful when the same tests need to run for many test-data rows or configurations.

Medium Occasional 1 min read

Q26.How do you integrate TestNG with Selenium POM?

Extend a BaseTest that has @BeforeMethod to launch the browser and @AfterMethod to quit it. Page objects are instantiated inside tests. See our POM guide (concepts translate to Java).

Medium Occasional 1 min read

Q27.How do you generate reports?

Default: test-output/emailable-report.html. Popular add-ons: ExtentReports, Allure, ReportPortal. Wire them via a Listener.

Medium Occasional 1 min read

Q28.How do you integrate TestNG with Jenkins/GitHub Actions?

- run: mvn -B test -DsuiteXmlFile=testng.xml
- uses: actions/upload-artifact@v4
  if: always()
  with: { name: reports, path: test-output/ }
Confidence check

If you can confidently answer the Parallel, Listeners & XML questions above, you're well prepared for this section of your interview. Move on, or rehearse the trickier ones aloud with our AI mock interviewer.

Quick revision

  1. Q1: What is TestNG — TestNG is a Java testing framework inspired by JUnit but designed for a wider range of test categories — unit, integration, end-to-end, and functional.
  2. Q2: TestNG vs JUnit — key differences — Feature TestNG JUnit 5 Data providers Native @DataProvider @ParameterizedTest Groups Yes Tags only Dependencies dependsOnMethods Not supported Parallel Native (XML) Native (propert
  3. Q3: How do you add TestNG to a Maven project — &lt;dependency&gt; &lt;groupId&gt;org.testng&lt;/groupId&gt; &lt;artifactId&gt;testng&lt;/artifactId&gt; &lt;version&gt;7.10.2&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/
  4. Q4: What is a testng.xml file — The suite configuration file.
  5. Q5: How do you run a single test method — mvn test -Dtest=LoginTest#validLogin # or from testng.xml, use &lt;methods&gt;&lt;include name="validLogin"/&gt;&lt;/methods&gt;

Frequently asked questions

Yes — it's still the default runner for Java Selenium and REST Assured suites. JUnit 5 is catching up but TestNG dominates enterprise QA jobs.

If you're doing Selenium/Java automation, learn TestNG first — it maps 1:1 to what most QA jobs use. Add JUnit 5 later.

Yes. Store the driver in ThreadLocal<WebDriver>, initialize in @BeforeMethod, tear down in @AfterMethod, set parallel='methods' in testng.xml.

Use 7.10+ (Java 11+ required). Older 6.x still works but misses newer annotations and native soft-assert improvements.

Was this article helpful?

Cluster · QA Career

More from QA Interview Questions

Behavioral, framework, coding — full interview prep.

Pillar guide · 10 articles
More in this cluster
From the QA Career pillar

Key takeaways

  • Master the fundamentals before tackling advanced TestNG scenarios.
  • Always explain trade-offs — interviewers reward judgement, not memorisation.
  • Use real project examples; generic answers blend in.
  • Practice answers out loud — written prep doesn't transfer to live rounds.
  • Revise the 30-second cheat sheet the night before your interview.
  • Keep one strong scenario story ready for every section above.

These Questions Are Just the Start

Get 1000+ more with detailed model answers in our QA Interview Preparation Kit. Covers manual testing, automation, API, SQL, Selenium, Playwright, and framework concepts — everything asked in real QA interviews.

Get Interview Kit — ₹1,045

TestNG jobs hiring now

Live, indexable TestNG openings — updated daily in Jobs Radar.

Browse all QA jobs on Jobs Radar

Loading current openings…

Home