I know that having interdependent tests is a bad idea. But, then my question is:

What are the valid usecases of dependsOnMethods which TestNg team provided ?

In other word, under what conditions i can have interdependent tests ?

2 Answers 2


Mostly it makes sense to use it when the results of the first test determine the results of the second test.

Think about an OR operation. If you have false OR function(), you know that the results of the operation will false, so you don't need to call function(). (assuming it is a pure function).

Now for the dependsOnMethods. Let's say you have UI-focused tests for a web search bar.

If the first is about being able to write texts on the bar. And the second test is about the suggestion list.

enter image description here

If the first test results in you not being able to write anything, most probably the second test will not give you any useful information. Thus it can make sense to skip the second one all together.

On the other hand, if your tests were focused on the use cases (not UI), e.g. hitting the APIs that receive the search text and generate the suggestions, they probably will be independent and here we shouldn't skip the second test if the first fails.

In summary, you have to understand well if you can get useful information from the second test if the first fail in specific ways. If you can't get, optimize your runtime by skipping the second test.


The dependsOnMethods feature in TestNG allows you to define dependencies between test methods. While it is generally recommended to avoid interdependent tests, there are valid use cases where dependsOnMethods can be beneficial. Let's explore some scenarios where using dependsOnMethods can be appropriate:

  1. Test Execution Order:
  • In certain cases, you may have test methods that need to be executed in a specific order due to dependencies between them.
  • For example, if you have a series of test methods that follow a logical sequence, such as creating an entity, performing actions on it, and then verifying the results, you can use dependsOnMethods to ensure they are executed in the desired order.
  1. Prerequisite Setup:
  • When a test method requires specific setup or data that is created or modified by another test method, you can use dependsOnMethods to establish the required order.
  • For instance, if you have a test case that verifies the deletion of a user account, it may be necessary to create the user account first in a separate test method. The deletion test method can depend on the creation test method to ensure the required setup is in place.
  1. Performance Optimization:
  • In certain scenarios, you may have test methods that perform expensive setup operations, such as database initialization or API calls.
  • By using dependsOnMethods, you can ensure that these setup methods are executed only once and shared among multiple test methods, reducing the overall execution time.

It's important to note that while dependsOnMethods can be useful in these scenarios, it's essential to exercise caution and maintain the independence of tests as much as possible. Here are a few best practices to consider when using dependsOnMethods:

  • Clearly document the dependencies between test methods to ensure clarity and understanding among team members.
  • Avoid creating long chains of dependencies, as this can make the test suite more fragile and difficult to maintain.
  • Regularly review and refactor the test suite to minimize dependencies and promote test independence.
  • Use other TestNG features such as @BeforeMethod, @BeforeClass, and @BeforeSuite annotations to handle setup operations, when possible, instead of relying solely on dependsOnMethods.

By considering these best practices and using dependsOnMethods judiciously, you can effectively manage dependencies between test methods and maintain a well-structured and maintainable test suite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.