How to use DataProvider with @BeforeTest

3,448 views
Skip to first unread message

Vasty Jay

unread,
Mar 24, 2014, 2:07:34 PM3/24/14
to testng...@googlegroups.com
I set a database row in my beforeTest() method as follows.
@BeforeTest
public void beforeTest() {
   setDataRow("DATABASE_ROW", 5);
}
 
I would like to parameterize the value 5 because I want a different database row to
be set for each iteration

Is there a way in TestNg that I can pass a value from a Dataprovider method to a method
annotated with
@BeforeTest
@BeforeClass
@BeforeMethod

For example doing something like this:
@BeforeTest(dataProvider="dataRows")
public void beforeTest(int row) {
    setDataRow("DATABASE_ROW", row);
}  
 
 
@DataProvider(name = "dataRows")
public Object[][] getData(){
  Object[][] data = new Object[2][1];
  data[0][0]=5;
  data[1][0]=4;
  return data;
}
 
Thanks
Vasty

 

Bryan

unread,
Mar 24, 2014, 4:03:03 PM3/24/14
to testng...@googlegroups.com
Hi, It's a good question!

I'd like to make sure I understand what you are thinking about. So, is it like this:

You have several tests, and you have a fixture for those tests using @BeforeTest and @AfterTest for setup and teardown, respectively.

You'd like to run this whole thing once for every datapoint that the dataprovider provides.


Is this correct?






--
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.

Vasty Jay

unread,
Mar 24, 2014, 4:48:07 PM3/24/14
to testng...@googlegroups.com

Hi Bryan,
Thanks for your quick response.
Yes that's exactly what I want to do.

You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/jyaaHRCQu2s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.

Krishnan Mahadevan

unread,
Mar 24, 2014, 9:20:26 PM3/24/14
to testng...@googlegroups.com
Have you considered using a @Factory + @DataProvider to get this done ?

Your DataProvider would basically provide values that your factory method requires to create TestClass instances. Within your TestClass instance you can then have a @BeforeClass or a @BeforeMethod work with the values received while instantiation and proceed. Would that work ?


iSent. iPhone. iReget. iThumbs.iTypos!

Vasty Jay

unread,
Mar 25, 2014, 7:53:27 AM3/25/14
to testng...@googlegroups.com
Hi Krishnan,
Thanks for your suggestion, I just need to figure out how to combine the two annotations together to achieve the desired result.
An example will be great.
 
Thanks

Krishnan Mahadevan

unread,
Mar 25, 2014, 7:56:59 AM3/25/14
to testng...@googlegroups.com
Please refer to the scenario 3 section of my blog post : http://rationaleemotions.wordpress.com/2013/07/31/pretty-printing-with-testng/

You should see a sample there.

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/

Vasty Jay

unread,
Mar 25, 2014, 12:55:04 PM3/25/14
to testng...@googlegroups.com
Thanks Krishnan

jennife...@ringcentral.com

unread,
Jun 15, 2018, 3:06:53 AM6/15/18
to testng-users
Hello ,I have the same requirement, 
I can't open this post: http://rationaleemotions.wordpress.com/2013/07/31/pretty-printing-with-testng/
could you help give an example.
Reply all
Reply to author
Forward
0 new messages