Adding support for cartesian dataproviders

259 views
Skip to first unread message

Mark Derricutt

unread,
Aug 7, 2012, 12:00:04 AM8/7/12
to testn...@googlegroups.com
Cedric,

I was just updating some tests and came up with a pattern that I was thinking would be good to include in TestNG directly. The concept is a simple cartesian product support for data providers.

In my test I know have three providers, the first two providing discreet sets of data, with their concerns separated out, the third being the cartesian procut:

@DataProvider
public Object[][] provideBillingReferences() {
return new Object[][] {...};
}

@DataProvider
public Object[][] provideSplitData() {
return new Object[][] {...};
}

@DataProvider
public Iterator<Object[]> provideMergedSplitData() {
return DataProviderUtil.cartesianProviderFrom(ImmutableList.of(provideSplitData(), provideBillingReferences()));
}

@Test(dataProvider = "provideMergedSplitData")
….


What I was thinking was this could be handy to incorporate directly into TestNG then I could do something

@Test(dataProvider = {"provideBillingReferences", "provideSplitData"})

and have TestNG handle the cartesian itself. Thoughts?

My current implementation of this uses Google Guava:

public class DataProviderUtil {

/**
* Given a list of @DataProvider results, generate a cartesian product of available combinations.
* @param dataProviderData A list of @DataProvider results
* @return The cartesian product of available combinations.
*/
public static Iterator<Object[]> cartesianProviderFrom(List<Object[][]> dataProviderData) {

ImmutableList.Builder<Set<Object[]>> cartesianSets = ImmutableList.builder();
for (Object[][] objects : dataProviderData) {
cartesianSets.add(Sets.newHashSet(objects));
}

Set<List<Object[]>> cartesianData = Sets.cartesianProduct(cartesianSets.build());
List<Object[]> data = Lists.newArrayList();

for (List<Object[]> objects : cartesianData) {
Object[] mergedArray = flattenObjectArray(objects);
data.add(mergedArray);
}

return data.iterator();
}

private static Object[] flattenObjectArray(List<Object[]> arrays) {

int len = 0;
for (Object[] array : arrays) {
len += array.length;
}

int index = 0;

Object[] mergedArray = new Object[len];

for (Object[] array : arrays) {
for (int i = 0; i < array.length; i++) {
mergedArray[index++] = array[i];
}
}

return mergedArray;
}

}


Cédric Beust ♔

unread,
Aug 7, 2012, 12:04:42 AM8/7/12
to testn...@googlegroups.com
Hey Mark,

Happy to add your DataProviderUtils class to TestNG, mind sending me a pull request?

-- 
Cédric






--
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To post to this group, send email to testn...@googlegroups.com.
To unsubscribe from this group, send email to testng-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-dev?hl=en.


Mark Derricutt

unread,
Aug 7, 2012, 5:26:26 AM8/7/12
to testn...@googlegroups.com
I'll try and clean it up and remove the Guava dependency ( I'm not sure if TestNG currently have any Guava dependencies at all? I don't think it does ).

Currently it feels a little... wrong to add the class/dependency for just one method so I may hold off for now.

Cédric Beust ♔

unread,
Aug 7, 2012, 7:43:33 AM8/7/12
to testn...@googlegroups.com
Indeed, I had to remove the Guava (and Guice) dependency from TestNG because it was close to impossible to have those and still produce a jar file that works for both ant and maven.

-- 
Cédric




--

Karel Rank

unread,
Sep 17, 2012, 5:34:07 AM9/17/12
to testn...@googlegroups.com, ced...@beust.com
Any progress regarding combinations?

Mark Derricutt

unread,
Sep 17, 2012, 6:27:21 PM9/17/12
to testn...@googlegroups.com
To be honest I got sidetracked with other things to try and refactor it to not use Guava.

For now one could simply pull in the class to their own project. ( Or I could extract it to an artifact and push it to maven central ).

If I get some time this week I'll see if I can rework it tho.

On 17/09/2012, at 9:34 PM, Karel Rank <karel...@gmail.com> wrote:

> Any progress regarding combinations?

Roman Hiden

unread,
Oct 5, 2012, 4:31:23 PM10/5/12
to testn...@googlegroups.com
Thanks that would be helpful at least for me. Instead of creating manually all the combinations you could just rely on Cartesian provider
Reply all
Reply to author
Forward
0 new messages