testng not running in priority order when dependsOnMethods is specified

1,726 views
Skip to first unread message

Mrunal Gosar

unread,
Jun 26, 2014, 8:29:27 AM6/26/14
to testng...@googlegroups.com

Whenever we specify priority and dependsOnMethods on a @Test annotated method, the order of execution of test methods is not according to the priority. why is so? Here is the test class to demonstrate the issue:

package unitTest.TestNGTestCases;

import org.testng.annotations.Test;

public class TestNGTest1 {
    @Test(priority=1)
    public void t1()
    {
        System.out.println("Running 1");
    }
    @Test(priority=2,dependsOnMethods="t1")
    public void t2()
    {
        System.out.println("Running 2");
    }
    @Test(priority=3,dependsOnMethods="t2")
    public void t3()
    {
        System.out.println("Running 3");
    }
    @Test(priority=4)
    public void t4()
    {
        System.out.println("Running 4");
    }
}

Actual Output :

Running 1
Running 4
Running 2
Running 3

===============================================
All Tests Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================

Expected output :

Running 1
Running 2
Running 3
Running 4

===============================================
All Tests Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================

The order of test execution should have been t1, t2, t3, t4. why is t4 getting executed after t1, when t2 and t3 have higher priority then t4?

TIA

Shawn McCarthy

unread,
Jun 26, 2014, 10:19:07 AM6/26/14
to testng...@googlegroups.com
T2 and T3 need to wait for T1 to complete. T4 doesn't need to wait for anything to complete.

Mrunal Gosar

unread,
Jun 27, 2014, 12:09:05 AM6/27/14
to testng...@googlegroups.com
As far as i think all the test methods should execute according to the priority and dependesOnMethods should just help in specifying whether the dependent method should get executed or not. In this case t4 should wait until and unless t2 and t3 have finished. So are you saying the actual output is correct behaviour in this case?

Jeff

unread,
Jun 27, 2014, 12:31:56 AM6/27/14
to testng...@googlegroups.com
How many threads are you running?  Might be interesting to print the threadId as well.

Regardless, if you really want to force priority, use dependsOnMethods


--
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 post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.



--
Jeff Vincent
See my LinkedIn profile at:
http://www.linkedin.com/in/rjeffreyvincent

Jeff

unread,
Jun 27, 2014, 12:38:58 AM6/27/14
to testng...@googlegroups.com
Oops, I didn't completely read your sample...sorry.  Using just the dependsOnMethods should work however.  What do you need both for?

Krishnan Mahadevan

unread,
Jun 27, 2014, 5:55:52 AM6/27/14
to testng...@googlegroups.com
Here's how TestNG works with respect to @Test methods.

First it executes all "independent" methods.
Then it executes all methods that have a "dependsOnGroups/dependsOnMethods" attribute.

I dont u'stand what is the need to add priority for methods which are driven by dependencies. Why the redundancy ?

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.wordpress.com/

Mrunal Gosar

unread,
Jun 27, 2014, 11:34:32 AM6/27/14
to testng...@googlegroups.com
Hi Krishnan
The requirement is that t1, t2, t3 are linked and dependent on each other. but t4 is not dependent. it is independent. but it should get executed after t1,t2,t3 have executed. t4 happens to be clean up test. which is why i am using priority. And dependsOnMethods is used to link t1, t2, t3 each other such that if t1 fails, t2 and t3 should not get executed. is there any other way to achieve this? TIA

Mrunal Gosar

unread,
Jun 27, 2014, 11:35:22 AM6/27/14
to testng...@googlegroups.com
Please read my reply below to krishnan's concern. there i have specified the requirement. TIA
Message has been deleted

Jon Nelson

unread,
Jun 28, 2014, 12:00:29 PM6/28/14
to testng...@googlegroups.com
Krishnan is correct; your usage of priority here is unwarranted and unnecessary.  You have already enforced execution order on t1/t2/t3 via dependencies in an appropriate way (dependent tests will not execute if dependencies fail).  Per your own admission, "t4 happens to be clean up test. which is why i am using priority."  Cleanup in this case should not be a test, but rather an @afterClass method, which will only run after other tests are complete.  There's no need for priority here; you'll get the order want if you remove priorities and handle cleanup properly. Is there any reason you need cleanup to be an @Test? -jn- 

Mrunal Gosar

unread,
Jun 29, 2014, 4:32:06 AM6/29/14
to testng...@googlegroups.com
Yes the reason is that @AfterClass does not accepts dataprovider. Where as my cleanup activity has several of environments to cleanup which i keep passing to t4 test through dataprovider. here i want to see which environment has not got successfully cleaned when that test instance failed. so i cannot use @afterclass to cleanup test.

Jon Nelson

unread,
Jun 29, 2014, 9:22:05 AM6/29/14
to testng...@googlegroups.com
@dataprovider is really just a convenience feature; it doesn't do anything you couldn't do yourself with a loop.  You can and should use @afterClass rather than use this lack of dataprovider as an excuse to make cleanup a test when it really isn't one.  Alternatively, you could setup the XML to run T3 and T4 in that order, I think that would achieve your goal as well.

Krishnan Mahadevan

unread,
Jun 29, 2014, 9:47:16 AM6/29/14
to testng...@googlegroups.com
I believe that can be easily solved by having a data structure [ List/Map/Set ] that records all things that needs to be cleaned up and then have the @AfterClass annotated method just work on the above mentioned data structure to do the clean up.

The @Test annotated test method [which is powered by the @DataProvider] should persist these values into the data structure apart from being a data driven test method.

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.wordpress.com/


On Sun, Jun 29, 2014 at 6:52 PM, Jon Nelson <jondavi...@comcast.net> wrote:
@dataprovider is really just a convenience feature; it doesn't do anything you couldn't do yourself with a loop.  You can and should use @afterClass rather than use this lack of dataprovider as an excuse to make cleanup a test when it really isn't one.  Alternatively, you could setup the XML to run T3 and T4 in that order, I think that would achieve your goal as well.

--
Reply all
Reply to author
Forward
0 new messages