Can anyone explain the difference between a Object[][] DataProvider vs. an Iterable<Object[]> DataProvider?

900 views
Skip to first unread message

djangofan

unread,
Oct 8, 2014, 12:08:09 PM10/8/14
to testng...@googlegroups.com
Can anyone explain the difference between a Object[][] DataProvider  vs.  an Iterable<Object[]> DataProvider?

The TestNG documentation, says the following, but it's not clear enough for me.   The explanation of the Iterator<Object[]>  method is kindof cryptic and hard to understand what he really means in the paragraph.

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

The Data Provider method can return one of the following two types:
  • An array of array of objects (Object[][]) where the first dimension's size is the number of times the test method will be invoked and the second dimension size contains an array of objects that must be compatible with the parameter types of the test method. This is the cast illustrated by the example above.
  • An Iterator<Object[]>. The only difference with Object[][] is that an Iterator lets you create your test data lazily. TestNG will invoke the iterator and then the test method with the parameters returned by this iterator one by one. This is particularly useful if you have a lot of parameter sets to pass to the method and you don't want to create all of them upfront. 

niharika varshney

unread,
Oct 9, 2014, 5:16:39 AM10/9/14
to testng...@googlegroups.com

Iterator would basically be used if your data setup step is time-consuming.

Let's assume you are reading data from an excel, based on which you are doing some processing to generate data - probably invoking a webservice or another io operation. If the processing takes 2 seconds per data and considering you have 200 data,

  1. With Object[][], your first test would run after 400 seconds after all data has been generated
  2. With iterator, if you move your processing logic to next() method, your first test starts after 2 seconds, the test executes and then the next data setup takes place.

So, the choice basically depends on how you want your tests structured if your datasetup is long.

Regards,

Niharika


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

Andy Doan

unread,
Oct 9, 2014, 1:06:31 PM10/9/14
to testng...@googlegroups.com
Hey Jon,

Adding to Niharika's explaination

An Object[][] is an indexed array, while an Iterable<Object[]> is a linked list.

So to create an Object[][] you need index your sets of parameters, Object[0], Object[1] etc. Whereas with an Iterable TestNg walks the linked list. If your test data is already a Java collection, you can convert it to an iterable much more efficiently.

-An

djangofan

unread,
Oct 9, 2014, 2:01:16 PM10/9/14
to testng...@googlegroups.com
Thank you!
So, it sounds like TestNG automatically handles the call to next() for me? So, the DataProvider will block and hand out parameters as they come in from the data source? So, test instances will start as they come available right?
-Jon

djangofan

unread,
Oct 9, 2014, 7:47:58 PM10/9/14
to testng...@googlegroups.com
Hey Andy,
Thanks.  That answers my question fairly well.  What I was really trying to answer for myself is :  what are the use cases where I would use an Iterable vs.  using the standard 2-D object array or even vs.  the 1-D Object[] factory.    Mostly, I am trying to feel things out just in case I can improve an existing DataProvider, where I currently have just been using the Object[][] method.
Based on your answer, Andy, it seems like the Iterable is useful in the case where I would rather not use arrays in my DataProvider and instead, work only with Collection objects, which is preferrable.   If the collection isn't blocking the performance should be the same as the static Object[][].

-Jon


On Thursday, October 9, 2014 10:06:31 AM UTC-7, Andy Doan wrote:

Bill Ross

unread,
Nov 7, 2014, 6:54:20 PM11/7/14
to testng...@googlegroups.com
I wonder if you could have one test adding to the iterator dynamically while another test pulls from it?


On Wednesday, October 8, 2014 9:08:09 AM UTC-7, djangofan wrote:

Pawel Kowalski

unread,
Nov 9, 2014, 2:13:53 AM11/9/14
to testng...@googlegroups.com
Of course you could. Take a look at following example:

public class TestDynamicDataProvider {

    private List<Object[]> arguments = new LinkedList<Object[]>();


    @Test
    public void testProducer(){
        for(int i=0; i<20; i++){
            arguments.add(new Object[] {i});
        }
    }

    @Test(dataProvider = "dp", dependsOnMethods = "testProducer")
    public void testConsumer(int x){
        System.out.println("TestDynamicDataProvider.testConsumer("+x+")");
    }

    @DataProvider
    public Iterator<Object[]> dp(){
        return arguments.iterator();
    }
}

Best regards,
Pawel

--
Reply all
Reply to author
Forward
0 new messages