实验04使用基本路径测试法求解自动售货机问题_第1页
实验04使用基本路径测试法求解自动售货机问题_第2页
实验04使用基本路径测试法求解自动售货机问题_第3页
实验04使用基本路径测试法求解自动售货机问题_第4页
实验04使用基本路径测试法求解自动售货机问题_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验报告实验序号:04 实验项目名称:使用基本路径测试法求解 “自动售货机”问题学号122姓名专业、班实验地点实1-511指导教师实验时间2013/4/27一、实验目的及要求l 理解基本路径覆盖测试法的概念和方法;l 掌握使用Eclipse+JUnit+EclEmma进行基本路径覆盖测试的方法。二、实验设备(环境)及要求l 开发环境:Eclipse v3.7及以上版本;JUnit v4.10及以上版本;文本编辑软件。 l 硬件要求:CPU PIV 以上,256M 内存,1G 硬盘空间。l 系统要求:Windows98/Me/XP/NT/2000,IE 5 以上。三、实验内容步骤1. 下载并安装

2、Eclipse+JUnit+EclEmma实验环境;2. 通读自动售货机程序,并在Eclipse环境下运行该程序;3. 使用基本路径测试法设计测试用例,完成以下表格;编号输入值Type输入值money状态预期输出实际情况001Beer5C各资源剩余Input Information Type: Beer; Money: 5 Cents; Change: 0Current StateBeer: 5Orange Juice: 65 Cents: 71 Dollar: 6002OrangeJuice5C各资源剩余Input Information Type: OrangeJuice; Money:

3、5 Cents; Change: 0Current StateBeer: 6Orange Juice: 55 Cents: 71 Dollar: 6003Beer1D没有啤酒Failure InformationBeer Shortage步骤:1、解压eclemma软件包,并放到eclipse安装目录的dropins文件夹下:2、重新启动eclipse软件,菜单栏会出现新的图标:3、查看Windows的Customize perspective项中的Command Groups Availabiity 多了Coverage项:4、编写待测试类SaleMachine.java文件和测试Junit

4、 Test Case文件SaleMachineTest.java:5、先点击运行看看,覆盖了33.1%:6、分析:红色代表未执行、黄色代表条件没有全部执行、绿色代表执行过了:7、继续修改测试代码,直到覆盖率达到100%:共编写了12个测试用例,使待测试代码100%被覆盖,运行如图:四、实验结果与数据处理a、最终待测试代码如下:public class SaleMachine private int countOfBeer, countOfOrangeJuice, countOfFiveCents, countOfOneDollar;private String typeOfGoods = &q

5、uot;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)/便于测试的初始化函数countOfFiv

6、eCents = fiveCents;countOfOneDollar = oneDollar;countOfBeer = numOfBeer;countOfOrangeJuice = numOfOrange;public String currentState()String state = "Current Staten" + "Beer: " + countOfBeer + "n" + "Orange Juice: " + countOfOrangeJuice + "n" + "

7、5 Cents: " + countOfFiveCents + "n" + "1 Dollar: " + countOfOneDollar;return state;public String operation(String type, String money)/type是用户选择的产品,money是用户投币种类if(money.equalsIgnoreCase("5C") /如果用户投入5角钱if(type.equals(typeOfGoods0) /如果用户选择啤酒if(countOfBeer>0) /如果还有

8、啤酒countOfBeer-;countOfFiveCents+;resultOfDeal = "Input Information n" + "Type: Beer; Money: 5 Cents; Change: 0nn" + currentState();return resultOfDeal;elseresultOfDeal = "Failure Information n" + "Beer Shortage"return resultOfDeal;else if (type.equals(typeOfGo

9、ods1) /用户选择橙汁if(countOfOrangeJuice > 0)countOfOrangeJuice-;countOfFiveCents+;resultOfDeal = "Input Information n" + "Type: OrangeJuice; Money: 5 Cents; Change: 0nn" + currentState();return resultOfDeal;elseresultOfDeal = "Failure Information n" + "OrangeJuice Sh

10、ortage"return resultOfDeal;elseresultOfDeal = "Failure Information n" + "Type Error"return resultOfDeal;else if(money.equalsIgnoreCase("1D") /如果用户投入一元钱/if(countOfFiveCents>0) /如果用户投入一元钱/if(countOfFiveCents > 0) /如果售货机有零钱if(type.equals(typeOfGoods0)&&c

11、ountOfBeer>0)/如果用户选择啤酒而且还有啤酒countOfBeer-;countOfFiveCents-;countOfOneDollar+;resultOfDeal = "Input Information n" + "Type: Beer; Money: 1 Dollar; Change: 5 Centsnn" + currentState();return resultOfDeal;else if (type.equals(typeOfGoods1)&&countOfOrangeJuice>0)/如果用户选择

12、橙汁而且还有橙汁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 = "F

13、ailure Information n" + "Beer Shortage"return resultOfDeal;else if(type.equals(typeOfGoods1)&&countOfOrangeJuice<=0)resultOfDeal = "Failure Information n" + "OrangeJuice Shortage"return resultOfDeal;elseresultOfDeal = "Failure Information n" +

14、"Type Error"return resultOfDeal;elseresultOfDeal = "Failure Information n" + "Change Shortage"return resultOfDeal;/*elseresultOfDeal = "Failure Information n" + "Money Error"return resultOfDeal;*/resultOfDeal = "Failure Information n" + &qu

15、ot;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 SaleMachine saleMachine = new SaleMachine();Beforepublic void setUp() throws Exception Afterpublic void tearDown() thr

16、ows Exception Testpublic void testOperation1() String expectedResult = "Input Information n" +"Type: OrangeJuice; Money: 5 Cents; Change: 0nn" +"Current Staten" +"Beer: 6n" +"Orange Juice: 5n" +"5 Cents: 7n" +"1 Dollar: 6"assertEq

17、uals(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&

18、quot; +"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 = "Failure Information n" +"Beer Shorta

19、ge"assertEquals(expectedResult, saleMachine.operation("Beer", "5c");Testpublic void testOperation4() SaleMachine saleMachine = new SaleMachine(6,6,6,0);String expectedResult = "Failure Information n" +"OrangeJuice Shortage"assertEquals(expectedResult, sal

20、eMachine.operation("OrangeJuice", "5c");Testpublic void testOperation5() SaleMachine saleMachine = new SaleMachine(6,6,6,6);String expectedResult = "Failure Information n" +"Type Error"assertEquals(expectedResult, saleMachine.operation("apple", "

21、;5c");Testpublic void testOperation6() String expectedResult = "Input Information n" +"Type: Beer; Money: 1 Dollar; Change: 5 Centsnn" +"Current Staten" +"Beer: 5n" +"Orange Juice: 6n" +"5 Cents: 5n" +"1 Dollar: 7"assertEqual

22、s(expectedResult, saleMachine.operation("Beer", "1D");Testpublic void testOperation7() String expectedResult = "Input Information: n" +"Type: OrangeJuice; Money: 1 Dollar; Change: 5 Centsnn" +"Current Staten" +"Beer: 6n" +"Orange Juice

23、: 5n" +"5 Cents: 5n" +"1 Dollar: 7"assertEquals(expectedResult, saleMachine.operation("OrangeJuice", "1D");Testpublic void testOperation8() SaleMachine saleMachine = new SaleMachine(6,6,0,6);String expectedResult = "Failure Information n" +"

24、;Beer Shortage"assertEquals(expectedResult, saleMachine.operation("Beer", "1D");Testpublic void testOperation9() SaleMachine saleMachine = new SaleMachine(6,6,6,6);String expectedResult = "Failure Information n" +"Type Error"assertEquals(expectedResult, saleMachine.operation("apple", "1D");Testpublic void testOperation10() SaleMachine saleMachine = new SaleMachine(6,6,6,0);String expectedResult = "Failure Information n" +&

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论