As the lead of a large TestNG suite, would you allow dependsOnMethods at all, and what rule would you set for when it is acceptable?
answer
- report clarity versus isolation
- state carried in instance fields
- chains constrain scheduling
- setup belongs in hooks
- a written, reviewable exception
basics
~20 sAllow dependsOnMethods only as a reviewed exception: for steps of one workflow in one class, where a later step is meaningless after an earlier failure. Setup goes in configuration methods, and every other test must run and pass on its own.
solid answer
~40 sI would allow it, but narrowly. What `dependsOnMethods` buys is real: when `pay` fails, `ship` is reported SKIP instead of failing with a misleading error, so the report points at one root cause. What it costs is also real: the chain usually shares state through instance fields, a dependent cannot be run or debugged without its upstream methods, the scheduler must hold dependents back, and a long chain turns one break into a wall of skips that hides coverage. So my rule would be: dependencies only between steps of a single business flow inside one class; no cross-class `dependsOnMethods`; never as an ordering trick; setup and teardown in `@Before...`/`@After...` methods, not in fake tests; and the skip count watched like the failure count. `dependsOnGroups` on one environment-readiness group can be the one suite-wide exception.
go deeper
Recall that dependsOnMethods orders tests and skips dependents of a failure, and that independent tests are the default expectation.
Explain the coupling a dependency hides: shared instance state, no isolated runs, and a skip cascade from one break.
Show from real suites where a dependency clarified a report and where a chain hid coverage or blocked parallel runs.
Set and defend a written rule: narrow allowed uses, forbidden patterns, setup in configuration methods, and skip counts monitored like failures.
## The decision a lead actually faces TestNG is one of the few mainstream runners with a built-in test-to-test dependency: `@Test(dependsOnMethods = ...)` orders a method after its prerequisites and skips it when they fail. JUnit Jupiter can order methods but has no built-in attribute that skips one test because another failed. Because the feature exists, a TestNG codebase will grow dependencies unless someone sets a rule. The question is not whether the mechanism works; it does. It is whether the suite stays fast, debuggable and honest as it grows. ## What dependencies genuinely buy - **Root-cause clarity.** In a `createOrder -> pay -> ship` flow, a failure in `pay` yields one FAIL and one SKIP. Without the dependency, `ship` fails as well, with an error about a missing payment, and two failures must be triaged instead of one. - **No wasted work.** Steps that cannot meaningfully run after an upstream break are not executed, which matters for slow end-to-end flows driving a browser. - **A visible setup result.** A setup expressed as a test appears in the report as a pass or a failure; TestNG's documentation lists this as one reason for dependencies, since methods tagged `@Before`/`@After` are not part of the final report. ## What they cost - **Hidden state coupling.** The dependent usually reads something the prerequisite wrote to an instance field. That ties the methods to one instance and one execution order, and it is invisible in the method signature. - **Lost isolation.** A dependent cannot be run, rerun or debugged on its own; its whole upstream chain comes with it, and rerunning only the failed tests has to bring the chain back too. - **Constrained scheduling.** A dependency is an edge in TestNG's execution graph, and it outranks `priority` and `preserve-order`. Dependents wait for their prerequisites, so long chains limit how much a parallel run can overlap. - **Coverage hidden behind skips.** One early break can skip dozens of tests. A dashboard that counts only failures reports a mostly green run while most of the flow went unchecked. - **Fragile names.** Dependencies are strings, and in other classes fully qualified ones, so a rename breaks them at run time, not at compile time. ## A rule I would set and defend 1. **Default: every test is independent.** It creates its own data and passes when run alone. 2. **Allowed:** `dependsOnMethods` between steps of one business flow in one class, when a later step is meaningless after an earlier failure. 3. **Forbidden:** cross-class `dependsOnMethods`; dependencies used to force an order that the tests do not logically need; and fake "tests" whose only job is setup. 4. **Setup and teardown** go in configuration methods (`@BeforeClass`, `@BeforeMethod`, `@BeforeGroups`), not in tests that others depend on. 5. **One suite-wide exception:** `dependsOnGroups` on a small environment-readiness group, so that a dead environment produces a handful of failures and a clear wall of skips instead of hundreds of confusing failures. 6. **Soft dependencies are reviewed too.** `alwaysRun = true` keeps the order while dropping the gate; it should be reserved for steps such as audits that must follow a step whatever its outcome. 7. **Measure skips.** Track the skip count next to the failure count, and treat a rising skip count as a defect. ## Trade-offs to argue openly | Position | Strongest argument | Weakest point | |---|---|---| | Ban dependencies outright | Every test is isolated, parallel-friendly and individually rerunnable | Long workflows report cascades of misleading failures | | Allow freely | Cheap to write, reports read like a story | State coupling and skip walls grow with the suite | | Allow narrowly, by rule | Keeps the clarity where a flow is genuinely sequential | Needs review discipline to stay narrow | The narrow rule is my default. A team running mostly unit-level API checks may reasonably ban dependencies outright; a team running long browser workflows will want the gate on its few sequential flows. Either way, the rule should be written down and enforced in review, because each individual dependency always looks harmless. Revisit it when the suite changes shape: a move to parallel execution, or a push to rerun failures automatically, both raise the cost of every chain already in the codebase.
- In TestNG, a team wants a fixed run order for readability and proposes chaining every test with dependsOnMethods. What do you say?That it buys order at the price of gating: one failure now skips everything after it, and no test can run alone. If the tests do not logically need each other, they should not depend on each other; and if a fixed order is truly needed, the reason should be made explicit and the hidden shared state removed.
- How would you justify a dependsOnGroups gate on an environment-readiness group in a TestNG suite?A dead environment otherwise produces hundreds of failures that each look like a product bug. Making functional tests depend on a small readiness group turns that into a few honest failures plus clearly labelled skips, so triage starts in the right place. It stays acceptable because it is one well-named group, not a web of method chains.
- dependsOnMethods is a harmless way to make tests run in a fixed order.
- Skipped tests are nothing to monitor because they are not failures.
- Setup belongs in a test that other tests depend on.
- Dependencies have no effect on how much a parallel run can overlap.
- Banning or allowing dependencies has one right answer for every team.