How to preserve method dependency within DataProvider data element

1,032 views
Skip to first unread message

Kenji

unread,
Aug 15, 2009, 1:29:30 AM8/15/09
to testng-users
Hi,

I see similar topics in this news-groups about my problem but I cannot
find a definitive answer. The following code snippet is the
modification of the example of the website.

public class TestNG {
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
{ "Cedric", new Integer(36) },
{ "Anne", new Integer(37)},
};
}

//This test method declares that its data should be supplied by the
Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
System.out.println("test1:"+n1 + " " + n2);
}

// this method depends on completion of test1
@Test(dataProvider = "test1", dependsOnMethods = { "verifyData1" })
public void verifyData2(String n1, Integer n2) {
System.out.println("test2:"+n1 + " " + n2);
}
}

I execute them right now with TestNG5.9. And I got the following
results:

test1: Cedric 36
test1: Anne 37
test2: Cedric 36
test2: Anne 37

However, I want this to be

test1: Cedric 36
test2: Cedric 36
test1: Anne 37
test2: Anne 37

In other words, I would like to fix the data element in DataProvider
and execute methods in order defined by its method dependency. Are
there any easy way to do that?

Of course, I could combine verifyData1 and verifyData2 but I would
like to separate them since verifyData1 is a kind of setup method for
verifyData2.

Thanks.
Kenji

Cédric Beust ♔

unread,
Aug 15, 2009, 1:49:52 AM8/15/09
to testng...@googlegroups.com
Hi Kenji,

No, this is not possible since it would violate the dependency (in your example, test2 gets invoked even though there are more invocations of test1 to come)...

You will have to group the code in one test method...

--
Cedric
--
Cédric


Kenji

unread,
Aug 16, 2009, 12:41:51 AM8/16/09
to testng-users
Hi, Cedric,

Thank you for a quick answer. Are there any plan to implement this
feature? What I am thinking is to have another annotation like
dependsOnMethodsForDataProvider (I cannot think of good name). I have
a lot of data driven test cases and would like to have setup/teardown
for each test case.

For now, what I am doing is to have a wrapper to generate in-memory
testng.xml with data provider element as parameters (without using
data provider functionality) and execute it. This way I can preserve
the method dependency for each parameter element. For the example
below, the wrapper would generate the following in memory testng.xml
and execute it.

<suite name="TestSuite">
<test name="Test1">
<parameter name="n1" value="Cedric"/>
<parameter name="n2" value="36"/>
<classes>
<class name="TestNG"/>
</classes>
</test>
<test name="Test2">
<parameter name="n1" value="Anne"/>
<parameter name="n2" value="37"/>
<classes>
<class name="TestNG"/>
</classes>
</test>
</suite>


This works but it is not clear solution since I have to work on such a
wrapper and I cannot use DataProvider anymore.

Thanks.
Kenji
> ***Cédric
> *

Kenji

unread,
Sep 1, 2009, 10:37:04 AM9/1/09
to testng-users
Hi, Cedric,

Do you have any opinion about this? Are there any way to implement
this (like using method interceptor?)

Thanks.
Kenji
Reply all
Reply to author
Forward
0 new messages