These TestNG interview questions for freshers and juniors (0–2 years) cover the fundamentals every Selenium interview touches: annotations and their order, testng.xml, priority and groups, DataProvider versus @Parameters, assertions and the basics of listeners.

Fresher Level (0–1 Year)

1. What is TestNG?

TestNG (Test Next Generation) is an open-source Java testing framework inspired by JUnit. It adds powerful features like parallel execution, data-driven testing (@DataProvider), grouping (@Test groups), parameters from XML, and built-in reporting. Widely used for Selenium automation.

2. What is the execution order of TestNG annotations?

@BeforeSuite → @BeforeTest → @BeforeGroups → @BeforeClass → @BeforeMethod → @Test → @AfterMethod → @AfterClass → @AfterGroups → @AfterTest → @AfterSuite. BeforeMethod/AfterMethod repeat for each @Test.

3. What is @BeforeSuite vs @BeforeClass vs @BeforeMethod?

@BeforeSuite: runs ONCE before entire suite (e.g., start reporting). @BeforeClass: runs ONCE before all tests in the class (e.g., open browser). @BeforeMethod: runs before EACH @Test method (e.g., navigate to URL).

4. What is @Test(priority)?

Controls test execution order. Lower number runs first. Default priority = 0. Tests with same priority run alphabetically. Example: priority=1 runs before priority=2.

5. What is the difference between @Test(priority) and @Test(dependsOnMethods)?

priority: just controls ORDER — test always runs regardless of previous result. dependsOnMethods: test is SKIPPED if the dependency test FAILS. Use priority for independent ordering, dependsOnMethods for logical flow.

6. What is @Test(enabled = false)?

Disables/skips the test — it won't run. Similar to commenting out but keeps the code. Use for temporarily skipping broken tests. Different from @Test(groups={'broken'}) which is excluded in XML.

7. What is Assert.assertEquals?

Verifies two values are equal. assertEquals(actual, expected, 'message'). Stops test immediately on failure (hard assertion). Always provide a message as the 3rd parameter for better error reporting.

8. What is the difference between Assert and SoftAssert?

Assert (hard): stops test immediately on first failure. SoftAssert: collects ALL failures and reports them together when assertAll() is called. Create a new SoftAssert() per test. MUST call assertAll() at the end.

9. What is @DataProvider?

Annotation on a method that returns Object[][] to supply multiple data sets to a @Test. The test runs once per row of data. Enables data-driven testing without duplicating test code.

10. What is testng.xml?

XML configuration file for TestNG that defines suites, tests, classes, groups, parameters, parallel settings, listeners, and thread-count. Controls which tests run and how they run.

11. How do you skip a test in TestNG?

1) @Test(enabled=false) — hard skip. 2) throw new SkipException('reason') inside the test. 3) Exclude from testng.xml using <exclude name='testMethod'/>. 4) Use groups and don't include that group.

12. What is @BeforeMethod(alwaysRun=true)?

alwaysRun=true ensures the method runs even if the test it precedes is in a failed or skipped group. Critical for teardown methods — ensures browser is always closed even if test throws an unexpected exception.

Advertisement

Junior Level (1–2 Years)

13. What is the difference between @DataProvider and @Parameters?

@DataProvider: method returns Object[][] — different data per test run. Use for test data (credentials, inputs). @Parameters: injects single value from testng.xml — same for all runs. Use for configuration (browser, URL, environment).

14. Explain dependsOnMethods with example.

@Test(dependsOnMethods={'testLogin'}) — testCheckout runs only if testLogin PASSES. If testLogin FAILS, testCheckout is SKIPPED (not Failed). If you use alwaysRun=true, it runs regardless of dependency result.

15. What is ITestListener? What methods does it have?

Interface to listen to test events. Key methods: onTestStart (before each test), onTestSuccess (after pass), onTestFailure (after fail — capture screenshot here), onTestSkipped, onStart (before suite), onFinish (after suite — flush reports).

16. How do you take a screenshot on test failure in TestNG?

In @AfterMethod, check result.getStatus() == ITestResult.FAILURE. Or implement ITestListener.onTestFailure(). Use TakesScreenshot interface: ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE) then copy to screenshots folder.

17. What are TestNG groups? Give a real-world example.

Groups categorise tests: @Test(groups={'smoke'}). In testng.xml, include/exclude groups. Example: smoke group has critical path tests (login, home page). Run only smoke after each deployment. Regression runs full suite nightly.

18. What is @Test(expectedExceptions)?

Test passes only if the specified exception is thrown. @Test(expectedExceptions=ArithmeticException.class). If no exception is thrown, test FAILS. Can also add expectedExceptionsMessageRegExp to verify the message.

19. What is IRetryAnalyzer?

Interface with retry(ITestResult) method. Return true to retry the test, false to stop. Used for flaky tests — tests that occasionally fail due to timing/network issues. Implement and add to @Test(retryAnalyzer=MyRetry.class).

20. Explain how parallel execution works in TestNG.

Set parallel attribute in testng.xml: parallel='tests'|'classes'|'methods'|'instances'. Set thread-count for number of concurrent threads. Use ThreadLocal<WebDriver> to ensure each thread has its own browser instance.

21. What is SoftAssert? When would you use it?

SoftAssert collects assertion failures and reports them all at once when assertAll() is called. Use when validating multiple properties on the same page (title, price, rating, image) — you want all failures reported, not just the first.

22. What is the difference between @Test(timeOut) and WebDriverWait?

@Test(timeOut=3000): fails the entire test if it takes more than 3 seconds — guards against infinite loops or hanging tests. WebDriverWait: waits for a specific element condition. Both are time controls but serve different purposes.

23. How do you run only smoke tests using testng.xml?

In testng.xml, inside <groups><run>, add <include name='smoke'/>. Only tests annotated with @Test(groups={'smoke'}) will run. All other tests are excluded regardless of what classes are listed.

24. What is the difference between ITestResult.FAILURE and ITestResult.SKIP?

FAILURE (status=2): test ran but assertion/exception failed. SKIP (status=3): test was not executed — dependsOnMethods failed, or SkipException was thrown, or test is disabled.

FAQs

What is the first TestNG question in most interviews?

The order in which TestNG annotations run, from @BeforeSuite down to @AfterSuite, often followed by how priority and dependsOnMethods change the order of tests.