TestNG @AfterMethod with DataProvider

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));    
    }    
}
+4
source share
2 answers

One way could be to use @BeforeGroups and @AfterGroups. Make a test drop in the group, and then use the group annotation before / after to configure / disassemble once for the tests.

+1

: TestListener tearDown (.. TearDownListener).
onTestFailure onTestSuccess .
TestListener , tearDownListener.

+1

Source: https://habr.com/ru/post/1534548/


All Articles