DataProviders and Parameter Injection

893 views
Skip to first unread message

David Garcia

unread,
Mar 2, 2010, 2:13:23 PM3/2/10
to testng-users
I have a test that gets parameters from a dataProvider, but I would
also like to pass the test context to my test method.

myTestMethod(String someParam1, String someParam2, ITestContext
context)

What would be the best way to use both, dataProviders and parameter
injection at the same time?

All I can think is

Object[][] myDataProvider(ITestContext context) {
return new Object[][] {
{"val1, "val2, context},
{"val3, "val4, context},
}

but i find the solution quite ugly, is there a better way to do it?

Cédric Beust ♔

unread,
Mar 2, 2010, 2:15:53 PM3/2/10
to testng...@googlegroups.com
Just declare the ITestContext parameter in your test method, TestNG will inject the right things...

-- 
Cédric



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




David Garcia

unread,
Mar 2, 2010, 3:09:51 PM3/2/10
to testng-users
It doesn't.
Here's the example:

@Test(dataProvider = "sampleProvider", groups="hostnames")
public void myTest(String arg1, String arg2, ITestContext context) {
assertEquals(arg1, arg2);
}

@DataProvider
public Object[][] sampleProvider() {
return new Object[][] {
{"a", "a"},
{"b", "b"},
};
}

FAILED: myTest(myTest: Wrong number of arguments were passed by the
Data Provider: found 2 but expected 3)
java.lang.IllegalArgumentException: wrong number of arguments

On Mar 2, 11:15 am, Cédric Beust ♔ <cbe...@google.com> wrote:
> Just declare the ITestContext parameter in your test method, TestNG will
> inject the right things...
>
> --

> ***Cédric
> *


>
> On Tue, Mar 2, 2010 at 11:13 AM, David Garcia <david.garcia...@gmail.com>wrote:
>
> > I have a test that gets parameters from a dataProvider, but I would
> > also like to pass the test context to my test method.
>
> > myTestMethod(String someParam1, String someParam2, ITestContext
> > context)
>
> > What would be the best way to use both, dataProviders and parameter
> > injection at the same time?
>
> > All I can think is
>
> > Object[][] myDataProvider(ITestContext context) {
> >   return new Object[][] {
> >       {"val1, "val2, context},
> >       {"val3, "val4, context},
> > }
>
> > but i find the solution quite ugly, is there a better way to do it?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "testng-users" group.
> > To post to this group, send email to testng...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > testng-users...@googlegroups.com<testng-users%2Bunsu...@googlegroups.com>

Cédric Beust ♔

unread,
Mar 2, 2010, 5:06:38 PM3/2/10
to testng...@googlegroups.com
Oops, you're right, this has actually never been implemented, it only works for @Parameters at the moment.

I'll look into it.

-- 
Cedric



To unsubscribe from this group, send email to testng-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric


Cédric Beust ♔

unread,
Mar 3, 2010, 12:06:56 PM3/3/10
to testng...@googlegroups.com, david.g...@gmail.com
Hi David,

I just implemented this feature, can you try the beta at http://testng.org/beta and let me know how it works for you?

Thanks.

--
Cedric


On Tue, Mar 2, 2010 at 12:09 PM, David Garcia <david.garcia.mx@gmail.com> wrote:
To unsubscribe from this group, send email to testng-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric


Reply all
Reply to author
Forward
0 new messages