简体   繁体   中英

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

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

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

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++);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM