Reputation: 7880
So I have Selenium tests that use TestNG to test UI, but right now I'm struggling with a problem : how can I execute a specific method only once before a parameterized test ? I have a parameterized test like this :
@Test(dataProvider="myProvider")
public void testMyThingToTest() {
// Things to test in here
}
My parameterized test works great and gets executed once for each parameter given. Now I have a initialization that is common to all my instances of my parameterized test, so I would like it to be ran only once, not before every instance of the parameterized test... If I do something like using @BeforeTest or @BeforeMethod :
@BeforeMethod
public void initialization() {
// The initialization phase here...
}
The init method is run before every test... any idea how to achieve that ? The purpose of this is to avoid doing some overwhelming job for each parameter, because my initialization takes something like 30 sec, and I have 7-8 parameters for my test, so I would rather have an initialization time of 30sec * 1 rather than 30sec * 8
Upvotes: 3
Views: 2650
Reputation: 11
call function in dataprovider
@DataProvider
public Object[][] FeederOfResponseCodes()
{
initialization();
return new Object[][]
{
};
}
Upvotes: 1
Reputation: 121
Try
@BeforeSuite
public void initialization() {
// Init suite
}
If you want other scenarios. Pick annotation with following priority: @BeforeSuite > @BeforeTest > @BeforeClass > @BeforeMethod
Upvotes: 1
Reputation: 112
If I understood correctly, you want to execute a method only once for all tests that are in you test class.
TestNG provide an annotation for that @BeforeClass
: Annotates methods that will be run before the first method on the current test class is run.
You can find an example at http://java.dzone.com/articles/testng-beforeclass-annotation
Upvotes: 2