实验二 141 汤阳斌

一、实验目的

掌握基于覆盖理论与基本路径的基本白盒测试方法和实践

二、实验要求

运用逻辑覆盖测试的覆盖准则设计被测程序的测试用例,并运行测试用例检查程序的正确与否,给出程序缺陷小结。

三、实验内容

根据各位同学自己的被测程序,分别作出各类白盒测试技术的用例设计和相应的Junit脚本。

所有的覆盖的技术:语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、组合覆盖、路径覆盖,基本路径测试方法。

包括的内容有:

1)  被测代码

package demo;
import java.util.Scanner;

public class Commission {
    /*
     * hp:耳机 80元 mpc:手机壳 10元 cpsp:手机贴膜 8元
     */

    public static void main(String[] args) {
        String line;
        int hp = 0, mpc = 0, cpsp = 0;
        String[] input = null;
        float money = 0;
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        while (true) {

            System.out.println("请分别输入三种手机配件的销售情况:");
            line = scanner.nextLine();

            // 【去掉字符串前后的空格】
            line = line.trim();
            // 【去掉字符串中多余的空格】
            line = line.replaceAll("\\s{1,}", " ");

            input = line.split(" ");
            if (Judge(input)) {
                // 判断是否不小于0
                if ((hp = Integer.parseInt(input[0])) < 0) {
                    System.out.println("输入数量不满足要求");
                    continue;
                }
                if ((mpc = Integer.parseInt(input[1])) < 0) {
                    System.out.println("输入数量不满足要求");
                    continue;
                }
                if ((cpsp = Integer.parseInt(input[2])) < 0) {
                    System.out.println("输入数量不满足要求");
                    continue;
                }
            } else {
                System.out.println("输入数量不满足要求");
                continue;
            }
            money = commission(hp, mpc, cpsp);
            System.out.println("佣金金额:" + money);
        }

    }

    // 计算佣金
    private static float commission(int hp, int mpc, int cpsp) {
        float commission = 0;
        int total = hp * 80 + mpc * 10 + cpsp * 8;
        if (total < 1000) {
            commission = (float) (total * 0.1);
        } else if (total <= 1800) {
            commission = (float) (1000 * 0.1 + (total - 1000) * 0.15);
        } else {
            commission = (float) (1000 * 0.1 + 800 * 0.15 + (total - 1800) * 0.2);
        }
        return commission;
    }

    // 判断用户输入的是不是三个整数
    private static boolean Judge(String[] input) {
        String number = "0123456789";
        // 判断输入的是不是三个字符串
        if (input.length != 3) {
            return false;
        }
        // 判断三个字符串是不是纯数字且不含小数点
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < input[i].length(); j++) {
                if ("+-".indexOf(input[i].charAt(0)) == 1) {
                    continue;
                }
                if (number.indexOf(input[i].charAt(j)) == -1) {
                    return false;
                }
                // 【判断输入的字符串是否大于整型的最大数值】
                if (Long.parseLong(input[i]) > Integer.MAX_VALUE || input[i].length() > 10) {
                    return false;
                }
            }
        }
        return true;
    }
}

2)依据覆盖技术,测试用例列表:

 

 

<1>语句覆盖

A-E;A-B-E;A-B-C-E;A-B-C-D-E;A-B-C-D-F-H;A-B-C-D-F-G-I-K;A-B-C-D-F-G-J

测试用例:

<2>判定覆盖(条件覆盖、路径覆盖)

3.相应Junit测试脚本、执行结果

package demo;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;

public class CommissionTest {
    private int hp;
    private int mpc;
    private int cpsp;
    private String comm;
    @Parameters
    public static Collection<Object[]> data(){
        return Arrays.asList(new Object[][]{
            {1,1,1,"佣金额为:9.8"},
            {10,20,50,"佣金额为:160.0"},
            {15,20,3,"佣金额为:163.6"},
            {20,45,30,"佣金额为:318.0"},
            {20,20,50,"佣金额为:300.0"},
            {8,10,3,"佣金额为:76.4"},
            {30,15,62,"佣金额为:469.2"},
            {5,2,1,"佣金额为:42.8"},
            {14,16,5,"佣金额为:148.0"},
        });
    }
     
    public CommissionTest(int hp, int mpc, int cpsp, String comm){
        this.hp = hp;
        this.mpc = mpc;
        this.cpsp = cpsp;
        this.comm = comm;
    }
 }

测试小结:

1、写程序的时候应该注重程序的结构,尽量避免条件的嵌套,以降低测试的复杂性。

2、程序员要养成单元测试的习惯,可以发现很多代码上的错误。

3、单元测试中的参数化测试和打包测试比起普通测试更为高效。

4、经过几次实验, 我认识到了编程能力和测试能力相得益彰的重要性,今后要努力提高这两方面的能力。

posted @ 2017-04-14 00:31  141_汤阳斌  Views(211)  Comments(0Edit  收藏  举报