您可以使用 TestNG Factory
。例如:
关于工厂方法
public class TestClass {
private final int p1;
private final int p2;
public TestClass(int p1, int p2) {
this.p1 = p1;
this.p2 = p2;
}
@Factory(dataProvider = "inputList")
public static Object[] createTestClasses() {
return new Object[]{
new TestClass(1, 1),
new TestClass(1, 0),
new TestClass(1, -1),
};
}
@Test
public void A() {
// test `A` code, stores result in some class member field
}
@Test(dependsOnMethods = {"A"})
public void B() {
// test `B` code, uses stored result from test `A`
}
}
在构造函数上
public class TestClass {
private final int p1;
private final int p2;
@Factory(dataProvider = "inputList")
public TestClass(int p1, int p2) {
this.p1 = p1;
this.p2 = p2;
}
@DataProvider
public static Object[][] inputList() {
return new Object[][]{
{1, 1},
{1, 0},
{1, -1}
};
}
@Test
public void A() {
// test `A` code, stores result in some class member field
}
@Test(dependsOnMethods = {"A"})
public void B() {
// test `B` code, uses stored result from test `A`
}
}
请参阅TestNG 文档页面中的工厂。