Reputation: 2050
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++);
}
}
@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
@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
Upvotes: 0
Views: 66
Reputation: 2050
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++);
}
}
Upvotes: 0
Reputation: 7649
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
Upvotes: 1
Reputation: 146
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
Upvotes: 1