Knowing the DataProvider data in the AfterMethod

1,419 views
Skip to first unread message

NewGuy

unread,
Jun 20, 2012, 3:40:32 PM6/20/12
to testng...@googlegroups.com
I'm sure this has been asked many times but I can't find what I am looking for on the forums.

I need to have a @AfterMethod function clean up data that was allocated by the DataProvider.  Is there a way for the AfterMethod to know what parameters/arguments was passed to the Test method after the fact.

Here is what I am envisioning.

@DataProvider(name = "host")
public Object[][] createData(ITestContext context, Method m) throws Exception {
String host = hosts.getFreeHost();
return new Object[][] {{host}};
}

@AfterMethod 
public void freeHost () {
        hosts.freeHost(host);
}

The only problem is I don't know how to get the "host" parameter after the fact.  I can't use global static variables to track this easily because because I want to run the tests in parallel.  Any suggestions?

Cédric Beust ♔

unread,
Jun 20, 2012, 3:49:00 PM6/20/12
to testng...@googlegroups.com
No need for static, why don't you simply store the data passed to your method in a field?

@Test(dataProvider = ...)
public void test(Host host) {
  this.host = host;
  // test
}

@AfterMethod
public void am() {
  // clean up this.host
}

-- 
Cédric




--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/f4nrUfFOUkoJ.
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.

Krishnan Mahadevan

unread,
Jun 20, 2012, 4:01:16 PM6/20/12
to testng...@googlegroups.com
Cedric,
Wouldnt that cause problems when he tries parallel runs?

I thought perhaps he could use a Factory which is driven by a data provider. So that way each test class could get a host object in its constructor thereby giving the @AfterMethod a free access to the host object without causing issues in parallel runs as well. 

Please correct me if am 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/

Rick Wall

unread,
Jun 20, 2012, 4:09:01 PM6/20/12
to testng...@googlegroups.com
That is what I thought too.  I found the behavior varies between which type of parallel run you use though when it comes to how or when global variables are initialized.  In any case I choose to try something I found on the forum and that was simply 

@AfterMethod
public void freeData (ITestResult tr) {
String host = (String) tr.getParameters()[0];
Host.freeHost(host);
}

Now the other problem I have though.  Is there a way to make an AfterMethod run only when a specific DataProvider has been called?

Krishnan Mahadevan

unread,
Jun 20, 2012, 4:14:02 PM6/20/12
to testng...@googlegroups.com


On Thursday, June 21, 2012, Rick Wall wrote:
That is what I thought too.  I found the behavior varies between which type of parallel run you use though when it comes to how or when global variables are initialized.  In any case I choose to try something I found on the forum and that was simply 

@AfterMethod
public void freeData (ITestResult tr) {
String host = (String) tr.getParameters()[0];
Host.freeHost(host);
}

Now the other problem I have though.  Is there a way to make an AfterMethod run only when a specific DataProvider has been called?

I believe that is possible because @AfterMethod will also receive an instance of java.lang.reflect.Method as another param apart from ITestResult. So it should be easy to find the Test annotation object from the Method object and the. Just query it to find the name of the data provider it ran with.  

Cédric Beust ♔

unread,
Jun 20, 2012, 4:15:24 PM6/20/12
to testng...@googlegroups.com
Ah I missed the parallel bit.

Then store these values in a ThreadLocal field?

-- 
Cédric

Krishnan Mahadevan

unread,
Jun 20, 2012, 4:20:05 PM6/20/12
to testng...@googlegroups.com


On Thursday, June 21, 2012, Cédric Beust ♔ wrote:
Ah I missed the parallel bit.

Then store these values in a ThreadLocal field?

Neat and simple! Why didn't that strike to me :)  

Alexander Poulikakos

unread,
Jun 20, 2012, 5:03:07 PM6/20/12
to testng...@googlegroups.com

Hi

 

I’m wondering if it is possible to create my own annotation, which will be picked up by test-ng? Kind of extending the @Test annotation in a way test-ng recognize it.

 

So my test methods would look something like this.

 

@MyTestMethod

public void testSomething(){

    // test-something

}

 

/Alex

 


Cédric Beust ♔

unread,
Jun 20, 2012, 5:09:42 PM6/20/12
to testng...@googlegroups.com
Not really because then you would still have to implement logic in TestNG that recognizes these annotations and act on them.

What are you trying to do?

-- 
Cédric

Alexander Poulikakos

unread,
Jun 21, 2012, 6:55:59 AM6/21/12
to testng...@googlegroups.com

We use our own testautomation framework with its own annotations. This framework uses test-ng in the background and adds functionality on top of test-ng. Which means the number of annotations per test-method is increasing. I was trying to minimize the number of annotations needed per test-method.

 

It was just a though, and nothing of priority.

 

Thanks for the answer.

 

/Alex

 

 


Krishnan Mahadevan

unread,
Jun 21, 2012, 7:01:46 AM6/21/12
to testng...@googlegroups.com
Alex,
instead of adding more annotations why not add more params to annotations (Just a thought). That way your annotations count would be minimum, but still serve the purpose right ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


Francis Galiegue

unread,
Jun 21, 2012, 7:10:46 AM6/21/12
to testng...@googlegroups.com
On Thu, Jun 21, 2012 at 12:55 PM, Alexander Poulikakos
<alexander....@ericsson.com> wrote:
> We use our own testautomation framework with its own annotations. This
> framework uses test-ng in the background and adds functionality on top of
> test-ng. Which means the number of annotations per test-method is
> increasing. I was trying to minimize the number of annotations needed per
> test-method.
>

At this point, I guess the best you can do is describe what custom
annotations you use and what their purpose is. TestNG has so many of
them that maybe your use cases are already covered.

Also, don't forget about the usefulness of just creating abstract test
classes. For instance, I have tests which I must repeatedly call for
tens of classes but all having functionalities in common: I just
designed an abstract class with the relevant annotations (which are
TestNG annotations, but you can add your own if need be) and actual
test classes are just derived implementations from this abstract
class.

Case in point:

https://github.com/fge/json-schema-validator/blob/master/src/test/java/org/eel/kitchen/jsonschema/syntax/AbstractSyntaxCheckerTest.java
https://github.com/fge/json-schema-validator/blob/master/src/test/java/org/eel/kitchen/jsonschema/syntax/PatternSyntaxCheckerTest.java

--
Francis Galiegue, fgal...@gmail.com
"It seems obvious [...] that at least some 'business intelligence'
tools invest so much intelligence on the business side that they have
nothing left for generating SQL queries" (Stéphane Faroult, in "The
Art of SQL", ISBN 0-596-00894-5)
Reply all
Reply to author
Forward
0 new messages