testNG的参数化测试有两种方式:xml和dataprovider.个人更喜欢dataprovider,因为我喜欢把测试数据放在数据库里。
一.返回类型是Iterator<Object[]>,不用管有多少组测试数据,可以抽取出来以map的id为参数作为公用的提取数据方法。
@DataProvider(name="loginData")
private Iterator<Object[]> LoginDataProvider() throws IOException {
List<Object[]> result=new ArrayList<Object[]>();
SqlSession session=DatabaseUtil.getSqlSession();
List<Object> alldata=session.selectList("loginTestAll");
Iterator it=alldata.iterator();
while(it.hasNext()){
result.add(new Object[] { it.next() });
}
return result.iterator();
}
二.返回类型是Object[][],明确知道有几组测试数据
@DataProvider(name="loginData")
private Object[][] LoginDataProvider() throws IOException {
Object[][] result=null;
SqlSession session=DatabaseUtil.getSqlSession();
result=new Object[][]{{session.selectOne("loginTest",1)},{session.selectOne("loginTest",2)}};
return result;
}
测试调用
@Test(groups="login",dataProvider = "loginData")
public void loginTestCase(LoginTest loginTest) throws IOException {
//用测试数据发起请求,获取响应
String response=getResult(loginTest);
//响应断言
JSONObject rj=new JSONObject(response);
String code=rj.getInt("code")+"";
Assert.assertEquals(code,loginTest.getExpected());
}