TestNG
02 / 02

Dependencies, Timeouts & Soft Assertions

Dependencies, Timeouts & Soft Assertions

Test Dependencies

@Test
public void login() { ... }

@Test(dependsOnMethods = { "login" })
public void checkout() { ... }  // skipped, not failed, if login fails

Useful for genuinely sequential integration flows, but a failure cascades into skipped dependents rather than independently-reported failures — obscuring whether checkout would have passed on its own. Independent unit tests generally avoid this by design; integration tests more often have real sequential dependencies.

Timeouts & Repeated Runs

@Test(timeOut = 5000)
public void doesNotHang() { ... }   // fails instead of hanging the whole suite

@Test(invocationCount = 10)
public void checksForFlakiness() { ... }  // catches intermittent/race-condition bugs

@Test(expectedExceptions = IllegalArgumentException.class)
public void rejectsNegativeAmount() {
  account.withdraw(-10);
}

Soft Assertions

SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(response.getName(), "Ada");
softAssert.assertEquals(response.getEmail(), "ada@example.com");
softAssert.assertEquals(response.getStatus(), "active");
softAssert.assertAll();  // reports ALL failures together, not just the first

Regular assertions stop at the first failure. SoftAssert collects every failing check and reports them together — useful when validating multiple fields at once, avoiding a fix-one-rerun-see-next cycle.

TestNG + Selenium

TestNG has no built-in browser automation — it provides structure/execution/reporting, while Selenium's WebDriver API handles actual browser interaction, called from within @Test methods. This mirrors how Cucumber pairs with a separate automation library for UI step definitions.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free