白盒测试

关于白盒测试的详细介绍,请参考百度百科-白盒测试,接下来我将介绍我在项目中所用到的白盒测试方法

语句覆盖:

String tea_id = request.getParameter("user_id");
        String work_id = request.getParameter("work_id");
        String stu_id = request.getParameter("stu_id");
        String type = request.getParameter("type");
        System.out.println("type "+type);
        Comment comm = TeacherDao.getComment(work_id, stu_id, tea_id);
    if("score".equals(type)){
        comm.setComment_text("");
    }
response.getWriter().print(JsonUtils.toJason(comm));

这是一段简单的java代码,为了简单映射数据库的数据,而把对象的所有内容都放入内存,传入前台页面,而对于comment_text的内容相对是比较多的,当请求不需要时就设置为空,减小传送数据量,加快速度,对于if语句块,通过增加type字段的值来返回需要的结果

对应的前台的ajax依次

返回所有信息
var json3={
        work_id:work_id,
        stu_id:stu_id,
        user_id:'${tea_id}'
    }
    $.post("CommentGet",json3,function(data,textStatus){
        if(textStatus){
            $("#score").val(data.score);
            $("#comment_text").val(data.comment_text);
        }
},"json");
返回部分信息json3变为
var json3={
        work_id:work_id,
        stu_id:stu_id,
        user_id:'${tea_id}',
                type:'score'    
    }
    

返回的结果则分别含有comment_text信息,和不含有,这个是语句覆盖,前台传回的数据是指定好的,为简化项目,其他情况暂时不考虑

条件覆盖每个判定的每个条件应取到各种可能的值。

function alterUserPsw() {
    var user = $('#user').val();
    var psw = $('#psw').val();
        
    if(user==''||psw==''){
        alert("用户名和密码不能为空");
        $('#submit').attr('onsubmit','return false;');
        }else{
        $('#submit').attr('onsubmit','return true;');
        }
}

这是一个前台输入校验验证的一段代码,当用户输入的内容可能有

1,user字段的表单为空,psw的表单字段为空

2,user字段的表单不为空,psw的表单字段为空

3,user字段的表单为空,psw的表单字段不为空

4,user字段的表单不为空,psw的表单字段不为空

无论上述的不符合的条件满足一项都会阻止表单的提交,只有完全符合了才能提交表单

判定语句覆盖

//字段为空的请求
if(StringUtils.isEmptyStrings(new String[]{info,user_id,password})){
    response.getWriter().print("请填写需要的信息");
    return;
}
    //非法修改请求参数
    if(!(InfoDao.isStudent.equals(info.trim())||
    InfoDao.isTeacher.equals(info.trim()))){
    response.getWriter().print("你既不是老师,又不是学生。。。。");
    return;
}
        
if(!user_id.matches("\d+")){
    response.getWriter().print("帐号不合法");
    return;
}    

对于上述三段语句块,分别测试当执行条件为false和true的条件下,对于三个数据有一个为空时返回结果并终止语句向下执行测试类型


info="",user_id="",password=""      info=!"",user_id="",password=""
info="",user_id=!"",password=""     info="",user_id="",password!=""
info!="",user_id!="",password=""      info!="",user_id="",password!=""
info="",user_id!="",password1=""      info!="",user_id!="",password!=""
对于第一个的所有条件覆盖
对于第二个则结果有

InfoDao.isStudent.equals(info.trim()=true InfoDao.isTeacher.equals(info.trim()=true
InfoDao.isStudent.equals(info.trim()=true InfoDao.isTeacher.equals(info.trim()=false
InfoDao.isStudent.equals(info.trim()=false InfoDao.isTeacher.equals(info.trim()=true
InfoDao.isStudent.equals(info.trim()=false InfoDao.isTeacher.equals(info.trim()=false
条件2的语句覆盖这么多
而对于条件三,是正则的匹配,则语句覆盖可选择性的进行可能性匹配
user_id=11   user_id=-11  user_id=0  user_id='a' user_id='abc'  user_id="abc"  user_id=0.123  user_id=9*9
这里列举一部分
以上便是项目中用到的白盒测试的一部分内容

 
 
 
原文地址:https://www.cnblogs.com/zqq1234/p/5406299.html