2

I've seen similar questions here but none seems to resolve the problem. I've the following code;

   @Test(priority=2)
    public void increaseQty(){
       System.out.println("in increase qty");}

   @Test(priority=2,dependsOnMethods={"increaseQty"})
      public void decreaseQty(){
         System.out.println("in decrease qty");}

   @Test(priority=3)
     public void removeFromCart() throws Exception{
          System.out.println("remove qty");}

    @Test(priority=3,dependsOnMethods={"removeFromCart"})
       public void emptyCart() throws InterruptedException{
             System.out.println("empty Cart");}

expected sequence of run is

increaseQty
decreaseQty
removeFromCart
emptyCart

but the actual sequence is

increaseQty
removeFromcart
decreaseQty
emptyCart

I am not able to understand why its not following the specified sequence. seems to me like its running independent methods first and then the dependent irrespective of the priority , but no such behavior is mentioned anywhere in the documentation. What should i do to make it run in desired sequence

Alpana Chauhan
  • 407
  • 1
  • 9
  • 18

2 Answers2

4

Don't provide priority and depends on together, you can group the tests. You can do it like :

@Test(priority = 1, groups = { "qty" })
    public void increaseQty() {
    System.out.println("in increase qty");
    }

    @Test(dependsOnMethods = { "increaseQty" }, groups = { "qty" })
    public void decreaseQty() {
    System.out.println("in decrease qty");
    }

    @Test(dependsOnGroups = { "qty" })
    public void removeFromCart() throws Exception {
    System.out.println("remove qty");
    }

    @Test(dependsOnMethods = { "removeFromCart" })
    public void emptyCart() throws InterruptedException {
    System.out.println("empty Cart");
    }

Edit: Another work around is to change your testng xml to have desciption like:

<methods> <include name="increaseQty"/> <include name="decreaseQty"/> <include name="removeFromCart"/> <include name="emptyCart"/> </methods>

so testng will execute it in this manner only.

Vivek Singh
  • 3,641
  • 2
  • 22
  • 27
  • I you found this answer correct kindly mark it else for any discrepencies let us know...thanks – Vivek Singh Nov 24 '14 at 13:27
  • your answer resolves the problem but as per my understanding it is not a best practice to add unwanted dependencies in your test. If any method fails in group qty then the rest will be skipped which I don't want – Alpana Chauhan Nov 25 '14 at 06:09
  • The other way u can achieve it is by adding the method names in your testng.xml file in order u want them to execute..like the way i edited in answer or u can name your tests in order like T1_increaseQty, T2_decreaseQty and so on. As far as i have noticed TestNg goes in its execution on the basis of naming of tests in alphabetical order. – Vivek Singh Nov 25 '14 at 06:49
  • Can u plz confirm whether it helped or not? – Vivek Singh Nov 27 '14 at 07:42
  • My IDE tells me that groups is redundant when providing dependsOnMethods – Keith Tyler Jul 07 '17 at 23:12
0

If there are 3 or 4 set of sequence then we need to use both priority as well dependency Like [>>Seq1-create user, modify, delete >>Seq2-create group(empty), modify, delete >>Seq3-create user,create group, add user to group]

in each sequence there is dependency but there is no dependency between sequences.and we need to run each sequence one after other