testng中provider+factory数据驱动

1.新建一个factory

public class TestFactory {
  @DataProvider(name="userpass")
  public Object[][] userdata() {
      return new Object[][]{{"zhangsan","123456"},{"lisi","111111"}};
  }
  @Factory(dataProvider="userpass")
  public Object[] TestFactory(String username,String userid) {
      return new Object[]{new UserTest(username,userid)};
  }
}

2.创建usertest的class

  public  UserTest(String username,String userid) {
      this.username = username;
      this.userid = userid;
  }
  @Test
  public void login(){
      System.out.println("login with username = "+username+"and userid = "+userid);
  }
  @Test
  public void publish(){
      System.out.println("publish with username = "+username+"and userid = "+userid);
  }
}

3.执行结果,这样可保证多用户执行用例时数据不错乱

testng中provider+factory数据驱动_第1张图片
结果

你可能感兴趣的:(testng中provider+factory数据驱动)