在testNG中,我们是否有办法依赖于方法作为父方法的数据驱动测试周期的一部分?
实施例。假设我们需要为每个用户测试登录和注销场景,如下面的代码所示,如何用testng实现这个场景
@Test(dataProvider = "userDataProvider")
public void testLogin(User user){
//Some Logic goes here
}
@Test(dependsOnMethods="testLogin")
public void testLogOut(){
//Some Logic goes here
}
@DataProvider(name ="userDataProvider")
public Object[][] provideData() {
return new Object[][]{
{new User("user1","user1")},
{new User("user2","user2")},
{new User("user3","user3")},
};
}
答案 0 :(得分:2)
您可以使用由数据提供商提供支持的TestNG工厂来实现这一目标。
这是一个示例,向您展示如何执行此操作。
public class TestClass {
private User user;
@Factory(dataProvider = "userDataProvider")
public TestClass(User user) {
this.user = user;
}
@Test
public void testLogin() {
//Some Logic goes here that uses this.user
}
@Test (dependsOnMethods = "testLogin")
public void testLogout() {
//Some Logic goes here that uses this.user
}
@DataProvider(name ="userDataProvider")
public static Object[][] provideData() {
return new Object[][] {
{ new User("user1","user1") },
{ new User("user2","user2") },
{ new User("user3","user3") }
};
}
}