[英]Testng - How to run cleanup code after a test method is run for all data providers?
I have a testng test (below) where I use one value of "totalAmount" for each array given by the data provider "junkDP". 我有一个testng测试(如下),其中对数据提供者“ junkDP”给出的每个数组使用一个“ totalAmount”值。 I want to reset "totalAmount", ONLY after "test" method has run for each array in "junkDP".
我想重置“ totalAmount”,仅在为“ junkDP”中的每个数组运行“测试”方法之后。 Is this possible in testng ?
在testng中这可能吗? How to do it ?
怎么做 ?
Please note that @AfterMethod & @AfterTest do not do what I want. 请注意,@AfterMethod和@AfterTest不执行我想要的操作。 @AfterMethod resets "totalAmount" before "test" method is run for each array after the first array in "junkDP".
@AfterMethod在“ junkDP”中的第一个数组之后为每个数组运行“测试”方法之前,重置“ totalAmount”。 @AfterTest is run after the @AfterClass.
@AfterTest在@AfterClass之后运行。
The code: 编码:
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");
}
}
Output: 输出:
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
If i understand the question, you are searching for executor after the test method ? 如果我理解这个问题,您是在测试方法之后搜索执行程序吗?
...if yes, depending on the test runner ...如果是,取决于测试跑步者
here you go, how testng is doing it: 在这里,testng的工作方式:
http://testng.org/doc/documentation-main.html#annotations
http://testng.org/doc/documentation-main.html#annotations
@AfterTest or @AfterMethod @AfterTest或@AfterMethod
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.