【问题标题】:Is it possible to run @Test testNG annotation multiple times with a different @BeforeMethod each time?是否可以每次使用不同的@BeforeMethod 多次运行@Test testNG 注释?
【发布时间】:2020-12-30 10:06:42
【问题描述】:

我有 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()。

【问题讨论】:

  • 你能指定每个 beforeMethod 的作用吗?或者是否可以在数据提供者中做这 3 种不同的事情并将结果作为每个测试用例传递给测试方法?
  • 每个 BeforeMethod 将使用不同的用户/通过凭据登录一个用户,而 Test 方法将为每个用户做同样的事情。

标签: testng


【解决方案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 无法完成类似的实现

【讨论】:

  • 感谢您的指导。
猜你喜欢
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
相关资源
最近更新 更多