TestNG Dependent
Here’s an overview of how you can declare and manage dependencies between TestNG tests—so that one test only runs if another has passed—covering single- and multi-method dependencies, inheritance, group-based dependencies, and XML-driven dependencies.
1. What Are Dependent Tests?
A dependent test is one that will only execute if one or more “prerequisite” tests have passed. You declare dependencies using the dependsOnMethods or dependsOnGroups attributes of @Test. If any dependency fails or is skipped, the dependent test is automatically skipped.
2. Single Dependent Test Method
Link one test directly to exactly one other:
public class SingleDependency {
@Test
public void initialize() {
// setup code…
System.out.println("initialize");
}
@Test(dependsOnMethods = "initialize")
public void performAction() {
// only runs if initialize() passed
System.out.println("performAction");
}
}
Flow:
- initialize() runs.
- If it passes, then performAction() runs; otherwise it’s skipped.
3. Multiple Dependent Test Methods
Require all listed methods to pass before running:
public class MultipleDependencies {
@Test
public void stepOne() { /*…*/ }
@Test
public void stepTwo() { /*…*/ }
@Test(dependsOnMethods = { "stepOne", "stepTwo" })
public void finalStep() {
// runs only if both stepOne() & stepTwo() passed
}
}
- Listing a non-existent method triggers a configuration error.
- If either stepOne or stepTwo fails/skips, finalStep is skipped.
4. Inherited Dependent Test Methods
Child classes inherit dependencies declared in parent classes:
public class BaseTests {
@Test
public void baseSetup() { /*…*/ }
}
public class InheritedTests extends BaseTests {
@Test(dependsOnMethods = "baseSetup")
public void childTest() {
// will wait for BaseTests.baseSetup()
}
}
Even though baseSetup() lives in the superclass, TestNG will wire up the dependency correctly.
5. Group-Based Dependencies
Instead of naming methods, you can depend on entire groups:
public class GroupDependency {
@Test(groups = "init")
public void initDb() { /*…*/ }
@Test(groups = "init")
public void initCache() { /*…*/ }
@Test(dependsOnGroups = "init")
public void runTests() {
// runs after all tests in the “init” group have passed
}
}
You can also depend on multiple groups:
@Test(dependsOnGroups = { "init", "config" })
public void finalTest() { … }
6. Dependent Tests in XML Suite
You can declare dependencies in your testng.xml instead of (or in addition to) annotations:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="DependentSuite">
<test name="XMLDeps">
<classes>
<class name="com.example.tests.MyTests">
<methods>
<!-- require init() before running runMain() -->
<include name="runMain">
<depends-on>
<method name="init"/>
</depends-on>
</include>
</methods>
</class>
</classes>
</test>
</suite>
- Under each <include>, use <depends-on> with one or more <method name=”…”/>.
- If init() fails/skips, runMain() is skipped—even without dependsOnMethods in code.
Key Points & Best Practices
Hard dependency: declared via dependsOnMethods or dependsOnGroups. Failing prerequisites skip the dependent test.
Ordering: TestNG ensures dependencies run before the dependent, regardless of priorities.
Skip vs. Fail: Dependents are marked skipped (not failed) if a prerequisite fails.
Circular deps: TestNG will error on cycles (A→B→A).
Use sparingly: Over-chaining can make suites brittle; consider grouping or priorities for lighter coupling.
With these mechanisms you can precisely orchestrate multi-step scenarios—ensuring that later tests only run when all their prerequisites have succeeded.