Exit course
Chapter 6 of 8 Intermediate 6 min read 2 quick checks Updated

TestNG Interview Questions

On this page
  1. Junior level
  2. Mid level
  3. Senior level

TestNG questions show up in almost every Java automation interview, usually next to Selenium or Appium. Interviewers want to know whether you can make a suite run in parallel without the tests interfering with each other. Examples target TestNG 7.12, which requires Java 11 or later.

Junior level#

In what order do TestNG configuration methods run?#

@BeforeSuite
  @BeforeTest          <- once per <test> tag in testng.xml
    @BeforeClass
      @BeforeMethod
        @Test
      @AfterMethod
    @AfterClass
  @AfterTest
@AfterSuite

@BeforeGroups and @AfterGroups run before the first and after the last test method of a group.

  • Follow-up: "Where do you create the WebDriver?" In @BeforeMethod for full isolation (a fresh browser per test), or @BeforeClass when a class's tests deliberately share one session.
  • Red flag: Assuming @BeforeTest runs before every test method. It runs once per <test> element in testng.xml, which can contain many classes. This confusion is behind a lot of "the browser was already closed" bugs.

A class creates the driver in @BeforeTest and quits it in @AfterMethod. The first test method passes; every later one fails with "invalid session id". What do you change?

@BeforeTest runs once per <test> tag, but @AfterMethod runs after every method, so the second method uses a quit session. Pairing setup and teardown at the same level fixes it; retries would fail the same way.

Hard assert or soft assert?#

Assert.assertEquals() stops the test on the first failure. SoftAssert collects failures and reports them all when you call assertAll():

SoftAssert softly = new SoftAssert();
softly.assertEquals(page.title(), "Order confirmed");
softly.assertTrue(page.hasInvoiceLink(), "invoice link");
softly.assertAll(); // forget this and the test always passes

Soft asserts suit checking several independent fields on one page. Use hard asserts when later steps make no sense after a failure.

How do you group and select tests?#

@Test(groups = {"smoke", "checkout"})
public void guestCanCheckOut() { ... }
<test name="Smoke">
  <groups><run><include name="smoke"/></run></groups>
  <packages><package name="com.example.tests"/></packages>
</test>

With Maven Surefire you can also select groups on the command line with -Dgroups=smoke.

Mid level#

How do you run TestNG tests in parallel?#

Set parallel and thread-count on the suite (or on a <test>):

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Regression" parallel="methods" thread-count="8">
  <test name="Chrome">
    <classes>
      <class name="com.example.tests.CheckoutTest"/>
      <class name="com.example.tests.SearchTest"/>
    </classes>
  </test>
</suite>

parallel accepts methods, classes, tests and instances. tests is a common way to run the same classes on several browsers at once, with one <test> per browser and a <parameter name="browser" value="firefox"/> read via @Parameters("browser").

  • Follow-up: "Now tests close each other's browsers. Why?" The driver is stored in a shared field. With parallel="methods" every method runs on its own thread, so the driver must be held in a ThreadLocal<WebDriver> (see the pattern in the advanced Selenium chapter) or created per method.
  • Red flag: A static WebDriver driver in a base class combined with parallel="methods".

What is a DataProvider, and can it run in parallel?#

A method that supplies rows of arguments to a test:

@DataProvider(name = "logins", parallel = true)
public Object[][] logins() {
    return new Object[][] {
        {"standard_user", true},
        {"locked_user", false},
    };
}

@Test(dataProvider = "logins")
public void login(String user, boolean shouldSucceed) { ... }

With parallel = true the rows run on separate threads; the pool size is the suite's data-provider-thread-count (default 10), not thread-count.

A parallel DataProvider supplies 40 rows and the suite sets thread-count="20", yet only 10 rows run at a time. Why?

DataProvider parallelism has its own pool, set on the suite with data-provider-thread-count; thread-count does not affect it. Raise that attribute (and make sure your grid has the capacity).

How do you take a screenshot on failure?#

Implement ITestListener (its methods have default implementations, so override only what you need):

public class ScreenshotListener implements ITestListener {
    @Override
    public void onTestFailure(ITestResult result) {
        WebDriver driver = DriverFactory.get();
        if (driver != null) {
            byte[] png = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
            // attach to your report, named after result.getName()
        }
    }
}

Register it with @Listeners(ScreenshotListener.class) on a class or a <listeners> block in testng.xml. See what TestNG listeners are for the other listener interfaces.

Senior level#

How do you retry failed tests, and should you?#

Implement IRetryAnalyzer:

public class RetryOnce implements IRetryAnalyzer {
    private int attempts = 0;

    @Override
    public boolean retry(ITestResult result) {
        return attempts++ < 1;
    }
}

@Test(retryAnalyzer = RetryOnce.class)
public void flakyCheckout() { ... }

To apply it to every test without touching annotations, set it from an IAnnotationTransformer listener.

  • What the interviewer wants to hear: retries are a reporting tool. Track which tests needed a retry and treat that list as a backlog; otherwise retries hide real defects, including race conditions in the product.

dependsOnMethods and priority: when would you use them?#

dependsOnMethods skips a test when the method it depends on fails, and priority orders tests within a class. Both are legitimate, and both create coupling: a dependent test cannot run alone, and it cannot be spread across threads freely. A senior answer prefers independent tests that set up their own state through APIs, and uses dependencies only for genuinely sequential flows such as a long multi-step migration test.

TestNG or JUnit 5?#

Both are capable. TestNG's strengths are suite-level XML configuration, built-in parallel modes, groups, dependencies and parallel data providers, which is why many Selenium frameworks use it. JUnit 5 has the larger general Java ecosystem, extensions, and parallel execution configured in junit-platform.properties. Pick by team familiarity and existing tooling, and say so.

How does a TestNG suite talk to a remote grid?#

Nothing in TestNG changes: the @BeforeMethod creates a RemoteWebDriver pointing at the grid URL, with the browser chosen from a @Parameters value. The Selenium with TestNG course builds this up step by step, and the TestNG documentation for TestingBot shows a parallel suite on a cloud grid.