Dependencies with TestNG

‘TestNG’ is a framework that built with different set of APIs that facilitates many testing needs. It has variety of features that makes ‘TestNG’ more convenient than ‘JUnit’ framework. Among all the features, TestNG has ‘dependencies’ feature that gives the facility to determine the @Test annotation methods order. For the dependencies, TestNG uses ‘dependsOnMethods’ option for @Test annotation.

Hold on, should not confuse with ‘order’ word. I do not have meant that with this feature you could create your own order to run the Test methods.

TestNG dependency feature does not give guarantee on the order on which the methods depended upon will be run, however it guaranteed that TestNG will run all the @Test methods before the method which contains the ‘dependsOnMethods’ option.

In TestNG, we can achieve dependencies in two ways-

  • With Annotation.
  • With XML

It has two types also-

  • Hard Dependency
  • Soft Dependency

Hard dependency says that if depended upon @Test method fails then TestNG will skip the depended @Test method.

Soft dependency says that TestNG will run the depended @Test method if depended upon @Test methods fails also, however depended @Test method should have ‘alwaysRun=true’ option.

Hard Dependency Instance:

import org.testng.annotations.Test;

public class dependsOnMethods {

@Test

public void sub (){

int a = 7-3;

System.out.println(“sub “+a);

}

@Test (dependsOnMethods={“sub”})

public void div (){

int a = 10/2;

System.out.println(“div “+a);

}

@Test

public void add (){

int a = 7+3;

System.out.println(“add “+a);

}

}

[TestNG] Running:

C:\Users\testng-eclipse-224652221\testng-customsuite.xml

add 10

sub 4

div 5

PASSED: add

PASSED: sub

PASSED: div

TestNG runs all the @Test methods before the ‘div’ method.

Soft Dependency Instance:

import org.testng.annotations.Test;

public class dependsOnMethods {

@Test

public void sub (){

int a = 7-3;

System.out.println(“sub “+a);

}

@Test (alwaysRun= true, dependsOnMethods={“sub”})

public void div (){

int a = 10/2;

System.out.println(“div “+a);

}

@Test

public void add (){

int a = 7+3;

System.out.println(“add “+a);

}

}

TestNG will run the ‘div’ method if ‘sub’ methods fails also.

With Annotation Instance:

import org.testng.annotations.Test;

public class dependsOnMethods {

@Test

public void sub (){

int a = 7-3;

System.out.println(“sub “+a);

}

@Test (dependsOnMethods={“sub”})

public void div (){

int a = 10/2;

System.out.println(“div “+a);

}

@Test

public void add (){

int a = 7+3;

System.out.println(“add “+a);

}

}

‘div’ method is depended upon the ‘sub’ method. So whenever TestNG runs this class, all methods will run before the ‘div’ method.

[TestNG] Running:

C:\Users\testng-eclipse-224652221\testng-customsuite.xml

add 10

sub 4

div 5

PASSED: add

PASSED: sub

PASSED: div

With XML Instance:

To achieve dependency with XML, we should have testing.xml file. In testing xml file we can specify our group dependency with tag.

<test name=”regression suite”>

<groups>

<dependencies>

<group name=”sanity”/>

<group name=”functional” depends-on=”Sanity” />

<group name=”integration” depends-on=”Sanity  functional” />

</dependencies>

</groups>

</test>

The <depends-on> attribute contains a space-separated list of groups.