JUnit白盒测试之基本路径测试:称重3次找到假球

前言

记录一次软件测试课程的课后作业,作业内容是白盒测试中的基本路径测试,步骤如下

  1. 分析程序的控制流
  2. 计算环形复杂度
  3. 找出基本路径
  4. 设计测试用例
  5. 执行测试用例(要求使用JUnit)

作业要求

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

  • 程序要求

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

  • 程序设计思路

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

  • 递交材料

    测试用例设计电子稿、源程序、JUnit测试截图。

程序代码及控制流图

程序代码

文件SearchBall.java内容如下。

注意不要让代码的行号变动,程序流图中节点的编号是根据这份代码里每条执行语句里的行号对应的。

package module;

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 ]

基本路径

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

路径编号 经过节点
1 12-13-14
2 12-13-16-17-18
3 12-13-16-17-20
4 12-13-16-23-24
5 12-13-16-23-27
6 12-31-32
7 12-31-34-35-36
8 12-31-34-35-38
9 12-31-34-41-42
10 12-31-34-41-44

测试用例设计与执行

测试用例设计

路径编号 用例输入 期待输出
1 {5, 10, 10, 10, 10, 10, 10, 10, 10, 10} "1号是假球"
2 {10, 5, 10, 10, 10, 10, 10, 10, 10, 10} "2号是假球"
3 {10, 10, 5, 10, 10, 10, 10, 10, 10, 10} "3号是假球"
4 {10, 10, 10, 5, 10, 10, 10, 10, 10, 10} "4号是假球"
5 {10, 10, 10, 10, 5, 10, 10, 10, 10, 10} "5号是假球"
6 {10, 10, 10, 10, 10, 5, 10, 10, 10, 10} "6号是假球"
7 {10, 10, 10, 10, 10, 10, 5, 10, 10, 10} "7号是假球"
8 {10, 10, 10, 10, 10, 10, 10, 5, 10, 10} "8号是假球"
9 {10, 10, 10, 10, 10, 10, 10, 10, 5, 10} "9号是假球"
10 {10, 10, 10, 10, 10, 10, 10, 10, 10, 5} "10号是假球"

Junit执行测试用例

基于JUnit生成的测试代码,我补充了测试函数BeginSearch()的代码,文件SearchBallTest.java内容如下。

package test.module; 

import org.junit.Test; 
import org.junit.Before; 
import org.junit.After; 
import module.SearchBall;
import static org.junit.Assert.assertEquals;

/** 
* SearchBall Tester. 
* 
* @author <Authors name> 
* @since <pre>4月 7, 2020</pre> 
* @version 1.0 
*/ 
public class SearchBallTest { 

@Before
public void before() throws Exception { 
} 

@After
public void after() throws Exception { 
} 

/** 
* 
* Method: setBWeight(int w[]) 
* 
*/ 
@Test
public void testSetBWeight() throws Exception { 
//TODO: Test goes here... 
} 

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

执行函数testBeginSearch(),得到如下图所示的结果(从下图也可以看到项目的结构)

JUnit执行结果截图

其它


作者:@臭咸鱼

转载请注明出处:https://www.cnblogs.com/chouxianyu/

欢迎讨论和交流!


原文地址:https://www.cnblogs.com/chouxianyu/p/12652784.html