当前位置 博文首页 > 无限迭代中......:白盒测试——称重3次找到假球(基本路径测试)

    无限迭代中......:白盒测试——称重3次找到假球(基本路径测试)

    作者:[db:作者] 时间:2021-07-06 09:44

    问题描述

    使用白盒测试用例设计方法为下面的程序设计测试用例(基本路径测试)并Junit下测试:

    程序要求

    10个铅球中有一个假球(比其他铅球的重量要轻),用天平三次称出假球。

    程序设计思路

    第一次使用天平分别称5个球,判断轻的一边有假球;
    拿出轻的5个球,取出其中4个第二次称,两边分别放2个球:
    如果两边同重,则剩下的球为假球;
    若两边不同重,拿出轻的两个球称第三次,轻的为假球。

    程序代码

    package com.softtest.baihe;
    
    public class SearchBall {
        private static int x[]=new int[10];
        public SearchBall(){}
        public void setBWeight(int w[]){
            for(int i=0;i<w.length;i++){
                 x[i]=w[i];
            } 
        }
        public String BeginSearch(){
            if(x[0]+x[1]+x[2]+x[3]+x[4]<x[5]+x[6]+x[7]+x[8]+x[9]){
                if(x[1]+x[2]==x[3]+x[4]){
                    return "1号是假球";
                }
                if(x[1]+x[2]<x[3]+x[4]){
                    if (x[1]<x[2]) {
                        return "2号是假球";
                     }else {
                        return "3号是假球";
                     }
                 }else {
                    if (x[3]<x[4]){
                    	return "4号是假球";
                    }
                    else{
                    	return "5号是假球";
                    }
                 }
            }else {
                if(x[6]+x[7]==x[8]+x[9]){
                    return "6号是假球";
                }
                if(x[6]+x[7]<x[8]+x[9]) {
                	if (x[6]<x[7]) {
                		return "7号是假球";
                	}else {
                		return "8号是假球";
                	}
                }else {
                	if (x[8]<x[9]) {
                		return "9号是假球";
                	}else {
                		return "10号是假球";
                	}
                }
            }
         }
    
    }
    

    程序控制流图

    Y代表判定结果为真,N代表判定结果为假。

    简化后的控制流图

    计算环形复杂度

    控制流图G的环形复杂度计算公式:$ V(G)=E?N+2 $,其中E为控制流图中边的数量,N是控制流图中的结点数量。

    函数BeginSearch()的控制流图的环形复杂度为 V ( G ) = 18 ? 19 + 2 = 1 V(G)=18?19+2=1 V(G)=18?19+2=1

    基本路径

    从程序流图中可知,共有10条基本路径,具体如下

    路径编号经过结点
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    测试用例

    路径测试用例预期结果测试结果
    11,2,2,2,2,2,2,2,2,21号是假球1号是假球
    22,1,2,2,2,2,2,2,2,22号是假球2号是假球
    32,2,1,2,2,2,2,2,2,23号是假球3号是假球
    42,2,2,1,2,2,2,2,2,24号是假球4号是假球
    52,2,2,2,1,2,2,2,2,25号是假球5号是假球
    62,2,2,2,2,1,2,2,2,26号是假球6号是假球
    72,2,2,2,2,2,1,2,2,27号是假球7号是假球
    82,2,2,2,2,2,2,1,2,28号是假球8号是假球
    92,2,2,2,2,2,2,2,1,29号是假球9号是假球
    102,2,2,2,2,2,2,2,2,110号是假球10号是假球

    测试代码

    public class SearchBallTests {
        @Test
        public void test1() {
            SearchBall obj = new SearchBall();
            int[] input;
            int ballIndex;
            // 遍历测试各个基本路径
            for (int i = 0; i < 10; ++i) {
                // 生成用例输入
                input = new int[]{2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
                input[i] = 1;
                obj.setBWeight(input);
                // 测试用例输出
                ballIndex = i + 1;
                assertEquals(ballIndex + "号是假球", obj.BeginSearch());
            }
        }
    }
    
    

    测试结果

    在这里插入图片描述

    参考文章

    JUnit白盒测试之基本路径测试:称重3次找到假球
    使用基本路径测试设计测试用例并使用junit测试程序

    cs