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
    


  • 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
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2