Saturday, September 28, 2019

Post#129.Dependency Test in TestNG


Dependency Test in TestNG:
Sometimes, you may need to invoke methods in a Test case in a particular order or you want to share some data and state between methods. This kind of dependency is supported by TestNG as it supports the declaration of explicit dependencies between test methods.

In simple, Dependency means if one @Test method fails or skipped from execution then its dependent @Test method must not be executed.
TestNG allows you to specify dependencies either with:
·         Using attributes dependsOnMethods in @Test annotations OR
·         Using attributes dependsOnGroups in @Test annotations.

Take a look over the below example:
package TestNgAdv;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SetPriority2 {
    @Test(priority=1)
    public void Login() {
  System.out.println("LogIn Test code.");
  Assert.assertTrue(6>7,"condition is false");
    }
    @Test(priority=2, dependsOnMethods={"Login"})
    public void checkMail() {
  System.out.println("checkMail Test code.");  
    }
    @Test(priority=3, dependsOnMethods={"Login","checkMail"})
    public void LogOut() {
  System.out.println("LogOut Test code.");
    }
    @Test(priority=4)
    public void Validation() {
  System.out.println("Login Validation Test code.");
    }
}

In the above program, LogOut() method is dependent on login() and checkMail() methods. If these two methods get failed or skipped then LogOut() method will not execute.

Testng.xml

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

1 comment: