skip to content

After a TestNG suite starts excluding a group, the run errors with 'depends on nonexistent group'; why does dependsOnGroups do that, and how would you fix it, including with ignoreMissingDependencies?

level: seniorimportance: should knowfreq 36%

answer

  1. dependencies resolve against selected methods only
  2. an excluded group looks absent
  3. includes pull prerequisites in
  4. an opt-in escape hatch
  5. running without the prerequisite's state

basics

~20 s

dependsOnGroups resolves against the methods selected for the run, so excluding the depended-on group leaves nothing to match and TestNG throws a TestNGException. Restore the selection, or set ignoreMissingDependencies = true on the dependent to run without it.

solid answer

~50 s

With `@Test(dependsOnGroups = "db-seed")`, every selected method in a group matching `db-seed` must run and succeed first, and any non-success skips the dependent. TestNG builds that graph only from methods selected for the run, so a suite that adds `<exclude name="db-seed"/>` leaves the group empty and TestNG stops with a `TestNGException` saying the method depends on a nonexistent group. A misspelled group name or regex fails the same way. Three fixes: stop excluding a real prerequisite; select by inclusion, since when a suite includes groups TestNG implicitly adds the groups and methods the included tests depend on; or set `ignoreMissingDependencies = true` on the dependent, which then runs when its dependency is missing or excluded. That last flag is a deliberate choice: the test must be able to stand on its own when the prerequisite never ran.

code

java · 10 lines
java
import org.testng.annotations.Test;

public class InventoryReportTest {

  @Test(dependsOnGroups = "db-seed")
  public void stockReportNeedsSeedData() { }

  @Test(dependsOnGroups = "db-seed", ignoreMissingDependencies = true)
  public void reportHeaderRendersWithoutData() { }
}

go deeper

for a junior

Recall that dependsOnGroups waits for every method of the named groups and that a missing group is an error, not a silent skip.

for a middle

Explain that dependencies resolve against the selected methods, and that includes add prerequisites while exclude-only filters can remove them.

for a senior

Trace a 'nonexistent group' error to the suite file change behind it and pick the fix: restore the group, switch to inclusion, or opt in to ignoreMissingDependencies per test.

for a principal

Treat ignoreMissingDependencies as a reviewed exception rather than a default, because a blanket opt-in hides the misconfigurations the error exists to catch.

## What dependsOnGroups promises `dependsOnGroups` is the group-level form of a TestNG dependency. The `Test` annotation documents it precisely: every method member of the named groups is guaranteed to have been invoked before the dependent, and if any of those methods was not a SUCCESS the dependent is not run and is flagged as a SKIP. Group names here are regular expressions, so `dependsOnGroups = "init.*"` waits for every group whose name starts with `init`. The key detail for this failure is **where TestNG looks for the members**. It resolves the dependency against the methods selected for the current `<test>`, after group and method filtering, not against every method on the classpath. ## Why excluding a group breaks the run Picture a nightly pipeline that has run everything for months. A `db-seed` group seeds reference data, and several regression tests declare `dependsOnGroups = "db-seed"`. Someone speeds up a job by adding `<exclude name="db-seed"/>` to its suite file. Now: 1. Group filtering removes every `db-seed` method from the `<test>`. 2. While building its execution graph, TestNG looks up the members of `db-seed` among the selected methods and finds none. 3. TestNG throws a `TestNGException` whose message says the method "depends on nonexistent group" and names the group. The same error appears when the group name in `dependsOnGroups` is simply misspelled, or when the class holding the group's methods is not part of the `<test>`. For a method dependency the wording differs: TestNG reports that the method is depending on another method "which is not annotated with @Test or not included", or that it "depends on nonexistent method". ## Include-based selection pulls prerequisites in The two filtering styles behave differently here. When a suite **includes** groups, TestNG computes a transitive closure: the groups and methods that the included tests depend on are added implicitly, so including a group `z` that depends on `a`, `b` and `c` runs all four without naming the other three. An **exclude-only** filter computes no such closure; it only removes methods, and it can remove a prerequisite out from under its dependents. This is why the error often appears right after a suite is converted from includes to excludes: the old include quietly pulled `db-seed` in, and the new filter no longer does. ## The fixes, and what each one costs | Fix | What changes | Cost | |---|---|---| | Stop excluding the prerequisite | The dependency is honoured again | The job is not as fast as hoped | | Select by inclusion instead | Prerequisites are pulled in automatically | The suite file must name what to run, not what to skip | | `ignoreMissingDependencies = true` on the dependent | The dependent runs when its dependency is missing or excluded | The test may run without the state it assumed | | Move the seeding into a configuration method | No test-to-test dependency remains | A setup failure is reported as a configuration failure and skips tests, not as a test result | `ignoreMissingDependencies` defaults to `false`. Setting it to `true` changes the meaning of the dependency from "required" to "required if present": when the group is in the run it is still honoured, so a failing `db-seed` still skips the dependent, but when the group is absent the dependent runs anyway. That is only safe when the dependent can pass without the prerequisite, for example because the environment is already seeded in that pipeline. ## Declaring group dependencies in the suite file Group dependencies can also be stated in `testng.xml`, inside `<groups>`, with a `<dependencies>` element whose `<group name="..." depends-on="...">` entries take a space-separated list. This keeps the wiring next to the selection that might break it, at the price of spreading dependency information across code and suite files. ## Diagnosing it in practice - Read the message: "nonexistent group" names both the dependent method and the group. - Compare the suite's include and exclude lists against every `dependsOnGroups` value, remembering they are regular expressions. - Check that the classes holding the prerequisite are listed in the same `<test>`. - Decide per dependent whether the dependency is real; if it is, fix the selection rather than reaching for the flag. - After the fix, confirm the prerequisite actually appears in the results, not merely that the error has gone. A blanket `ignoreMissingDependencies = true` across a codebase hides exactly the misconfiguration this error exists to catch.

  • In TestNG, with ignoreMissingDependencies = true, what happens if the depended-on group is in the run but one of its methods fails?
    The dependent is still skipped. The flag only covers a dependency that is missing or excluded from the run; a dependency that is present is honoured as a normal hard dependency, so a failure in it produces the usual SKIP.
  • In TestNG, why does an include of the dependent's group usually avoid the 'nonexistent group' error?
    Because when a suite includes groups, TestNG computes the transitive closure of the included tests' dependencies and implicitly includes the groups and methods they depend on. An exclude-only filter computes no closure, so it can remove a prerequisite that dependents still need.

  • TestNG finds the depended-on group on the classpath even when it is excluded.
  • A missing dependency group just makes the dependent show as skipped.
  • ignoreMissingDependencies = true also ignores a prerequisite that ran and failed.
  • Group names in dependsOnGroups must match exactly, not as patterns.
  • Including a group never brings in groups it depends on.

Assumes TestNG 7.12: ignoreMissingDependencies defaults to false, and an included group implicitly pulls in the groups its tests depend on.