Current Method from ITestContext / Default DataProvider

950 views
Skip to first unread message

Nikolai Prokoschenko

unread,
Aug 18, 2008, 3:46:52 AM8/18/08
to testng-users
Hello,

my current test suite consists of tests which require a major product
version number as a parameter so that backward compatibility tests can
be easily implemented and started (there is a bigger testing framework
in the background, so I need it this way). I'm trying to implement a
generic DataProvider for my tests with a custom annotation Versions,
which defines a string array with all validated product versions for a
test. I want these strings to be parsed by a common DataProvider. I
can't use Parameters, since tests should run several times (AFAIK not
possible with Parameter annotation)

1. According to the source code, both DataProvider and Test
annotations have an empty string as name and dataProvider
respectively. So I concluded, I could just define a DataProvider
without a name in a basic class and so define a "default" DataProvider
this way which would work for every test. However this didn't work.
Where have I thought wrong?

2. The default DataProvider can take a ITestContext parameter.
However, I could not find a way to get current test method from the
context -- all I get is current test class (not enough to parse
annotations) and a list of all test methods (no way to find out which
is the current one). Any way this might be possible?

Thanks.

Cédric Beust ♔

unread,
Aug 18, 2008, 1:36:57 PM8/18/08
to testng...@googlegroups.com
On Mon, Aug 18, 2008 at 12:46 AM, Nikolai Prokoschenko <nikolai.pr...@gmail.com> wrote:

Hello,

my current test suite consists of tests which require a major product
version number as a parameter so that backward compatibility tests can
be easily implemented and started (there is a bigger testing framework
in the background, so I need it this way). I'm trying to implement a
generic DataProvider for my tests with a custom annotation Versions,
which defines a string array with all validated product versions for a
test. I want these strings to be parsed by a common DataProvider. I
can't use Parameters, since tests should run several times (AFAIK not
possible with Parameter annotation)

1. According to the source code, both DataProvider and Test
annotations have an empty string as name and dataProvider
respectively. So I concluded, I could just define a DataProvider
without a name in a basic class and so define a "default" DataProvider
this way which would work for every test. However this didn't work.
Where have I thought wrong?

There is no such thing as a "default data provider".  If you don't provide a name to the @DataProvider annotation, it will be set to the name of the method.
 

 
2. The default DataProvider can take a ITestContext parameter.
However, I could not find a way to get current test method from the
context -- all I get is current test class (not enough to parse
annotations) and a list of all test methods (no way to find out which
is the current one). Any way this might be possible?

I just realized that the documentation didn't mention that you can also declare a Method parameter in your data provider signature and it will be automatically injected by TestNG with the method that is about to be invoked.  Sorry about this.

  @DataProvider
  public Object[][] f(Method m) {
    System.out.println("METHOD:" + m);
    return new Object[][] {
        new Object[] { 1 }
    };
  }

Does this help?

--
Cédric

Nikolai Prokoschenko

unread,
Aug 19, 2008, 3:45:21 AM8/19/08
to testng-users
On 18 Aug., 19:36, Cédric Beust ♔ <cbe...@google.com> wrote:
> On Mon, Aug 18, 2008 at 12:46 AM, Nikolai Prokoschenko <
> > 1. According to the source code, both DataProvider and Test
> > annotations have an empty string as name and dataProvider
> > respectively. So I concluded, I could just define a DataProvider
> > without a name in a basic class and so define a "default" DataProvider
> > this way which would work for every test. However this didn't work.
> > Where have I thought wrong?
> There is no such thing as a "default data provider".  If you don't provide a
> name to the @DataProvider annotation, it will be set to the name of the
> method.

Is there any way I could implement such a data provider? I've looked
into implementing a IAnnotationTransformer, but I can't change the
dataProvider setting in the @Test annotation.

> I just realized that the documentation didn't mention that you can also
> declare a Method parameter in your data provider signature and it will be
> automatically injected by TestNG with the method that is about to be
> invoked.  Sorry about this.

Excellent! I've just implemented that, works like a charm! Now I have
just one data provider, which parses my custom annotation and return
the appropriate data. Thank you!

Cédric Beust ♔

unread,
Aug 19, 2008, 10:34:52 AM8/19/08
to testng...@googlegroups.com
On Tue, Aug 19, 2008 at 12:45 AM, Nikolai Prokoschenko <nikolai.pr...@gmail.com> wrote:
Is there any way I could implement such a data provider? I've looked
into implementing a IAnnotationTransformer, but I can't change the
dataProvider setting in the @Test annotation.

Actually you can:  look for "annotation transformers" in the documentation, this should solve your problem.

 

> I just realized that the documentation didn't mention that you can also
> declare a Method parameter in your data provider signature and it will be
> automatically injected by TestNG with the method that is about to be
> invoked.  Sorry about this.

Excellent! I've just implemented that, works like a charm! Now I have
just one data provider, which parses my custom annotation and return
the appropriate data.  Thank you!

Great.

--
Cédric

Cédric Beust ♔

unread,
Aug 19, 2008, 10:36:43 AM8/19/08
to testng...@googlegroups.com
On Tue, Aug 19, 2008 at 7:34 AM, Cédric Beust ♔ <cbe...@google.com> wrote:


On Tue, Aug 19, 2008 at 12:45 AM, Nikolai Prokoschenko <nikolai.pr...@gmail.com> wrote:
Is there any way I could implement such a data provider? I've looked
into implementing a IAnnotationTransformer, but I can't change the
dataProvider setting in the @Test annotation.

Actually you can:  look for "annotation transformers" in the documentation, this should solve your problem.

By the way, this is another example where Mark's idea of custom annotations (not implemented at the moment) would work as well.  You could define @MyTest as being equivalent to @Test(annotationTransformer="foo") and then use @MyTest everywhere you need that annotation transformer.

--
Cédric

Nikolai Prokoschenko

unread,
Aug 19, 2008, 11:34:12 AM8/19/08
to testng-users
On 19 Aug., 16:34, Cédric Beust ♔ <cbe...@google.com> wrote:
> On Tue, Aug 19, 2008 at 12:45 AM, Nikolai Prokoschenko <

> > Is there any way I could implement such a data provider? I've looked
> > into implementing a IAnnotationTransformer, but I can't change the
> > dataProvider setting in the @Test annotation.

> Actually you can:  look for "annotation transformers" in the documentation,
> this should solve your problem.

Well, I did -- it's also written in the quoted text ;-) I can read
annotations, but it seems there is no setDataProvider in ITest:

public class DataProviderParser implements IAnnotationTransformer {
public void transform(ITest annotation, Class testClass, Constructor
testConstructor, Method method) {
annotation.setDataProvider("default"); // does not exist!
}
}

Can you give me a small example how this might be possible? Maybe I
need a more current TestNG version? Thanks.

Nikolai.

Cédric Beust ♔

unread,
Aug 19, 2008, 11:38:02 AM8/19/08
to testng...@googlegroups.com
On Tue, Aug 19, 2008 at 8:34 AM, Nikolai Prokoschenko <nikolai.pr...@gmail.com> wrote:

On 19 Aug., 16:34, Cédric Beust ♔ <cbe...@google.com> wrote:
> On Tue, Aug 19, 2008 at 12:45 AM, Nikolai Prokoschenko <

> > Is there any way I could implement such a data provider? I've looked
> > into implementing a IAnnotationTransformer, but I can't change the
> > dataProvider setting in the @Test annotation.

> Actually you can:  look for "annotation transformers" in the documentation,
> this should solve your problem.

Well, I did -- it's also written in the quoted text ;-) I can read
annotations, but it seems there is no setDataProvider in ITest:

Oh indeed, sorry, that's an unfortunate oversight :-(

You should be able to cast it to TestAnnotation and use the setDataProvider() there, but that's a hack.  I'll have to fix that, but adding methods to interfaces is tricky.  I'll need to think about that more.

--
Cédric

Ankur Sharma

unread,
Apr 15, 2015, 7:34:05 PM4/15/15
to testng...@googlegroups.com

I tried this "You should be able to cast it to TestAnnotation and use the setDataProvider() there, but that's a hack"  and this is throwing "org.testng.internal.annotations.ConfigurationAnnotation cannot be cast to org.testng.annotations.ITestAnnotation" exception. Any thoughts/suggestions?

Krishnan Mahadevan

unread,
Apr 20, 2015, 11:13:34 AM4/20/15
to testng...@googlegroups.com
Ankur,

Can you please share what does your test code look like ? That would basically add some details into what you tried and where you are going wrong.

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 Scribbings @ http://rationaleemotions.wordpress.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.
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.

Reply all
Reply to author
Forward
0 new messages