alwaysRun Attribute in TestNG
By default, if a method listed in dependsOnMethods fails, the dependent test is skipped. Setting alwaysRun = true overrides this — the test runs regardless of what happened to its dependencies.
Without alwaysRun (default behavior):
Java
@Test
public void parentTest() {
Assert.fail("Failed test"); // This FAILS
}
@Test(dependsOnMethods = {"parentTest"})
public void dependentTest() {
System.out.println("This is SKIPPED because parentTest failed");
}
With alwaysRun = true:
Java
@Test
public void parentTest() {
Assert.fail("Failed test"); // This FAILS
}
@Test(dependsOnMethods = {"parentTest"}, alwaysRun = true)
public void dependentTest() {
System.out.println("Running even if parent test failed");
// This RUNS despite parentTest failing
}
Report difference:
| Scenario | parentTest | dependentTest |
|---|---|---|
| Without alwaysRun | FAILED | SKIPPED |
| With alwaysRun = true | FAILED | RUNS (passes or fails on its own) |
Practical use case — cleanup after failure:
Java
@Test
public void createUserTest() {
// Create a user — might fail
Assert.assertTrue(createUser("testUser"), "User creation failed");
}
@Test(dependsOnMethods = {"createUserTest"}, alwaysRun = true)
public void deleteUserTest() {
// ALWAYS delete the user to clean up, even if creation failed
deleteUser("testUser");
}
alwaysRun with groups:
Java
@Test(dependsOnGroups = {"login"}, alwaysRun = true)
public void teardownTest() {
// Always runs cleanup even if login group tests fail
driver.quit();
}
Key points:
- ✓Without
alwaysRun: dependent test is SKIPPED when dependency fails - ✓With
alwaysRun = true: dependent test ALWAYS RUNS regardless - ✓Useful for cleanup tests that must run even after failure
- ✓Removing
alwaysRun=truefrom@AfterClass/@AfterMethodcan prevent cleanup
