Careeroza
Priority Dependencies and Timeouts
Test Attributes
| Attribute | Example | Effect |
|---|---|---|
priority | @Test(priority = 1) | lower number runs first (default 0) |
dependsOnMethods | @Test(dependsOnMethods = "login") | skipped if login failed |
enabled | @Test(enabled = false) | not run (e.g. known bug) |
timeOut | @Test(timeOut = 30000) | fails if longer than 30 s |
invocationCount | @Test(invocationCount = 3) | runs 3 times |
expectedExceptions | @Test(expectedExceptions = NoSuchElementException.class) | passes only if it throws |
description | @Test(description = "TC_LOGIN_001") | shown in reports |
dependsOnMethods in Action
@Test(priority = 1) public void login() { /* fails: wrong password in config */ }
@Test(priority = 2, dependsOnMethods = "login") public void addToCart() { ... }
@Test(priority = 3, dependsOnMethods = "addToCart") public void checkout() { ... }Example
Output:
mvn test -Dtest=CheckoutFlowTestExample output
[ERROR] login(com.shopease.tests.CheckoutFlowTest) <<< FAILURE! [WARNING] Tests run: 3, Failures: 1, Errors: 0, Skipped: 2
Note: Use dependencies sparingly. Good tests are independent: each one sets up its own state (logs in itself) so one failure does not skip twenty other tests.
How Priority Ordering Works
Example
TestNG sorts by priority, then by method name. This runnable example shows the resulting order:
import java.util.*;
public class Main {
record TestMethod(String name, int priority) {}
public static void main(String[] args) {
List<TestMethod> tests = new ArrayList<>(List.of(
new TestMethod("checkout", 3), new TestMethod("searchProduct", 0),
new TestMethod("login", 1), new TestMethod("addToCart", 2), new TestMethod("homePageLoads", 0)));
tests.sort(Comparator.comparingInt(TestMethod::priority).thenComparing(TestMethod::name));
int i = 1;
for (TestMethod t : tests) System.out.println(i++ + ". " + t.name() + " (priority " + t.priority() + ")");
}
}Output
1. homePageLoads (priority 0) 2. searchProduct (priority 0) 3. login (priority 1) 4. addToCart (priority 2) 5. checkout (priority 3)