《实验04.使用基本路径测试法求解“自动售货机”问题》由会员分享,可在线阅读,更多相关《实验04.使用基本路径测试法求解“自动售货机”问题(10页珍藏版)》请在金锄头文库上搜索。
1、实验报告实验序号:04 实验项目名称:使用基本路径测试法求解 “自动售货机”问题学号 122 姓名 专业、班实验地点 实 1-511 指导教师 实验时间 2013/4/27一、实验目的及要求 理解基本路径覆盖测试法的概念和方法; 掌握使用 Eclipse+JUnit+EclEmma 进行基本路径覆盖测试的方法。二、实验设备(环境)及要求 开发环境:Eclipse v3.7 及以上版本;JUnit v4.10 及以上版本;文本编辑软件。 硬件要求:CPU PIV 以上,256M 内存,1G 硬盘空间。 系统要求:Windows98/Me/XP/NT/2000,IE 5 以上。三、实验内容步骤1.
2、 下载并安装 Eclipse+JUnit+EclEmma 实验环境;2. 通读自动售货机程序,并在 Eclipse 环境下运行该程序;3. 使用基本路径测试法设计测试用例,完成以下表格;编号输入值Type输入值money状态 预期输出 实际情况001 Beer 5C 各资源剩余Input Information Type: Beer; Money: 5 Cents; Change: 0Current StateBeer: 5Orange Juice: 65 Cents: 71 Dollar: 6002 OrangeJuice5C 各资源剩余Input Information Type: Ora
3、ngeJuice; Money: 5 Cents; Change: 0Current StateBeer: 6Orange Juice: 55 Cents: 71 Dollar: 6003 Beer 1D 没有啤酒Failure InformationBeer Shortage步骤:1、解压 eclemma 软件包,并放到 eclipse 安装目录的 dropins 文件夹下:2、重新启动 eclipse 软件,菜单栏会出现新的图标:3、查看 Windows 的 Customize perspective 项中的 Command Groups Availabiity 多了 Coverage 项
4、:4、编写待测试类 SaleMachine.java 文件和测试 Junit Test Case 文件SaleMachineTest.java:5、先点击运行看看,覆盖了 33.1%:6、分析:红色代表未执行、黄色代表条件没有全部执行、绿色代表执行过了:7、继续修改测试代码,直到覆盖率达到 100%:共编写了 12 个测试用例,使待测试代码 100%被覆盖,运行如图:四、实验结果与数据处理a、最终待测试代码如下:public class SaleMachine private int countOfBeer, countOfOrangeJuice, countOfFiveCents, coun
5、tOfOneDollar;private String typeOfGoods = Beer, OrangeJuice;private String resultOfDeal;public SaleMachine()initial();public void initial()countOfBeer = 6;countOfOrangeJuice = 6;countOfFiveCents = 6;countOfOneDollar = 6;public SaleMachine(int fiveCents, int oneDollar, int numOfBeer, int numOfOrange)
6、/便于测试的初始化函数countOfFiveCents = fiveCents;countOfOneDollar = oneDollar;countOfBeer = numOfBeer;countOfOrangeJuice = numOfOrange;public String currentState()String state = Current Staten +Beer: + countOfBeer + n +Orange Juice: + countOfOrangeJuice + n +5 Cents: + countOfFiveCents + n +1 Dollar: + count
7、OfOneDollar;return state;public String operation(String type, String money)/type是用户选择的产品,money 是用户投币种类if(money.equalsIgnoreCase(5C) /如果用户投入5角钱if(type.equals(typeOfGoods0) /如果用户选择啤酒if(countOfBeer0) /如果还有啤酒countOfBeer-;countOfFiveCents+;resultOfDeal = Input Information n +Type: Beer; Money: 5 Cents; C
8、hange: 0nn + currentState();return resultOfDeal;elseresultOfDeal = Failure Information n + Beer Shortage;return resultOfDeal;else if (type.equals(typeOfGoods1) /用户选择橙汁if(countOfOrangeJuice 0)countOfOrangeJuice-;countOfFiveCents+;resultOfDeal = Input Information n + Type: OrangeJuice; Money: 5 Cents;
9、 Change: 0nn + currentState();return resultOfDeal;elseresultOfDeal = Failure Information n + OrangeJuice Shortage;return resultOfDeal;elseresultOfDeal = Failure Information n + Type Error;return resultOfDeal;else if(money.equalsIgnoreCase(1D) /如果用户投入一元钱/if(countOfFiveCents0) /如果用户投入一元钱/if(countOfFiv
10、eCents 0) /如果售货机有零钱if(type.equals(typeOfGoods0)&countOfBeer0)/如果用户选择啤酒而且还有啤酒countOfBeer-;countOfFiveCents-;countOfOneDollar+;resultOfDeal = Input Information n +Type: Beer; Money: 1 Dollar; Change: 5 Centsnn + currentState();return resultOfDeal;else if (type.equals(typeOfGoods1)&countOfOrangeJuice0)
11、/如果用户选择橙汁而且还有橙汁countOfOrangeJuice-;countOfFiveCents-;countOfOneDollar+;resultOfDeal = Input Information: n +Type: OrangeJuice; Money: 1 Dollar; Change: 5 Centsnn + currentState();return resultOfDeal;elseif(type.equals(typeOfGoods0)&countOfBeer=0) resultOfDeal = Failure Information n + Beer Shortage;
12、return resultOfDeal;else if(type.equals(typeOfGoods1)&countOfOrangeJuice=0)resultOfDeal = Failure Information n + OrangeJuice Shortage;return resultOfDeal;elseresultOfDeal = Failure Information n + Type Error;return resultOfDeal;elseresultOfDeal = Failure Information n + Change Shortage;return resul
13、tOfDeal;/*elseresultOfDeal = Failure Information n + Money Error;return resultOfDeal;*/resultOfDeal = Failure Information n + Money Error;return resultOfDeal;b、最终测试代码如下:import static org.junit.Assert.*;import org.junit.After;import org.junit.Before;import org.junit.Test;public class SaleMachineTest
14、SaleMachine saleMachine = new SaleMachine();Beforepublic void setUp() throws Exception Afterpublic void tearDown() throws Exception Testpublic void testOperation1() String expectedResult = Input Information n +Type: OrangeJuice; Money: 5 Cents; Change: 0nn +Current Staten +Beer: 6n +Orange Juice: 5n
15、 +5 Cents: 7n +1 Dollar: 6;assertEquals(expectedResult, saleMachine.operation(OrangeJuice, 5C);Testpublic void testOperation2() String expectedResult = Input Information n +Type: Beer; Money: 5 Cents; Change: 0nn +Current Staten +Beer: 5n +Orange Juice: 6n +5 Cents: 7n +1 Dollar: 6;assertEquals(expectedResult, saleMachine.operation(Beer, 5C);Testpublic void testOperation3() SaleMachine saleMachine = new SaleMachine(6,6,0,6);String expectedResult = Fa