In TestNG, what does dependsOnMethods do, and what happens to dependent tests when the method they depend on fails?
answer
- runs after, and only after success
- skipped, not failed
- the cascade travels down the chain
- hard versus soft dependency
- alwaysRun on the dependent
basics
~20 sdependsOnMethods makes a TestNG test run only after the named methods, and only if they all succeeded. If one fails, the dependent is reported SKIP, as is everything downstream, unless the dependent sets alwaysRun = true.
solid answer
~50 s`@Test(dependsOnMethods = "pay")` tells TestNG two things: run `pay` first, and run this method only if `pay` was a SUCCESS. That is a **hard dependency**, the default. If `pay` fails, the dependent is not invoked and is marked SKIP, not FAIL, and the skip propagates: a method depending on a skipped method is skipped as well. So in a `createOrder -> pay -> ship` chain where `pay` fails, the report shows one pass, one failure and one skip, which points straight at the real cause. Adding `alwaysRun = true` to the dependent makes it a **soft dependency**: it still runs after its prerequisites, but runs even when they failed. Names may be regular expressions, all overloads of a named method are run, a method in another class needs its fully qualified name, and listing several prerequisites does not order them among themselves.
code
java · 21 linesimport org.testng.annotations.Test;
public class OrderFlowTest {
private String orderId;
@Test
public void createOrder() {
orderId = "ord-42";
}
@Test(dependsOnMethods = "createOrder")
public void pay() {
throw new IllegalStateException("payment gateway declined " + orderId);
}
@Test(dependsOnMethods = "pay")
public void ship() { }
@Test(dependsOnMethods = "pay", alwaysRun = true)
public void writeAuditTrail() { }
}go deeper
Recall that dependsOnMethods means run after, and only if, the named methods succeeded, and that dependents of a failure show as skipped.
Explain the cascade through a chain, the difference between hard and soft dependencies, and that alwaysRun = true on the dependent gives order without the gate.
Read a report of one failure and several skips correctly, and know the edges: per-instance skipping, unordered prerequisites, cross-class names and overloads.
Weigh what the gate buys in report clarity against the state coupling and lost isolation every dependency introduces.
## What dependsOnMethods declares In TestNG a test method can declare that it depends on other test methods through the `dependsOnMethods` attribute of `@Test`. The declaration carries **two guarantees** at once: 1. **Ordering:** every method named is run before the dependent. 2. **Gating:** the dependent runs only if every one of those methods ended as a SUCCESS. TestNG's own documentation calls this a **hard dependency**, and it is the default (`alwaysRun = false`). It exists for workflows in which a later step is meaningless once an earlier one has broken: you cannot pay for an order that was never created, and a failure in `ship` after a broken `pay` tells you nothing new. ## The skip cascade When a hard prerequisite fails, TestNG does not invoke the dependent at all. It records the dependent as **SKIP**, and the HTML report shows skips in a colour that is neither the pass nor the fail colour, because a skipped method is not necessarily broken. The cascade is transitive: TestNG treats any prerequisite that did not end in SUCCESS, including one that was itself skipped, as a reason to skip. Take the reserved example, a three-step order flow in one class: | Method | Declared dependency | Outcome when `pay` throws | |---|---|---| | `createOrder` | none | PASS | | `pay` | `createOrder` | FAIL | | `ship` | `pay` | SKIP | | `notifyCustomer` | `ship` | SKIP | | `writeAuditTrail` | `pay`, with `alwaysRun = true` | runs, after `pay` | One failure and two skips is a far more honest report than three failures: the reader looks at `pay` first, and the skipped methods are visibly "not evaluated" rather than "broken". ## Hard versus soft dependencies A **soft dependency** is a dependency with `alwaysRun = true` on the dependent method. TestNG's documentation puts it this way: you will always be run after the methods you depend on, even if some of them failed. Use it when you want the order but not the gate, for example an audit or cleanup check that must come after `pay` whether or not `pay` succeeded. The `Test` annotation's own documentation notes that `alwaysRun` is ignored on a test that depends on no method or group, so it is not a general "never skip" switch. ## Details that trip people up - **Regular expressions:** the names in `dependsOnMethods` may be patterns, as may those in `dependsOnGroups`. - **Overloads:** if a named method is overloaded, all overloaded versions are run; the documentation suggests `dependsOnGroups` when you want only one of them. - **Other classes:** a bare name is qualified with the declaring class, so a prerequisite in another class must be written with its fully qualified name, such as `com.shop.OrderSetup.createOrder`. - **Several prerequisites:** `dependsOnMethods = {"a", "b"}` guarantees both run before the dependent, but not which of the two runs first. - **Instances:** when a factory or data provider produces several instances of the class, a failure skips dependents in the same instance, not in the others. By default TestNG also groups dependent methods by class, so it runs `a()` for every instance before any `b()`; the suite attribute `group-by-instances` changes that to one instance's chain at a time. - **Ordering against priority:** TestNG ranks its ordering rules, and a method dependency outranks `priority`. A high-priority method still waits for its prerequisites; priority only orders methods that are free to run. ## Reading a cascade in a report When a run shows one failure and many skips, read it from the top of the chain: - Find the FAIL entries first; TestNG's skip reason on each dependent says it "depends on not successfully finished methods" and lists its prerequisites. - Expect the skip count to grow with the length of the chain below the failure, not with the number of bugs. - Treat a skip with no failed prerequisite above it as a different problem, such as a failed configuration method, which also produces skips. ## What a dependency is not A dependency is not a way to share state for free. If `pay` needs the order id that `createOrder` produced, that id travels through a field of the test instance, which ties the methods to one instance and one execution order. That coupling, and whether to accept it, is the real design decision behind every `dependsOnMethods`. JUnit Jupiter offers ordering through method orderers but no built-in attribute that skips one test because another failed; this gating is specific to TestNG.
- In TestNG, if pay has priority = 10 and ship has priority = -5 but ship dependsOnMethods pay, which runs first?pay. TestNG ranks a method dependency above priority when it builds its run order, so ship waits for its prerequisite however low its priority number is. Priority, where lower numbers are scheduled first and the default is 0, only arbitrates among methods whose dependencies are already satisfied.
- In TestNG, why is a skipped dependent better than letting it fail on its own?Because the skip names the cause. If ship ran after a broken pay it would fail with a misleading error, such as a missing payment, and the report would show two failures to triage. A SKIP tells the reader the method was never evaluated and that the real fault sits upstream, in the one FAIL.
- In TestNG, does alwaysRun = true on a test method with no dependencies change anything?No. The Test annotation documents that alwaysRun is ignored when the method depends on no method or group. It only turns a hard dependency into a soft one; it does not stop a test from being filtered out or skipped for other reasons.
A hard dependency is like a relay race: if the second runner drops the baton, the third never starts, and the result sheet says did not run, not lost. A soft dependency is the official who records the time after the second leg whatever happened on it.
- A test whose prerequisite failed is reported as FAIL.
- dependsOnMethods only orders tests; it never prevents one from running.
- Listing two prerequisites runs them in the order written.
- alwaysRun = true makes any test immune to skipping.
- A skip only affects the direct dependent, not tests further down the chain.
- A bare method name finds a prerequisite in another class.
Assumes TestNG 7.12, where a method dependency outranks priority in the run-order ranking and skips cascade through skipped prerequisites.