TestNG is asked about in virtually every Selenium automation interview. Here are the remaining questions that were not covered inline above. Practice answering these out loud — interviewers want confident, structured answers.
Q: How do you run failed tests in TestNG?
A: TestNG automatically generates testng-failed.xml in the test-output directory after every run. This file contains only the failed tests. I re-run them with: mvn test -DsuiteXmlFile=test-output/testng-failed.xml. Additionally, I implement IRetryAnalyzer to automatically retry failed tests up to 2 times during the same run. I apply the retry globally using IAnnotationTransformer so I do not need to add retryAnalyzer to every @Test.
Q: What is the difference between @BeforeTest and @BeforeClass?
A: @BeforeTest runs once before ALL test classes inside a <test> tag in testng.xml. @BeforeClass runs once before the first test method in the CURRENT class. If you have 3 classes inside one <test> tag, @BeforeTest runs once for all 3, but @BeforeClass runs once per class (3 times total). In practice, I use @BeforeClass for class-level setup like loading test data, and @BeforeTest when multiple classes share a setup step.
Q: How do you pass parameters from testng.xml to test methods?
A: Using @Parameters annotation. In testng.xml, define <parameter name="browser" value="chrome" />. In the test class, add @Parameters({"browser"}) before the method. The method receives the value as a String parameter. For optional parameters, use @Optional("defaultValue") so the test does not fail if the parameter is missing from testng.xml.
Q: What listeners have you used in your framework?
A: Three main listeners: (1) ITestListener — I override onTestFailure() to take screenshots, onStart()/onFinish() to log suite summary. (2) IRetryAnalyzer — to retry flaky tests up to 2 times. (3) IAnnotationTransformer — to apply RetryAnalyzer to all @Test methods globally without modifying each test class. I register all listeners in testng.xml under the <listeners> tag.
Q: What is the difference between dependsOnMethods and priority?
A: priority controls execution ORDER — lower number runs first, but all tests run regardless. dependsOnMethods creates a DEPENDENCY — if the parent method fails, the dependent method is SKIPPED. Use priority when you want a specific order but tests are independent. Use dependsOnMethods when a test genuinely cannot run without the other passing (like testing dashboard after login).
Q: How do you group tests in TestNG? Give a real example.
A: Using the groups attribute on @Test: @Test(groups = {"smoke", "regression"}). In testng.xml, I include or exclude groups: <groups><run><include name="smoke" /></run></groups>. In my project, we have three groups: "smoke" (runs on every build, 5 minutes), "regression" (runs nightly, 45 minutes), and "sanity" (runs after deployment, 2 minutes). The CI pipeline runs smoke on every commit and regression every night.
Q: How do you skip a test in TestNG?
A: Three ways: (1) @Test(enabled = false) — disables the test, shows as skipped in reports. (2) throw new SkipException("reason") inside the test — conditionally skip at runtime. (3) dependsOnMethods — if the dependency fails, the dependent test is automatically skipped. I use enabled=false for temporarily disabled tests and SkipException for environment-specific skips like "skip this test on Windows."
Key Point: Be ready to explain annotations, execution order, DataProviders, testng.xml, parallel execution, Hard vs Soft Assert, and listeners. These come up in every Selenium interview.