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 ?
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.
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.
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:
dependsOnMethods to ensure they are executed in the desired order.
dependsOnMethods to establish the required order.
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
@BeforeSuite annotations to handle setup operations, when possible, instead of relying solely on
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.