TestNG Dependent Tests with Examples

Test Dependency is a feature in TestNG that allows a test method to depend on a single or a group of test methods. This will help in executing a set of dependent tests to be executed before a test method.

Tests dependency only works if the depend-on-method is part of the same class or any of the inherited base class (i.e. while extending a class).

In this tutorial, we’ll learn about creating dependent tests in TestNG.

1. Tests with Single Test Dependency

Use dependsOnMethods to create a sample test method that depends on another test method of the same class.

public class DependentTestExamples
{
	@Test(dependsOnMethods = { "testTwo" })
	public void testOne() {
		System.out.println("Test method one");
	}

	@Test
	public void testTwo() {
		System.out.println("Test method two");
	}
}

The preceding test class contains two test methods that print a message name onto the console when executed. Here, the test method testOne depends on the test method testTwo.

This is configured by using the attribute dependsOnMethods while using the Test annotation.

Let’s run the tests now.

Test method two
Test method one

PASSED: testTwo
PASSED: testOne

In the above test result, you can see the message Test method two printed before the Test method one message. This shows that the testOne method got executed after testTwo as it depends on testTwo.

2. Test with Multiple Dependencies

Sometimes it may be required for a test method to depend upon multiple other methods. This feature is very well supported by TestNG as part of the dependency support.

public class DependentTestExamples
{
	@Test(dependsOnMethods = { "testTwo", "testThree" })
	public void testOne() {
		System.out.println("Test method one");
	}

	@Test
	public void testTwo() {
		System.out.println("Test method two");
	}

	@Test
	public void testThree() {
		System.out.println("Test method three");
	}
}

The preceding test class contains three test methods that print a message name onto the console when executed. Here test method testOne depends on test methods testTwo and testThree. This is configured by using the attribute dependsOnMethods while using the Test annotation.

Let’s run the test now.

Test method three
Test method two
Test method one

PASSED: testThree
PASSED: testTwo
PASSED: testOne

By looking at the console message we can see that methods testTwo and testThree got executed before testOne.

3. Dependency Tests from Parent Class

Till now we have seen samples in which the dependent test methods were part of the same class. Dependency on test methods can only be mentioned for test methods that belong to the same class or any of the inherited base classes.

Now, let’s see how TestNG executes the test methods when the dependent tests are part of the inherited base class.

public class ParentClassTest
{
	@Test(dependsOnMethods = { "testTwo" })
	public void testOne() {
		System.out.println("Test method one");
	}

	@Test
	public void testTwo() {
		System.out.println("Test method two");
	}
}

public class DependentTestExamples extends ParentClassTest
{
	@Test(dependsOnMethods = { "testOne" })
	public void testThree() {
		System.out.println("Test three method in Inherited test");
	}

	@Test
	public void testFour() {
		System.out.println("Test four method in Inherited test");
	}
}

The preceding test class contains two test methods that print a message name onto the console when executed. Here test method testThree depends on test method testOne. This is configured by using the attribute dependsOnMethods while using the Test annotation.

Let’s run the test now.

Test four method in Inherited test
Test method two
Test method one
Test three method in Inherited test

PASSED: testFour
PASSED: testTwo
PASSED: testOne
PASSED: testThree

As you can see from the test results the sequence of execution is testFour, testTwo, testOne, and lastly, testThree. As testThree depends on testOne and on testTwo, TestNG executes all the test methods based on the dependency and finally the respective test method.

4. Dependent on Test Groups

Similar to dependent tests, TestNG also allows tests to depend on test groups. This makes sure that a group of test methods gets executed before the dependent test method.

public class DependentTestExamples
{
	@Test(dependsOnGroups = { "test-group" })
	public void groupTestOne() {
		System.out.println("Group Test method one");
	}

	@Test(groups = { "test-group" })
	public void groupTestTwo() {
		System.out.println("Group test method two");
	}

	@Test(groups = { "test-group" })
	public void groupTestThree() {
		System.out.println("Group Test method three");
	}
}

The preceding test class contains two test methods that print a message name onto the console when executed.

Here, the test method testOne depends on test method testTwo. This is configured by using the attribute dependsOnMethods while using the Test annotation.

Let’s run the tests now.

Group Test method three
Group test method two
Group Test method one

PASSED: groupTestThree
PASSED: groupTestTwo
PASSED: groupTestOne

That’s all related to dependent tests in TestNG. Let me know if you have any queries.

5. Conclusion

Test dependencies only work with other tests that belong to the same class or in one of the inherited classes but not across other different classes.

In case you need a test method that exists in a separate class; you can achieve this by assigning the said test method to a group and configuring the dependent test method to be dependent on that group.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode