'How to run TestNG test several times with BeforeTest execution?

I try to execute the test several times with DataProvider. All works fine, but @BeforeTest and @AfterTest execute only one time, but I need this execution with each iteration.:

public class TestClass{

    @BeforeTest
    public void before(){
        Log.info("BeforeTest");
    }

    @AfterTest
    public void after(){
        Log.info("AfterTest");
    }

    @DataProvider
    public Object[][] tenTime(){
        return new Object[][]{
                {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}
        };
    }
    
    private static Integer c = 0;

    @Test(dataProvider = "tenTime")
    public void tenTimesTest(Integer count){
        Assert.assertSame(count, c++);
    }
}
  • Actual result:
    @BeforeTest and @AfterTest executes once:
[INFO] 19-01-21 11:54:47 main | BeforeTest
[INFO] 19-01-21 11:54:47 main | The tenTimesTest test started
[INFO] 19-01-21 11:54:47 main | tenTimesTest test is starting.
[INFO] 19-01-21 11:54:48 main | The tenTimesTest test succeed
[INFO] 19-01-21 11:54:48 main | tenTimesTest test is passed
[INFO] 19-01-21 11:54:48 main | The tenTimesTest test started
[INFO] 19-01-21 11:54:48 main | tenTimesTest test is starting.
[INFO] 19-01-21 11:54:48 main | The tenTimesTest test succeed
[INFO] 19-01-21 11:54:48 main | tenTimesTest test is passed
..............
[INFO] 19-01-21 11:54:50 main | AfterTest
  • Expected result
    @BeforeTest and @AfterTest executes each time with tenTimesTest:
[INFO] 19-01-21 11:54:47 main | BeforeTest
[INFO] 19-01-21 11:54:47 main | The tenTimesTest test started
[INFO] 19-01-21 11:54:47 main | tenTimesTest test is starting.
[INFO] 19-01-21 11:54:48 main | The tenTimesTest test succeed
[INFO] 19-01-21 11:54:48 main | tenTimesTest test is passed
[INFO] 19-01-21 11:54:50 main | AfterTest
[INFO] 19-01-21 11:54:47 main | BeforeTest
[INFO] 19-01-21 11:54:47 main | The tenTimesTest test started
[INFO] 19-01-21 11:54:47 main | tenTimesTest test is starting.
[INFO] 19-01-21 11:54:48 main | The tenTimesTest test succeed
[INFO] 19-01-21 11:54:48 main | tenTimesTest test is passed
[INFO] 19-01-21 11:54:50 main | AfterTest


Solution 1:[1]

Follow construction is suitable for me:

    @BeforeTest
    public void before(){
        isBeforeTestExecutedFlag = true;
        method();
    }

    private void method(){
        Log.info("BeforeTest");
    }

    bool isBeforeTestExecutedFlag = false;

    @BeforeMethod
    public void beforeMethod(){
        if(isBeforeTestExecutedFlag){
             isBeforeTestExecutedFlag = false;
             return;
        }
        method();
    }

    @AfterTest
    public void after(){
        Log.info("AfterTest");
    }

    @DataProvider
    public Object[][] tenTime(){
        return new Object[][]{
                {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}
        };
    }
    
    private static Integer c = 0;

    @Test(dataProvider = "tenTime")
    public void tenTimesTest(Integer count){
        Assert.assertSame(count, c++);
    }
}

Solution 2:[2]

use @BeforeMethod and @AfterMethod as following:

public class DemoTest {

    private static final Logger log = LoggerFactory.getLogger(DemoTest.class);

    @BeforeMethod
    public void before() {
        log.info("before");
    }

    @AfterMethod
    public void after() {
        log.info("after");
    }

    @Test
    public void test1() {
        log.info("test1");
    }

    @Test
    public void test2() {
        log.info("test2");
    }
}

result:

before
test1
after
before
test2
after

Solution 3:[3]

Even if you're using a data provider, your tenTimesTest is considered a single test, so @BeforeTest will only be executed once. If you need to execute it before every invocation you may try @BeforeMethod. Beware, though, that if you have more tests it will be executed as well

If you need some more fine grained execution control you may need to invoke the method by yourself from the test

You have more info in the docs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Valentyn Hruzytskyi
Solution 2 Jack
Solution 3 Alberto S.