TestNG dependencies, types of dependencies and it’s details are explained in this post.

Why we use dependencies:
  • To run a test method before invoking another test methods
  • To share some data and states between test methods

TestNG dependency is used at @Test annotation, it allows a test method to depend on either another test method or a group.

  • dependsOnMethods is used in @Test annotation to depend on a method.
  • dependsOnGroups is used in @Test annotation to depend on a group.
Dependency on methods:
  • @Test
    public void connectToDB(){}
    @Test(dependsOnMethods = { "connectToDB" })
    public void test1() {}

    Above test method test1() is declared as dependent on test method connectionToDB(), hence connectionToDB() will always be executed first.

Dependency on Groups:
  • @Test(groups = { "init" })
    public void serverStarted() {}
    @Test(groups = { "init" })
    public void initEnvironment() { }
    @Test(dependsOnGroups = { "init" }) 
    public void test2() { }

    In the above example test method test2() is declared on group init, hence test methods serverStarted() and initEnvironment() will always be executed first.

TestNG support two types of DEPENDENCIES:
  1. Hard dependency:
    1. The test method will not be invoked and marked as skip in report if any dependency methods failed,
    2. By default, hard dependency is enabled ( alwaysRun=false)
  2.  Soft dependency:
    1. The main test methods will be invoked if any dependency method fails.
    2. To enable this, add “alwaysRun=true” in @Test annotation

Note: A method can be dependent on multiple other test methods, use dependentOnMethods with “,” separator in @Test annotation.

Below is the complete code of TestNG class with dependency methods and groups.

  • package com.exploreselenium;
    import org.testng.Assert;
    import org.testng.annotations.Test;
    public class DependencyTest {
        public void connectToDB(){
            System.out.println("Connected to DB ....");
        @Test(dependsOnMethods = { "connectToDB" })
        public void test1(){
            System.out.println("From Test method..test1...");
        @Test(groups = { "init" })
        public void serverStarted() {
            System.out.println("From Server started ...");
        @Test(groups = { "init" })
        public void initEnvironment() {
            System.out.println("From init environment ...");
        @Test(dependsOnGroups = { "init" })
        public void test2() {
            System.out.println("From test2 method ...");
        public void failedTest(){
            System.out.println("From failedTest ...");
        @Test(dependsOnMethods = {"failedTest"})
        public void test3(){
            System.out.println("From test3 method ...");


  • In the above example, here are the dependencies.
    • method test1() is dependent on connectToDB() methods
    • method test2() is dependent on group init
    • method test3() is dependent on method failedTest()

testng.xml file (dependent.xml)

  • <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name="Dependency Sample" verbose="1">
        <test name="Dependency" preserve-order="true">
                <class name="com.exploreselenium.DependencyTest"/>

Following is the output When executed the above dependent.xml file.

TestNG dependencies

  • method test1() dependent on connectToDB() methods, hence connectToDB method was executed before method test1()
  • method test2() dependent on group init, hence methods serverStarted() and initEnvironment() were executed before method test2()
  • method test3() dependent on method failedTest(), this failedTest() method were failed due to Assert, hence method test3() was skipped.

In this post, we learned TestNG method dependency on other methods and method dependency on Groups.

TestNG Parameters - Passing from XML
TestNG Data Provider