HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TestNG / TestNG – Dependent Tests

TestNG – Dependent Tests

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.

Table of Contents

1. Test with single test method dependency
2. Test with multiple test methods dependencies
3. Inherited dependency test
4. Test that depends on a group

1. Writing test 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 which print a message name onto the console when executed. Here, 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.

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. Writing test with multiple tests 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 which 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. Inherited dependency test

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 which 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. Tests depend on groups

Similar to dependent tests, TestNG also allows tests to depend on 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 which print a message name onto the console when executed. Here, 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
Test dependency only works with other tests that belong to the same class or in one of the inherited classes but not across 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.

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

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Coder

    August 4, 2020

    Can we define 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 ");
        }
    }
    
  2. Dasagriva manu

    April 3, 2018

    Thanks for explaining TestNG dependency concept with such clarity

  3. Himansu

    July 26, 2016

    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(){}
    }

  4. kavya

    July 21, 2015

    Is there any possible way to skip test and also other tests depend upon this test in testng

Comments are closed on this article!

Search Tutorials

TestNG Tutorial

  • TestNG – Introduction
  • TestNG – Hello World
  • TestNG – Maven
  • TestNG – Annotations
  • TestNG – Expected Exception
  • TestNG – Disable/Ignore Tests
  • TestNG – Parallel Tests
  • TestNG – Dependent Tests
  • TestNG – Timeout Tests
  • TestNG – @Parameters
  • TestNG – @DataProvider
  • TestNG – @Factory
  • TestNG – @DataProvider
  • TestNG – Before and After
  • TestNG – Test Groups

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces