TestNG – Dependent Tests

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 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.

Note

Test dependencies only work with other tests that belong to the same class or in one of the inherited class 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 !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

7 thoughts on “TestNG – Dependent Tests”

  1. Hi, Any idea how to run a test suite which have methods with declared dependency but the tests on which they are dependent are not inculded in test suite?

    Reply
  2. Can we define a dependency between 2 different Test Classes?

    public class TestA {

    @Test(groups = "TestA")
    public void executeTest() {

    System.out.println("Executing testA");
    assertTrue(Boolean.TRUE,
    "Test A failed to pass the assertions ");

    }

    public class TestB {
    @Test(groups = "TestB", dependsOnGroups = "TestA")
    public void execute() {
    System.out.println("Executing testB");
    assertTrue(Boolean.TRUE,
    "Test B failed to pass the assertions ");
    }
    }

    Reply
  3. Hi Lokesh,

    Similar to dependencyOnXXXX we can also control the order of execution using priority and this works even between 2 completely separated class and which can further fine tuned by placing them in the same group or separate.
    Class A{
    @Test( priority = 2 )
    M1(){}
    }

    Class B{
    @Test(priority = 1)
    M2(){}
    }

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.