基于Hibernate对Http接口进行全集测试实践
在测试工作中,一般都很难做到全集测试,通常都是基于边界值、等价类划分的方法进行测试数据的设计。在实际测试中,可能会因为覆盖不全导致一些bug没有测出来。在本文中我准备对一个接口进行全集测试,就是覆盖所有路径。
测试对象
Http接口:http://..../common/getddd.do?d1=D032&d2=D032034&d3=D032055
从接口可以看出传进去的参数有3层组织:d1,d2,d3。d1>d2>d3,d1包含多个d2,d2包含多个d3,d1有多个。所以这边的排列组合是非常多的,全集测试时如果这块测试数据自己直接去组织的话,会非常繁琐。
如下图:
所以,要想办法简化测试数据生成过程。
考虑到,d1,d2,d3的组织关系可以直接到数据库中查关系表来获取,所以打算用Hibernate直接从数据库中取对应关系生成测试数据。
使用Hibernate取组织关系,生成测试数据
从关系表中,取得d1,d2,d3之间的关系,拼装成各种组合。
用Hibernate遍历数据库关系表,通过下面的代码获取某个d1下面所有d2,d3的组合。
public static ArrayList GetList(String deptId){
urllist.add("dept1="+deptId);
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Criteria criteriadep2 = session.createCriteria(DepartmentTab.class);
Criteria criteria2 = session.createCriteria(DepartmentTab.class);
Criterion criterion1 = Restrictions.eq("parentId", deptId);
criteriadep2.add(criterion1);
List dept2List = criteriadep2.list();
for(EhrbiDepartmentTab dept2:dept2List){
urllist.add("dept1="+deptId+"&"+"dept2="+dept2.getDeptId());
dept2IdList.add(dept2.getDeptId());
}
logger.info(dept2IdList);
logger.info(urllist);
for(int i = 0;i dept3List = criteriadep3.list();
for(EhrbiDepartmentTab dept3:dept3List){
urllist.add("dept1="+deptId+"&"+"dept2="+dept2IdList.get(i)+"&"+"dept3="+dept3.getDeptId());
}
}
if(session!=null){
session.close();
}
return urllist;
}
获得下面的组合:[INFO ]16:11:45,833,main,[Class]DatabaseUtil, [Method]GetUrlList, [dept1=D032, dept1=D032&dept2=D032001, dept1=D032&dept2=D032002, dept1=D032&dept2=D032007, dept1=D032&dept2=D032008, dept1=D032&dept2=D032012, dept1=D032&dept2=D032016, dept1=D032&dept2=D032025, dept1=D032&dept2=D032029, dept1=D032&dept2=D032030, dept1=D032&dept2=D032034, dept1=D032&dept2=D032062, dept1=D032&dept2=D032002&dept3=D032009, dept1=D032&dept2=D032002&dept3=D032010, dept1=D032&dept2=D032002&dept3=D032041, dept1=D032&dept2=D032007&dept3=D032014, dept1=D032&dept2=D032008&dept3=D032042, dept1=D032&dept2=D032008&dept3=D032043, dept1=D032&dept2=D032008&dept3=D032044, dept1=D032&dept2=D032008&dept3=D032045, dept1=D032&dept2=D032008&dept3=D032046, dept1=D032&dept2=D032008&dept3=D032047, dept1=D032&dept2=D032012&dept3=D032066, dept1=D032&dept2=D032025&dept3=D032026, dept1=D032&dept2=D032025&dept3=D032027, dept1=D032&dept2=D032025&dept3=D032028, dept1=D032&dept2=D032029&dept3=D032048, dept1=D032&dept2=D032029&dept3=D032049, dept1=D032&dept2=D032029&dept3=D032050, dept1=D032&dept2=D032029&dept3=D032052, dept1=D032&dept2=D032029&dept3=D032053, dept1=D032&dept2=D032029&dept3=D032060, dept1=D032&dept2=D032029&dept3=D032063, dept1=D032&dept2=D032030&dept3=D032031, dept1=D032&dept2=D032030&dept3=D032032, dept1=D032&dept2=D032030&dept3=D032033, dept1=D032&dept2=D032034&dept3=D032036, dept1=D032&dept2=D032034&dept3=D032037, dept1=D032&dept2=D032034&dept3=D032040, dept1=D032&dept2=D032034&dept3=D032054, dept1=D032&dept2=D032034&dept3=D032055, dept1=D032&dept2=D032034&dept3=D032056, dept1=D032&dept2=D032034&dept3=D032057, dept1=D032&dept2=D032034&dept3=D032058, dept1=D032&dept2=D032034&dept3=D032059, dept1=D032&dept2=D032034&dept3=D032061, dept1=D032&dept2=D032034&dept3=D032064]
通过上面的方法,遍历一级组织后,就可以获取全集的测试数据。看下面的代码:
public static ArrayList GetAllList(){
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Criteria criteriadep1 = session.createCriteria(EhrbiDepartmentTab.class);
Criteria criteria2 = session.createCriteria(EhrbiDepartmentTab.class);
Criterion criterion1 = Restrictions.eq("depth", 1);
criteriadep1.add(criterion1);
List dept1List = criteriadep1.list();
for(DepartmentTab dept1:dept1List){
urllist.add("dept1="+dept1.getDeptId());
dept1IdList.add(dept1.getDeptId());
}
for(int i=0;i<dept1IdList.size();i++){
GetUrlList((String)dept1IdList.get(i));
}
session.close();
logger.info(urllist);
return urllist;
}
看下面的返回结果:
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032001", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032002", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032007", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032008", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032012", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032016", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032025", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032029", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032030", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032062", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032002&dept3=D032009", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032002&dept3=D032010", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032002&dept3=D032041", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032007&dept3=D032014", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032008&dept3=D032042", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032008&dept3=D032043", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032008&dept3=D032044", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032008&dept3=D032045", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032008&dept3=D032046", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032008&dept3=D032047", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032012&dept3=D032066", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032025&dept3=D032026", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032025&dept3=D032027", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032025&dept3=D032028", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032029&dept3=D032048", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032029&dept3=D032049", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032029&dept3=D032050", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032029&dept3=D032052", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032029&dept3=D032053", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032029&dept3=D032060", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032029&dept3=D032063", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032030&dept3=D032031", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032030&dept3=D032032", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032030&dept3=D032033", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034&dept3=D032036", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034&dept3=D032037", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034&dept3=D032040", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034&dept3=D032054", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034&dept3=D032055", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034&dept3=D032056", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034&dept3=D032057", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034&dept3=D032058", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034&dept3=D032059", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034&dept3=D032061", 0)
PASSED: GetddTest("...By一级、二级、三级部门", "http://....netease.com/common/getdd.do?dept1=D032&dept2=D032034&dept3=D032064", 0)
===============================================
Default test
Tests run: 47, Failures: 0, Skips: 0
===============================================
这是一个d1下面的遍历结果,可想而知如果是几十个d1级别的组织需要遍历的数量是非常庞大的,如果通过手工编写测试数据的方式是没法进行全集测试的。
结论
通过使用Hibernate从数据库读取关系表的方法来获取多层组织的测试数据,进行全集测试的方法在一些项目中是可以适用的。