Dependencies in TestNg – Selenium Online Tutorials

Dependencies in TestNg, In this tutorial we will study about how to create dependency in TestNg , We may across come to the situation where we need to execute the test case when certain test case executes.

Why do we need dependencies in TestNg ?

Example – Suppose we have a test case to check the user details . Now before logging to the user page we need to login first . So in this scenario the login test case must pass . If login test case fails then there is no need to execute the user profile test case as User profile test case automatically fails as login test case fails and we are not able to login to website.


In TestNg there are two types of dependencies

  • Dependsonmethod
  • Dependsongroups

dependsOnmethods in TestNg

It is used when we need to create the dependency of one test case with another. Below is the example to understand the dependencies in TestNg.

Syntax : dependsOnMethods ={“Test 1”} where Test 1  is a method used inside @Test annotation.

Test Case 1 : Open Browser
Test case 2 : Open website
Test Case 3 : Login
Test Case 4 : Check User profile

Java Code

package com.testng;
 
import org.testng.annotations.Test; 
public class dependencies_testNg {
 
// Test case to open the browser
@Test
public void Open_browser(){
 
System.out.println("Browser is opened");
 
}
 
// Test case to open the website
// it is dependent on Open browser
@Test(dependsOnMethods={"Open_browser"})
public void Open_Webpage(){
 
System.out.println("Open website ");
}
 
// Test case to perform login and
// it is dependent on Open webpage
@Test(dependsOnMethods={"Open_Webpage"})
public void Login(){
 
System.out.println("Login with credentials");
}
 
// Test case to perform login and
//it is dependent on login function
@Test(dependsOnMethods={"Login"})
public void Check_User_Profile(){
 
System.out.println("validate the user profile");
}
 
}

Output :

PASSED: Open_browser
PASSED: Open_Webpage
PASSED: Login
PASSED: Check_User_Profile

Now just analyze the above program , without opening the browser we cannot open the website , and without opening the website we cannot execute test cases . So here we need the dependency that test should run before some test cases.

Hope now you are clear why dependency is needed in TestNg and what are Dependencies in TestNg.


Example 2 – Lets check suppose Browser open test case got failed , the all dependent test case will get skipped.

Java Program to fail the Test case

package com.testng;
 
import org.testng.Assert;
import org.testng.annotations.Test;
 
public class dependencies_testNg {
 
// Test case to open the browser
@Test
public void Open_browser(){
 
System.out.println("Browser is opened");
 
// fail the test case
Assert.assertEquals(true, false);
 
}
 
// Test case to open the website
// it is dependent on Open browser
@Test(dependsOnMethods={"Open_browser"})
public void Open_Webpage(){
 
System.out.println("Open website ");
}
 
// Test case to perform login and
// it is dependent on Open webpage
@Test(dependsOnMethods={"Open_Webpage"})
public void Login(){
 
System.out.println("Login with credentials");
}
 
// Test case to perform login and
//it is dependent on login function
@Test(dependsOnMethods={"Login"})
public void Check_User_Profile(){
 
System.out.println("validate the user profile");
}
 
}

Output

===============================================

Default suite

Total tests run: 4, Failures: 1, Skips: 3


DependsOnGroups in TestNg

Just like we have created dependencies among test cases , we can create dependency of test case with some group which means that one test case executes only after all test cases of a group executes.

Syntax : dependsOnGroups ={“Group 1”} where Group 1  is a group used inside @Test annotation


Let’s suppose we need to execute some of the test cases after the regression testing . In that case we create the dependency of the test case to the regression test group.

Below is the example – Java code

package com.testng;
import org.testng.annotations.Test;
 
public class dependencies_testNg {
 
// Test case 4 execute after regression using group annotation
@Test(dependsOnGroups={"regression testing"})
public void Test1Case4(){
 
System.out.println("Test Case executed not of regression");
}
 
// Test case 1 of regression testing group
@Test(groups={"regression testing"})
public void TestCase1(){
 
System.out.println("Test Case 1 executed of regression testing");
}
 
// Test case 2 of regression testing group
@Test(groups={"regression testing"})
public void TestCase2(){
 
System.out.println("Test Case 2 executed of regression testing");
}
 
// Test case 3 of regression testing group
@Test(groups={"regression testing"})
public void TestCase3(){
 
System.out.println("Test Case 3 executed of regression testing");
}
 
 
}

TestNg.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="com.testng.dependencies_testNg"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Output

Test Case 1 executed of regression testing
Test Case 2 executed of regression testing
Test Case 3 executed of regression testing
Test Case executed not of regression
PASSED: TestCase1
PASSED: TestCase2
PASSED: TestCase3
PASSED: Test1Case4

Hope now you are all set with the Dependencies in TestNg and have clear understanding when to use the dependencies in TestNg.

Enroll for the Selenium online Training

Leave a Comment

Your email address will not be published. Required fields are marked *