Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

TestNG Framework

  • What is TestNG Framework?
  • Advantages and Disadvantages of TestNG:
  • Difference between TestNG And Junit framework
  • What is TestNG Annotations?
  • Install TestNG In Eclipse & IntelliJ?
  •  Hierarchy In TestNG Annotations
  • TestNG’s prioritization
  • TestNG Dependent
  • Reporter Class in TestNG
  • TestNG Reports
  • Assertions in TestNG
  • TestNG Groups
  • TestNG Parameters
  •  Cross-Browser Testing in TestNG
  •  Parallel Testing in TestNG
  • Data Providers
  • TestNG Listeners
  • Rerunning failed tests in TestNG
View Categories
  • Home
  • Tutorials
  • TestNG
  • TestNG Framework
  • TestNG Dependent

TestNG Dependent

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.

testng framework
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
TestNG’s prioritizationReporter Class in TestNG
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105