[英]Is there a way to get method meta data when using @BeforeMethod in TestNG?
[英]Data Providers for BeforeMethod capabilities
有没有办法为BeforeMethod
function 使用数据提供程序? 我想对许多不同的设备运行并行测试,所以我想使用参数来设置功能。 但我想要一种不同于使用testng.xml
的方式。
您可以通过Object[]
参数在BeforeMethod
中获取数据提供者传递给测试的数据。
@BeforeMethod
public void beforeMethod(Object[] data) {
//.......
}
假设以下代码:
@Test(dataProvider = "dataOne")
public void testMethodOne(String one, int two) {
}
@Test(dataProvider = "dataTwo")
public void testMethodTwo(int one) {
}
@DataProvider
public Object[][] dataOne() {
return new Object[][]{ {"a", 1} };
}
@DataProvider
public Object[][] dataTwo() {
return new Object[][]{ {1} };
}
为了在数据提供者到达测试方法之前获取数据提供者传递的数据,您定义了一个 before 方法,如下所示。 我也添加了Method m
参数。 这将有助于识别正在运行的测试用例。 Object[] data
包含数据提供者传递的数据。 如果你添加了这个参数,那么 testNG 会自动将数据传递给 before 方法。
@BeforeMethod
public void beforeMethod(Method m, Object[] data) {
if(m.getName().equals("testMethodOne")) {
String x = (String) data[0];
int y = (int) data[1];
} else if(m.getName().equals("testMethodTwo")) {
int x = (int) data[0];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.