[testng-users] use of @dataProvider method under @Factory method of testNG at @BeforeClass level .

97 views
Skip to first unread message

Sachin

unread,
Aug 17, 2015, 7:22:33 AM8/17/15
to testng...@googlegroups.com


I am using the @Factory annotation of TestNG with @dataProvider method in @BeforeClass level . 

And expecting First iteration of  @beforeclass method is executed then  both the @Test method should executed for same iteration and so on ....  

But observed that after @BeforeClass method execution , 1st @Test method is executed and once these execution is completed, 2nd @Test method is executed 

Not sure what is the issue ? 

public class TestPrackties {
   String userMode;

   @Factory(dataProvider = "userModeProvider")
   public TestPrackties(String userMode) {
       this.userMode = userMode;
   }

   @BeforeClass(alwaysRun = true)
   public void init() throws Exception {
       System.out.println("Before Running the class userMode is " + userMode);
   }

   @Test(groups = "testFactory", description = "test method1")
   public void testMethod1() throws Exception {
       System.out.println("Inside method 1 " + userMode );
   }

   @Test(groups = "testFactory", description = "test method2")
   public void testMethod2() throws Exception {
       System.out.println("Inside method 2 " + userMode);
   }
   

   @DataProvider
   private static String [][] userModeProvider() {
       return new String [][]{
               new String []{"Admin"},
               new String []{"Tenant"},
               new String []{"AdminUser"},
               new String []{"TenantUser"},
       };
   }
}

----------------------------------------------
Actual OutPut :- 

Before Running the class userMode is Tenant
Inside method 1 Tenant
Before Running the class userMode is TenantUser
Inside method 1 TenantUser
Before Running the class userMode is AdminUser
Inside method 1 AdminUser
Before Running the class userMode is Admin
Inside method 1 Admin
Inside method 2 Tenant
Inside method 2 TenantUser
Inside method 2 AdminUser
Inside method 2 Admin

--------------------------------------------------------
Expected Output :-

Before Running the class userMode is Tenant
Inside method 1 Tenant
Inside method 2 Tenant
Before Running the class userMode is TenantUser
Inside method 1 TenantUser
Inside method 2 TenantUser
Before Running the class userMode is AdminUser
Inside method 1 AdminUser
Inside method 2 AdminUser
Before Running the class userMode is Admin
Inside method 1 Admin
Inside method 2 Admin

krishnan.ma...@gmail.com

unread,
Aug 17, 2015, 8:40:36 AM8/17/15
to testng...@googlegroups.com

Please try adding group-by-instances=true to your suite xml file and see if that helps.

-Krishnan Mahadevan

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

--
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.

Sachin Gainewar

unread,
Aug 18, 2015, 8:24:47 AM8/18/15
to testng-users
Thanks krishnan

Hubert Grzeskowiak

unread,
Aug 31, 2015, 10:04:14 AM8/31/15
to testng-users
Hey,

AFAIK the @Before* and @After* methods are guaranteed to run after or before the corresponding suites, tests, classes or methods. However, the order of independent units is not specified. This also applies to explicit dependencies between methods, i.e. having methods test1, test2 and test3 where test3 depends on test2, TestNG could potentially put test1 right between the other two order-wise.

Cheers
Hubert Grzeskowiak
Reply all
Reply to author
Forward
0 new messages