Careeroza
TestNG and Framework Design
What is the execution order of TestNG annotations?
@BeforeSuite
@BeforeTest (per <test> tag in testng.xml)
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite@BeforeGroups/@AfterGroups run before the first / after the last test of a group. Tests inside a class run in alphabetical order unless priority or dependsOnMethods is set.
What is the difference between hard assert and soft assert?
A hard assert (Assert.assertEquals) stops the test at the first failure. A soft assert collects failures and reports all of them when you call assertAll().
SoftAssert soft = new SoftAssert();
soft.assertEquals(order.status(), "PLACED", "status");
soft.assertEquals(order.total(), "₹1,499", "total");
soft.assertEquals(order.items(), 1, "items");
soft.assertAll(); // forget this and the test always passes!Example
when two checks failExample output
java.lang.AssertionError: The following asserts failed: total expected [₹1,499] but found [₹1,549], items expected [1] but found [2]
How do you pass data to tests in TestNG?
@DataProvider(name = "logins")
public Object[][] logins() {
return new Object[][] {
{"[email protected]", "Secret@123", true},
{"[email protected]", "wrong", false},
{"", "Secret@123", false},
};
}
@Test(dataProvider = "logins")
public void login(String email, String pwd, boolean shouldPass) { ... }Other ways: @Parameters from testng.xml (browser, environment), and DataProviders that read Excel (Apache POI), CSV or JSON. @DataProvider(parallel = true) runs rows in parallel.
How do you rerun failed tests in TestNG?
- IRetryAnalyzer:
retry(ITestResult)returns true to rerun; attach with@Test(retryAnalyzer = ...)or to all tests with anIAnnotationTransformer. - testng-failed.xml: TestNG writes it to
test-output/target/surefire-reportsafter a run; run it to execute only failed tests. - In Cucumber: the
rerun:plugin plus a second runner.
Note: Say in the interview that retries are for infrastructure problems and that retried tests are reported and fixed, not hidden.
Walk me through your automation framework.
A good answer follows a fixed order (2 minutes):
- Stack: Java 17/21, Selenium 4, TestNG, Maven, Log4j2, Extent/Allure, Git, Jenkins/GitHub Actions.
- Design: hybrid framework, Page Object Model with a BasePage holding explicit-wait helpers.
- Driver: DriverFactory with
ThreadLocal<WebDriver>for parallel runs; local or RemoteWebDriver on Grid from config. - Config and data: properties per environment, DataProviders from Excel, API calls for setup.
- Reporting: listeners take screenshots on failure, Extent report mailed after each run.
- CI: smoke on every build, regression nightly on Grid, results in Jenkins.