是否可以使用不同的@BeforeMethod多次运行@Test testNG注释



我有三个方法("setup1"、"setup2"、"setup.3"(,我在每个方法上都使用@BeforeMethod注释。每个方法都做一些不同的事情,我想运行方法Test1((3次,每次都使用不同的@BeforeMethod方法(例如:run1:setup1((>gt;测试1((,运行2:setup2((>gt;测试1((,运行3:setup3((>gt;测试1(((。这可能吗?

这是我的代码:

@BeforeMethod
public void setup1() {
//do something
}
@BeforeMethod 
public void setup2() {
//do something else
}
@BeforeMethod 
public void setup3() {
//do something else
}    
@Test
public void Test1() {
//do something    
}

我试过在@Test:中使用dependsMonethods{}

@Test(dependsOnMethods = {"setup1","setup2", "setup3"})
public void Test1(){
//do something
} 

但这种方法不起作用,因为在进入Test1((之前,它将同时运行所有3@BeforeMethods,而我想完成的是运行setup1((>gt;测试1((,运行setup2((>gt;测试1((并最终运行setup3((>gt;测试1((。

您可以创建一个数据提供程序方法,在其中可以创建3个不同的用户并将其传递给测试方法。因此,将为每个用户调用测试方法。

@Test(dataProvider = "users")
public void Test1(User user) {
//do something with user   
}
@DataProvider
public Object[][] users(){
User user1 = null; //replace the null with code to create user1
User user2 = null; //replace the null with code to create user2
User user3 = null; //replace the null with code to create user3
return new Object[][]{{user1},{user2},{user3}};
}

我不认为使用@BeforeMethod可以实现类似的实现

最新更新