testng - 是否可以每次使用不同的@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()。
解决方案
您可以创建一个数据提供者方法,在其中您可以创建 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
推荐阅读
- ansible - 如果键在ansible play中的dict中不可用,如何设置默认值
- c# - 如何在itext7中居中对齐并设置粗体文本
- python - 比较和量化一组非线性数据中的相似性
- content-management-system - 在 Hybris 中,如何在没有自定义组件的情况下呈现新的 CMS 页面?
- php - Symfony 引导主题
- c# - List 中的 Foreach 仅在迭代后的不同条件下返回相同的记录
- python - python)如何将python 1D列表划分为恒定数量的随机大小python列表(2D)
- python - 试图在 python 3.9 中识别 Django 3.2 应用程序
- node.js - Github 操作 - 错误:进程已完成,退出代码
- javascript - 我应该如何在其他输入字段中获取 base64 格式的图像?