У меня есть тестовый тест (ниже), где я использую одно значение «totalAmount» для каждого массива, предоставленного поставщиком данных «junkDP».Я хочу сбросить «totalAmount», ТОЛЬКО после запуска метода «test» для каждого массива в «junkDP».Это возможно в testng?Как это сделать?
Обратите внимание, что @AfterMethod & @AfterTest не делают то, что я хочу.@AfterMethod сбрасывает «totalAmount» перед тем, как метод «test» запускается для каждого массива после первого массива в «junkDP».@AfterTest запускается после @ AfterClass.
Код:
import org.testng.annotations.DataProvider;
import org.testng.annotations.*;
public class JUNKdP {
@DataProvider( name = "junkDP")
public static Object[][] junkDP() {
Object [] [] dataSet = new Object[][] {
new Object[] {1, 2},
new Object[] {3, 4},
new Object[] {5, 6}};
return dataSet;
}
}
public class JUNK {
private int totalAmount = 0;
@BeforeClass
public void beforeClass(){System.out.println("BeforeClass\n");}
@BeforeMethod
public void beforeMethod(){
System.out.println("BeforeMethod\n");
}
@AfterMethod
public void afterMethod(){
System.out.println("AfterMethod\n");
}
@AfterTest
public void afterTest(){
System.out.println("AfterTest\n");
this.totalAmount = 0;
System.out.println("Reset total amount to 0");
}
@AfterClass
public void afterClass(){System.out.println("AfterClass\n");}
@Test(dataProvider = "junkDP", dataProviderClass = JUNKdP.class, enabled = true)
public void test(int a, int b){
System.out.println("Test method");
int sum = a + b;
System.out.println("Sum: " + sum);
this.totalAmount = this.totalAmount + sum;
System.out.println("totalAmount: " + this.totalAmount + "\n");
}
}
Выход:
BeforeClass
BeforeMethod
Test method
Sum: 3
totalAmount: 3
AfterMethod
BeforeMethod
Test method
Sum: 7
totalAmount: 10
AfterMethod
BeforeMethod
Test method
Sum: 11
totalAmount: 21
AfterMethod
AfterClass
AfterTest
Reset total amount to 0