Sequence of execution in "@Dataprovider and @Factory" annotations. Please provide the solution

1,461 views
Skip to first unread message

M.Sampath kumar

unread,
Aug 8, 2014, 9:57:11 PM8/8/14
to testng...@googlegroups.com
I am M. Sambath Kumar using TestNG for Selenium automation project and for data driven am using @Dataprovider and @Factory annotations. 
I need clarification on "@Dataprovider and @Factory" annotations:

---> I am having 10 rows of data, but while reading the data from excel sheet its not following the proper order(ex: Row4, Row1, Row6, ... ).

--->It is not following the  below sequence, instead its following as Row4, Row1, Row6, Row3, Row2, Row5, Row9, Row10, Row8, Row7.

But its not following the below order sequentially:
Row1 values, Row2 values ,Row3 values, ... Row10 values

Please provide the solution to overcome this issue.

Krishnan Mahadevan

unread,
Aug 8, 2014, 11:02:11 PM8/8/14
to testng...@googlegroups.com
Please include a sample code that you have which we can run to see the problem by ourselves. That would help us help you. 

~ Krishnan

iSent. iPhone. iReget.iTypos!
--
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.

Mrunal Gosar

unread,
Aug 20, 2014, 1:12:29 PM8/20/14
to testng...@googlegroups.com
Hi Sampath
If you are creating test instances using @Factory and @DataProviders then i am sorry it will not run in sequence as you wish, it will run in random order. Courtesy : limitation of TestNG implementation.

Krishnan Mahadevan

unread,
Aug 20, 2014, 1:21:22 PM8/20/14
to testng...@googlegroups.com

Mrunal

I beg to differ with you on that. It's very much possible and I don't think it's any limitation of Testng for sure.

By restricting the data provider thread count one can ensure that the test instances are created one at a time. By using depends on within a test class which the factory produces ordering of methods can be guaranteed.

So what exactly is this limitation you are talking about ?

On Aug 20, 2014 10:42 PM, "Mrunal Gosar" <predator.t...@gmail.com> wrote:
Hi Sampath
If you are creating test instances using @Factory and @DataProviders then i am sorry it will not run in sequence as you wish, it will run in random order. Courtesy : limitation of TestNG implementation.

--

Mrunal Gosar

unread,
Aug 24, 2014, 4:29:51 AM8/24/14
to testng...@googlegroups.com
Hi Krishnan
Please try below code snippet and you will see that while using dataprovider and factory the test instances do not run in order.
 Dataprovider class:

package practise;

import org.testng.annotations.DataProvider;

public class TestFactory
{
@DataProvider
public static Object[][] dp()
{
return new Object[][]{{"Test1"},{"Test2"}};
}
}

Test Class:
 
package practise;

import org.testng.Reporter;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.testng.asserts.LoggingAssert;

public class TestNGNameTest
{
String testName;
static int counter=0;
@Factory(dataProvider="dp",dataProviderClass=TestFactory.class)
public TestNGNameTest(String name)
{
this.testName=name;
}
@Test(groups="test1")
public void test1()
{
LoggingAssert softAssert=new LoggingAssert();
softAssert.assertTrue(true);
Reporter.log("Order of execution : "+(++counter)+" "+this.testName+" "+Thread.currentThread().getStackTrace()[1].getMethodName(),true);
}
@Test(groups="test2")
public void test2()
{
LoggingAssert softAssert=new LoggingAssert();
softAssert.assertTrue(true);
Reporter.log("Order of execution : "+(++counter)+" "+this.testName+" "+Thread.currentThread().getStackTrace()[1].getMethodName(),true);
}
@Override
public String toString()
{
return this.testName;
}
}

Expected output:
Order of execution : 1 Test1 test1
Order of execution : 2 Test1 test2
Order of execution : 3 Test2 test1
Order of execution : 4 Test2 test2

Actual output:
Order of execution : 1 Test2 test1
Order of execution : 2 Test2 test2
Order of execution : 3 Test1 test1
Order of execution : 4 Test1 test2

And the above output is not fix it will some times run Test1 instance first or sometimes Test2 instance.

This is what i was trying to explain. Let me know if you have a solution to this, where we would want test instances to run in the order as they have got added in dataprovider.

Regards
Mrunal

Krishnan Mahadevan

unread,
Aug 26, 2014, 12:46:07 PM8/26/14
to testng...@googlegroups.com

Mrunal

First and foremost my apologies for not understanding completely what you were trying to say. Yes I understand now what you were hinting at. Let me see if I can deep dive into Testng code base to understand if there are any alternatives.

And oh btw thanks for correcting me and giving me a chance to learn something new. 😆

--

Mrunal Gosar

unread,
Aug 26, 2014, 1:39:58 PM8/26/14
to testng...@googlegroups.com
Hi Krishnan
No Problems man. To add to this, when i had investigated i've found that testNG first creates a plan as to what all things to be executed beforehand and then sends it for execution. In this case it is storing in Set. which is why it is not getting executed in an orderly fashion. All the best for your investigation. If you do get something as work around or perfect solution, please post it. (It would be better if you would directly post on testNG documentation for everyone.).

Regards
Mrunal

Vinayak Kolagi

unread,
Sep 4, 2014, 4:09:16 AM9/4/14
to testng...@googlegroups.com
You can implement a Listener class which extends from IMethodInterceptor.
MethodInterceptor basically gives you a list of test cases going to be executed.
In the IMethodInterceptor class you can alter the list accordingly and return the list.

e.g.

      public static class TestListSort implements IMethodInterceptor {
         
           @Override
           public List<IMethodInstance> intercept(List<IMethodInstance> arg0, ITestContext arg1) {
           Collections.sort(arg0, new MyComparator());
           return arg0;
          }
      }

      public static class MyComparator implements Comparator<IMethodInstance> {
           
          @Override
          public int compare(IMethodInstance o1, IMethodInstance o2) {
              MyTest o1MyTest = (MyTest) o1.getInstance();
              MyTest o2MyTest = (MyTest) o2.getInstance();
             
              return o1MyTest.getTestName().compareTo(o2MyTest.getTestName());
          }
      }

~
Vinayak Kolagi

shiva motamarri

unread,
Apr 28, 2020, 8:57:27 AM4/28/20
to testng-users

⇜Krishnan Mahadevan⇝

unread,
Apr 28, 2020, 1:55:16 PM4/28/20
to testng...@googlegroups.com
Shiva,

Not sure what your query is. You just revived a 6 year old post without adding any context etc.,

But am going to assume that you are trying to find out if this issue has been resolved.

Can you please try using the latest released version of 7.1.0 ? If its still a problem, I would suggest that you please file a new issue in https://github.com/cbeust/testng/issues with a simple reproducible sample that can be used to recreate the problem.

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/


--
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.
Reply all
Reply to author
Forward
0 new messages