Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 15, 2016

Dependency Test in TestNG





Dependency test in TestNG will discuss about how a test can dependent on other tests or group of tests. This feature help in executing a set of tests to be executed before a test method. Method dependency only works if the depend-on-method is part of the same class or any of the inherited base class.

Test depends on Single Method:

A test can dependent on a single method or more than a single method. In this section we will see how a test depends on a single method.

Below is the sample program:

package com.java.testng.youtube;

import org.testng.annotations.Test;

public class DependsOnSingleTest
{
    @Test(dependsOnMethods={"testTwo"})
    public void testOne()
    {
        System.out.println("This method depends on testTwo");
    }
    
    @Test
    public void testTwo()
    {
        System.out.println("This method should execute before testOne");
    }
}

In the above program, we have two test methods called “testOne”,”testTwo” and testOne depends on testTwo, this can be achieved by one of the @Test attributes called “dependsOnMethods” as shown in the program. When you set this attribute, then it will look for the dependent method to execute first before the dependency test. If you execute the above program testTwo will execute first and then testOne.

Test depends on Multiple Methods:

As discussed in the previous section a method can depends on single method or multiple methods. In this section we will see how a test can depends on multiple methods.

Below is the sample program:

package com.java.testng.youtube;

import org.testng.annotations.Test;

public class DependsOnSingleTest
{
    @Test(dependsOnMethods={"testTwo","testThree"})
    public void testOne()
    {
        System.out.println("This method depends on testTwo and testThree");
    }
    
    @Test
    public void testTwo()
    {
        System.out.println("testTwo method should execute before testOne");
    }
    
    @Test
    public void testThree()
    {
        System.out.println("testThree method should execute before testOne");
    }
}

In the above program, we have three test methods called “testOne”,”testTwo” and “testThree” and testOne depends on testTwo,testThree, this can be achieved by one of the @Test attributes called “dependsOnMethods” as shown in the program. When you set this attribute, then it will look for the dependent method to execute first before the dependency test. If you execute the above program testTwo and testThree will execute first and then testOne. And we need to use comma to separate the test methods in the dependsOnMethods attribute.





Test that depends on its ParentClass Methods:

In the previous sections we have seen that dependency test and dependent tests are in the same class. But in this scenario dependency and dependent tests are in a seperate classes and there should be a relation between these two classes to achieve this functionality. Means, dependency test will be in the child class and dependent method should be parent class and child class should extend the parent class.

Below are the sample programs:

Child Class:

package com.java.testng.youtube;

import org.testng.annotations.Test;

public class ChildClassForTestDependency extends ParentClassForTestDependency
{
    @Test(dependsOnMethods={"parentTestOne"})
    public void childTestOne()
    {
        System.out.println("This method depends on parentTestOne which is in another class");
    }
    
    @Test
    public void childTestTwo()
    {
        System.out.println("This is childTestTwo");
    }
}

Parent Class:

package com.java.testng.youtube;

import org.testng.annotations.Test;

public class ParentClassForTestDependency
{
    @Test
    public void parentTestOne()
    {
        System.out.println("This method is the base for childTestOne method which is in another class");
    }
}

In the above, we have two classes called ChildClassForTestDependency and ParentClassForTestDependency. ChildClassForTestDependency methods are depends on ParentClassForTestDependency methods and this can be achieved using the inheritance concepts that child class will extend the parent class then child class can depends on the methods which are available in the parent class.

childTestOne is dependent on parentTestOne in the above example. Once the parentTestOne executes then the childTestOne will execute.

This way we can make tests dependent on each other to control the execution flow while executing the test suite.

Please watch the youtube video for better understanding.



Share this post: on Twitter on Facebook

TestNG Grouping Tests Tests Depends on Groups in TestNG

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025