首页 > 解决方案 > 是否可以每次使用不同的@BeforeMethod 多次运行@Test testNG 注释?

问题描述

我有 3 种方法(“setup1”、“setup2”、“setup3”),每个方法都使用 @BeforeMethod 注释。每种方法都有不同的作用,我想运行方法 Test1() 3 次,每次使用不同的 @BeforeMethod 方法(例如:run1: setup1()>>Test1(), run2: setup2()>>Test1(),运行 3:setup3()>>Test1())。那可能吗?

这是我的代码:

@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 中使用dependOnMethods{}:

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

但是这种方法不起作用,因为它会在到达 Test1() 之前一次运行所有 3 个 @BeforeMethods,而我想要完成的是运行 setup1()>>Test1(),运行 setup2()>>Test1( ) 最后运行 setup3()>>Test1()。

标签: testng

解决方案


您可以创建一个数据提供者方法,在其中您可以创建 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


推荐阅读