dependsOnMethods & priority for maintaining Order of execution

63 views
Skip to first unread message

Nisarg Dave

unread,
Oct 21, 2020, 8:43:20 AM10/21/20
to testng-users
Hi All, 

I am looking for some assistance on dependsOnMethod & priority functionalities of TestNG. I know, using both of them together is bit of redundancy & it doesn't work in expected manner. Hence looking for some help.

Here is my scenario:

1. I have a Test Flow class which has X number of Test methods. I am looking to execute them in proper sequence. Hence I know I can use either dependsOnMethods  or priority.
Because I am not looking to continue with execution if few important tests are failing, I am using dependsOnMethods. 
But the need is: I have few tests in Test class, which are not very much important (meaning even if they fail, I am not looking to skip with all my other subsequent test methods) 
I am using hierarchical dependsOnMethods (meaning 3rd test case is dependent on 2nd testcase & 2nd test case is dependent on 1st test case etc.). As you know, with dependsOnMethods, if 2nd test case fails, TestNG will skip 3rd & all other tests which are after it.
If I introduce priority along with dependsOnMethods, then I know the execution order will be mess. 
If I use only priority, then if important test case fails, then also TestNG will try to run all test cases which are in that test class (which will be time consuming & not needed)

So, the ask is: I want to execute the test cases in particular sequence (which I have defined) & for few test cases even if they fail, I want to continue with execution. 

Any thoughts ? Can anyone help in this regard to achieve this ? Thanks in advance. 

Regards,
Nisarg Dave



  

Nisarg Dave

unread,
Oct 21, 2020, 8:46:31 AM10/21/20
to testng-users
Also if any important test case fails (which I can define) then I will skip the execution.

Regards,
Nisarg

Aliasger Kiranawala

unread,
Oct 21, 2020, 9:07:17 AM10/21/20
to testng-users
Why dont you club test cases and give away the dependencies?
For better execution there should have been minimum dependency between test cases and if you are dependent on something its safe to seed/mock it with helpers rather then from another test. 

Nisarg Dave

unread,
Oct 21, 2020, 10:12:01 AM10/21/20
to testng...@googlegroups.com
I didn't get. Can you please elaborate how that would work?

Regards,
Nisarg
9725626638
-----------------------

   

[E-Book] Unlock infinite possibilities in your supply chain with AI

[E-Book] Home Services in 2020 and the changing role of logistics

[Whitepaper] Captive vs Outsourced Fleet: Math behind Transportation and Distribution


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/6c9e6d8d-dc11-468f-b45f-5787751834a9n%40googlegroups.com.

Aliasger Kiranawala

unread,
Oct 21, 2020, 10:18:09 AM10/21/20
to testng-users
I mean each test should seed its own test data and configs. For example if there is a login required each test should login and logout on its own. It should not be like login in test A and make test B dependent on A that after login perform x steps.

Nisarg Dave

unread,
Oct 21, 2020, 11:09:03 AM10/21/20
to testng...@googlegroups.com
Nope. Requirement is completely different. 

What I need is a test flow class having test methods should execute test cases. Test cases which are important fails (for which there is no need to move further if fails) execution should skip remaining test cases. 
And if few test cases which are not so important (can fail and I don't need to skip the remaining). 
But in both the cases order should be maintained. 
I know this is bit tricky. Please advise.

Regards,
Nisarg
9725626638
-----------------------

   

⇜Krishnan Mahadevan⇝

unread,
Oct 21, 2020, 11:29:13 AM10/21/20
to testng-users
Nisarg

Why dont you show an example of what your ask looks like. Feel free to make it as elaborate as you can (using sysout statements ofcourse) and also include a suite xml file.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


Nisarg Dave

unread,
Oct 21, 2020, 4:17:31 PM10/21/20
to testng...@googlegroups.com
Thanks for looking. I will share the example. 

Regards,
Nisarg

-----------------------

   

On Wed, Oct 21, 2020, 8:59 PM ⇜Krishnan Mahadevan⇝ <krishnan.ma...@gmail.com> wrote:
Nisarg

Why dont you show an example of what your ask looks like. Feel free to make it as elaborate as you can (using sysout statements ofcourse) and also include a suite xml file.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


On Wed, Oct 21, 2020 at 8:39 PM Nisarg Dave <nisargd...@gmail.com> wrote:
Nope. Requirement is completely different. 

What I need is a test flow class having test methods should execute test cases. Test cases which are important fails (for which there is no need to move further if fails) execution should skip remaining test cases. 
And if few test cases which are not so important (can fail and I don't need to skip the remaining). 
But in both the cases order should be maintained. 
I know this is bit tricky. Please advise.

Regards,
Nisarg

-----------------------

   

On Wed, Oct 21, 2020, 7:48 PM Aliasger Kiranawala <alia...@locus.sh> wrote:
I mean each test should seed its own test data and configs. For example if there is a login required each test should login and logout on its own. It should not be like login in test A and make test B dependent on A that after login perform x steps.


On Wednesday, October 21, 2020 at 7:42:01 PM UTC+5:30 nisargd...@gmail.com wrote:
I didn't get. Can you please elaborate how that would work?

Regards,
Nisarg
-----------------------

   

Nisarg Dave

unread,
Oct 26, 2020, 4:01:06 AM10/26/20
to testng-users
Hi All, 

Please find the example here:

1. I want to maintain sequence & want to skip others when any testcase fails. So, I am using hierarchical dependsOnMethods like below. 

@Test
public void test1() {
}
@Test(dependsOnMethods = "test1")
public void test2() {
}
@Test(dependsOnMethods = "test2")
public void test3() {
}
@Test(dependsOnMethods = "test3")
public void test4() {
}
@Test(dependsOnMethods = "test4")
public void test5() {
}

The output: 

PASSED: test1
PASSED: test2
PASSED: test3
PASSED: test4
PASSED: test5


2. I can achieve same also using priority (like: 1,2,3,4,5). But, It will not skip after failure. It will keep on executing. And in a lengthy test class (where there are 100 test cases), it will be time consuming to go with Priority.

@Test(priority = 1)
public void test1() {
}
@Test(priority = 2)
public void test2() {
}
@Test(priority = 3)
public void test3() {
}
@Test(priority = 4)
public void test4() {
}
@Test(priority = 5)
public void test5() {
}

The output: 

PASSED: test1
PASSED: test2
PASSED: test3
PASSED: test4
PASSED: test5


3. Now, I am looking to achieve some thing like: 
    a. I want to maintain test cases execution order
    b. Few test cases (which I think are not that important) fails, I want to keep on with execution (meaning priority functionality)
         For ex: test2() is not so important, if it fails I want to move on with execution of test3(), test4(), test5()

    c. Few test cases (which I think are very important) fails, I don't want to move on with execution. we should skip after that point. (meaning dependsOn functionality)
         For ex: test3() is important, if it fails I want to skip test4() & test5()

So to achieve this, I need to use dependsOnMethods & priority functionalities together. But when these 2 are used together, its sequence is maligned. 

Can you please advise ? Thanks !

Regards,
Nisarg
--------------------

Nisarg Dave

unread,
Oct 26, 2020, 4:20:36 AM10/26/20
to testng-users
Should I do something like this ?
i. define priority for all test cases, to achieve it executes everything irrespective of any failure observed earlier
ii. And define dependsOnMethods for those important test cases, where I want to skip for earlier failure. Since dependsOnMethods has higher precedence than priority. 

Any thoughts please ?

@Test(priority = 1)
public void test1() {
}
@Test(priority = 2, dependsOnMethods = "test1")
public void test2() {
int i = 10/0;

// failing test2()
}
@Test(priority = 3)
public void test3() {

              // since test2() is not that important, I have kept only priority defined for test3() to continue with execution.
}
@Test(priority = 4, dependsOnMethods = "test3")
public void test4() {
int i = 10/0;
               
              // failing test2()  
}
@Test(priority = 5, dependsOnMethods = "test4")
public void test5() {

              // since  test4() is important, I have kept both priority & dependsOnMethods defined for test4(). Hence it should skip test5() & remaining.
}

Krishnan Mahadevan

unread,
Oct 27, 2020, 1:15:02 PM10/27/20
to testng...@googlegroups.com

Nisarg,

 

  • dependsOnMethods – is available in TestNG to establish hard dependencies and [ i.e., execution will not move forward when there’s an upstream failure ]
  • priority – is available in TestNG to establish soft dependencies [ i.e., execution will move forward when there’s an upstream failure ]

 

IMO, ordering is just a side effect of dependsOnMethods

 

TestNG first builds the execution graph depending on the order specified via dependsOnMethods and on the resulting list of methods to be executed, it then sorts based on priority. But still dependsOnMethods is what will get honoured.

 

So, I would suggest that you don’t try to mix both of these.

 

Why can’t you just segregate the tests into two separate classes (one class which houses all dependsOnMethods and the other class which houses all the priority driven tests)

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.com/

Nisarg Dave

unread,
Oct 27, 2020, 1:35:50 PM10/27/20
to testng...@googlegroups.com
Thanks for looking into this Krishnan.

I completely understand and even I didn't want to mix both: dependsOnMethods & priority. 

But my requirement is to keep tests in a flow and not separating few tests with priority. 

Hence I am looking to find some solution. So far what I could think of is: 

1. Define priority for all tests in a test flow class. 

2. Also Define dependsOnMethods for only those tests, for which I want to skip the remaining tests. 

Do you think this will work ? Or do you have a better implementation idea ? 

Thanks in advance !

Regards,
Nisarg

-----------


Regards,
Nisarg
9725626638
-----------------------

   

Krishnan Mahadevan

unread,
Oct 27, 2020, 1:42:12 PM10/27/20
to testng...@googlegroups.com

Like I said before, mixing these two is perhaps going to give you unreliable results. You can give it a try. But I don’t know what will be the expected behavior (especially if you end up assigning a lower priority to a method that as an upstream dependency)

Nisarg Dave

unread,
Oct 27, 2020, 1:49:12 PM10/27/20
to testng...@googlegroups.com
I see. 

But, what if I strictly assign priority from low to high for all test cases in a test flow class & then assign dependsOnMethods for few of them ? Will it create any problem that you can think of @Krishnan ?


Regards,
Nisarg

-----------------------

   

Krishnan Mahadevan

unread,
Oct 27, 2020, 11:03:01 PM10/27/20
to testng...@googlegroups.com
I would suggest you try it out and see the results for yourself.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Nisarg Dave

unread,
Oct 28, 2020, 12:42:21 AM10/28/20
to testng...@googlegroups.com
Alright. I will try it out and update my findings here. Thanks !

Regards,
Nisarg

-----------------------

   

Nisarg Dave

unread,
Nov 2, 2020, 10:31:20 AM11/2/20
to testng-users
Hi Krishnan, 

I tried the way which I was suggesting. But, it is not executing as expected i.e. it executes priority tests first and depends on Tests are getting executed in later stage. Any other approach that you can think of to achieve the functionality ? Thanks in advance.

Regards,
Nisarg

⇜Krishnan Mahadevan⇝

unread,
Nov 3, 2020, 4:54:38 AM11/3/20
to testng-users
No. That is why I was suggesting that you are better off segregating them into two separate units of tests rather than trying to mix and match them both.
You can perhaps create some round about ways of doing it, but all of those would be pretty flaky.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Nisarg Dave

unread,
Nov 3, 2020, 5:02:48 AM11/3/20
to testng...@googlegroups.com
Hmm. I see. Thanks Krishnan for your time and assistance. 

Regards,
Nisarg

-----------------------

   

Reply all
Reply to author
Forward
0 new messages