实验二+018+李滨

一、实验目的

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

二、实验要求

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

三、实验内容

被测代码:

package test1;

import java.util.Scanner;

public class MonyCount {

	/**
	 * @param args
	 */
	//用于判断输入是否正确
	static boolean charge(String headphone, String shell, String protector){
		if(Integer.valueOf(headphone).intValue()<0||
				Integer.valueOf(shell).intValue()<0||
				Integer.valueOf(protector).intValue()<0){
			System.out.println("输入数量不满足要求");
			return false;
		}else{
			return true;
		}
	}
	static //计算佣金的公式
	float   commission (String Headphone, String Shell, String Protector){
		//实现字符串到数字的转化		
		 int headphone=0;
		 int shell=0;
		 int protector=0;
		try {
				     headphone = Integer.valueOf(Headphone).intValue();
				     shell= Integer.valueOf(Shell).intValue();
				     protector= Integer.valueOf(Protector).intValue();
			} catch (NumberFormatException e) {
				    e.printStackTrace();
			}
		
		int total=0;
		float money=0;
		total=80*headphone+10*shell+8*protector;
		if(total<1000){
			 money=(float) (total*0.1);
		}
		if(total>=1000&&total<1800){
			 money=(float) (total*0.15);
		}
		if(money>=1800){
			 money=(float) (1800*0.15+(total-1800)*0.2);
		}
		return money;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String headphone;
		String shell;
		String protector;
		float count;//用于输出计算后得到的佣金
		//分别对输入的数值进行校验
		while(true){
			System.out.println("请分别输入三种手机配件的销售情况:");
			headphone=sc.next();
			shell=sc.next();
			protector=sc.next();
			//改函数用于判断输入是否符合规范
			if(!charge(headphone,shell,protector)){
				continue;
			}
			count=commission(headphone,shell,protector);
			System.out.println("应支付的佣金为:"+count);
		}
	}

}

  

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

DD-路径图

 

<1>语句覆盖

编号

输入

预期输出

实际输出

执行路径

是否通过

1

10 101 0

222.0

222.0

ABF

2

10 100 0

220.0

220.0

ACDF

3

0 100 0

100.0

100.0

ACEF

<2>判定/条件覆盖

编号

输入

预期输出

实际输出

执行路径

是否通过

1

10 101 0

222.0

222.0

ABF

2

10 100 0

220.0

220.0

ACDF

3

0 100 0

100.0

100.0

ACEF

4

1 100 0

112.0

112.0

ACDF

5

1 1 1

9.8

9.8

ACEF

6

-1 -1 -1

错误提示

-9.8

ACEF

×

 

 

<3>组合覆盖

编号

输入

预期输出

实际输出

执行路径

是否通过

1

10 101 0

222.0

222.0

ABF

2

10 100 0

220.0

220.0

ACDF

3

0 100 0

100.0

100.0

ACEF

4

1 100 0

112.0

112.0

ACDF

5

1 1 1

9.8

9.8

ACEF

6

-1 -1 -1

错误提示

-9.8

ACEF

×

7

0 0 0

0.0

0.0

ACEF

 三、使用junit脚本进行测试

@RunWith(Parameterized.class)
public class SoftTest_1Test {
    private int ph;
    private int sh;
    private int pr;
    private String comm;
    @Parameters
    public static Collection<Object[]> data(){
        return Arrays.asList(new Object[][]{
            {5,10,5,"佣金额为:54.0"},
            {10,20,50,"佣金额为:160.0"},
            {20,20,50,"佣金额为:300.0"},
            {8,10,3,"佣金额为:76.4"},
            {15,20,3,"佣金额为:163.6"},
            {30,15,62,"佣金额为:469.2"},
            {5,2,1,"佣金额为:42.8"},
            {14,16,5,"佣金额为:148.0"},
            {20,45,30,"佣金额为:318.0"},
            {0,0,0,"佣金额为:0.0"}
        });
    }
    
    public SoftTest_1Test(int ph, int sh, int pr, String comm){
        this.ph = ph;
        this.sh = sh;
        this.pr = pr;
        this.comm = comm;
    }
    @Test
    public void testCommission() {
        assertEquals(comm, SoftTest_1.commission(ph, sh, pr));
    }

}

  执行结果

四、测试总结

 1、做完junit单元测试才发现这个真的非常方便,因为自己同时进行了手动输入的方式来进行测试,遇到这种只要输入参数就可以自动测试并得出结果正确性的工具真的非常棒。

2、想对程序进行小数和负数的测试,因为看完代码之后很明显这样的测试是通不过的。但是程序又没有这样的判断,写测试用例的时候不知道该写是什么覆盖方法,而且预期输出也不知道该怎么写,应为程序根本没有处理所以也不知道正确情况下程序应该是输出什么,只能是根据常识判断。

3.对于字符串的输入应当要先进行判断然后再进行比较。否则会对后面程序的判定产生较大的影响。

原文地址:https://www.cnblogs.com/leezoey/p/6700988.html