Site icon TestingDocs.com

TestNG Dependencies

Introduction

TestNG dependencies allows us to specify dependency methods or dependency groups for test methods. In the test code, we can specify the dependencies using the @Test annotation attributes:

TestNG guarantees that the methods depended upon will be run before the test method that contains this annotation is run. However, there is no guarantee on the order of the methods.

We can also specify the dependencies using the <dependencies> tag in the testng.xml suite file.

dependsOnMethods

The methods should be in the same Test class or in the parent class. If the methods are overloaded, all the overloaded methods will run.

If we specify a method of other class the test would org.testng.TestNGException depends on nonexistent method exception.

Types

There are two types of dependencies.

To know more about the types:

https://www.testingdocs.com/hard-and-soft-dependency-in-testng-framework/

 

Dependency is a handy tool in TestNG framework to make some methods run in the order you specify. TestNG by default executes the tests in ascending order of their names. By default the dependent method is skipped when there is a failure in the method you specify as a dependency.

Code Listing

 

package com.testingdocs.testng.web;

//www.TestingDocs.com - TestNG Tutorials
import org.testng.annotations.Test;

public class WebTests {

	/***************************************************************
	 * Run only if all the methods from "deployApplicatio"
	 * group are passed.
	 ***************************************************************/
	@Test(dependsOnGroups="deployApplication")
	public void init() {
		System.out.println("Initializations..");
	}

	/**************************************************************
	 * Run if "init" method is passed.
	 **************************************************************/
	@Test(dependsOnMethods = {"init"})
	public void testAppMethod() {
		System.out.println("Web Application Test...");
	}
}

 

init() method depends on group of tests called “DeployApplication”. The testAppMethod() depends on the init method. The deploy tests are:

 

package com.testingdocs.testng.deploy;

//www.TestingDocs.com - TestNG Tutorials
import org.testng.annotations.Test;

public class DeployTest {

	@Test(groups="deployApplication")
	public void deployApp() {
		System.out.println("Application War deploy ...");
	}

	@Test(groups="deployApplication",dependsOnMethods="deployApp")
	public void basicTest() {
		System.out.println("Basic Application Test...");
	}
}

 

There are 2 tests in the deployApplication test group.  All the method in the deploy group are run before the init() method. This is because the init() method depends on the deploy group:

 

@Test(dependsOnGroups="deployApplication") 
public void init() {
...}

 

 

In the same fashion, init() is run before the testAppMethod(). This is because the testAppMethod depends on the init() method.

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

Exit mobile version