I played with TestNG and found that @AfterMethod and @BeforeMethod is called more than once when I use dataProvider. Is it possible to call the method only once after running @Test with all the parameters passed from dataProvider. Like we can call the tearDown method only once, when "testPrimeNumberChecker" called 5 times using dataProvider.
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ParamTestWithDataProvider1 {
private PrimeNumberChecker primeNumberChecker;
private static final Logger logger = Logger.getLogger(ParamTestWithDataProvider1.class);
@BeforeMethod
public void initialize() {
logger.info("Before Method Fired !! - " );
primeNumberChecker = new PrimeNumberChecker();
}
@@AfterMethod
public void tearDown() {
logger.info("After Method Fired !! " );
}
@DataProvider(name = "test1")
public static Object[][] primeNumbers() {
return new Object[][] { { 2, true }, { 6, false }, { 19, true },
{ 22, false }, { 23, true } };
}
@Test(dataProvider = "test1")
public void testPrimeNumberChecker(Integer inputNumber,
Boolean expectedResult) {
logger.info(inputNumber + " " + expectedResult);
Assert.assertEquals(expectedResult,
primeNumberChecker.validate(inputNumber));
}
}
source
share