格斯文档网

您现在的位置是:格斯文档网 > 述职报告 >

黑盒测试实验报告

  1 《 软件测试技术

 》实验报告

  2 实验序号:

 04

  实验项目名称:黑盒测试

 3 一、实验目的及要求 掌握用等价类、边界值分析法、因果图的方法设计测试用例 二、实验设备(环境)及要求 实验在计算机系的机房进行,安装了 VC6.0,Java SDK,TurboC 等软件,学生能采用 C 语言,C++,或 Java 语言进行编程。

 三、实验内容与步骤 任务一:佣金问题 问题描述如下:

 一酒水销售公司指派销售员销售各种酒水。白酒卖 168 元/瓶,红酒卖 120元/瓶,啤酒卖 5 元/瓶。白酒每月的供应量为 5000 瓶,红酒为 3000 瓶,啤酒为30000 瓶,各个销售员每月至少要售出白酒 50 瓶,红酒 30 瓶,啤酒 300 瓶。月末,每个销售员上报他所在区域的销售业绩。这样酒水销售公司就知道了当月的销售情况,并根据销售额计算佣金提成如下:

 2 万以下(含):4%; 2 万(不含)~4.5 万(含):1% 4.5 万以上(不含):0.5% 这样,佣金程序生成月份销售报告,汇总售出的白酒、红酒和啤酒总数,销售公司的总销售额和各个销售员的佣金。

 这个问题分为三个不同的部分:

 输入数据部分,用来处理输入数据的有效性; 销售额计算部分; 佣金(提成)计算部分. 1、充分考虑输入有效性校验,编写佣金问题的代码; 2、按照等价类划分的思想,设计等价类测试用例; 1)佣金问题中,首先分析输入和输出,注意分析佣金问题中给出和隐含的

 对输入条件的要求:

 2)针对输入进行等价类划分,形成等价类列表,并为每一个等价类编号;

 3)从等价类划分的表格中设计测试用例:为有效等价类设计测试用例,为每一个无效等价类至少设计一个测试用例 3、执行测试,记录发现的问题。

 任务二:下一天问题 1、对于 NextDate 问题,分析它的输入限制;

 2、从输入限制的边界设计测试用例;

 3、执行测试,记录结果。

 任务三:自动售货机问题 有一个处理单价为 5 角钱的饮料的自动售货机软件测试用例的设计。其规格说明如下:若投入 5 角钱或 1 元钱的硬币,押下[橙汁]或[啤酒]的按钮,则相应的饮料酒送出来。若售货机没有零钱找,则一个显示[零钱找完]的红灯亮,这时再投入 1 元硬币并押下按钮后,饮料不送出来而且 1 元硬币也退出来;若有零钱找,则显示[零钱找完]的红灯灭,在送出饮料的同时退还 5 角硬币。

 1、分析这一段说明,列出原因和结果

 2、画出因果图。所有原因结点列在左边,所有结果结点列在右边。建立中间结点,表示处理的中间状态。中间结点:

 1)投入 1 元硬币且押下饮料按钮

  2) 押下〖橙汁〗或〖啤酒〗的按钮

  3) 应当找 5 角零钱并且售货机有零钱找

  4)钱已付清

 3、转换成判定表

 4、在判定表中,把不可能出现的情况删去。

 5、最后根据判定表中剩下的列作为确定测试用例的依据,设计测试用例。

 6、实验报告要求及记录、格式 四、程序调试与结果 1、佣金问题

 1)划分等价类并编号,下表等价类划分的结果

 输入等价类 有效等价类 无效等价类 白酒数量 ○1 在 50~5000 之间 ○2 在 0~49 之间 ○3 在 5000 以上不包括5000 红酒数量 ○4 在 30~3000 之间 ○5 在 0~29 之间 ○6 在 3000 以上不包括3000

 啤酒数量 ○7 在 300~30000 之间 ○8 在 0~299 之间 ○9 在 30000 以上不包括30000 2)设计测试用例,以便覆盖所有的有效等价类在表中列出了 3 个有效等价类,编号分别为①、○4 、○7 ,设计的测试用例如下:

 测试数据 期望结果 覆盖的有效等价类

 (55,40,400)

 输入有效 ①、○4 、○7

  3)为每一个无效等价类设计一个测试用例,设计结果如下:

 测试数据 期望结果 覆盖的无效等价类 (30,40,400)

 输入无效 ○2 (5500,40,400)

 输入无效 ○3 (55,25,400)

 输入无效 ○5

 (55,4000,400)

 输入无效 ○6 (55,75,250)

 输入无效 ○8 (55,75,33000)

 输入无效 ○9

  4) 测试结果:所有测试都成功,且测试的覆盖率为百分百

  2、NextDate 问题

 1)分析它的输入限制

 NextDate 是一个有三个变量(月份、日期和年)的函数。函数返回输入日期后面的那个日期。变量月份、日期和年都是整数值,并满足以下条件:

 c1:1<=月份<=12 c2:1<=日期<=31 c3:1812<=年<=2012

 2)边界值测试设计 编号 输入 期望输出 Year Month Day 001 1993 -1 15 Month 不在 1~12 中 002 1993 0 15 Month 不在 1~12 中 003 1993 1 15 1993 年 1 月 16 日 004 1993 11 15 1993 年 11 月 16 日

 005 1993 12 15 1993 年 12 月 16 日 006 1993 13 15 Month 不在 1~12 中

 007 1993 6 -1 Day 不在 1~31 中 008 1993 6 0 Day 不在 1~31 中 009 1993 6 1 1993 年 6 月 2 日 010 1993 6 15 1993 年 6 月 16 日 011 1993 6 29 1992 年 6 月 30 日 012 1993 6 30 1992 年 7 月 1 日 013 1993 6 31 Day 不在 1~30 中 014 1993 2 -1 Day 不在 1~29 中 015 1993 2 0 Day 不在 1~29 中 016 1993 2 1 1993 年 2 月 2 日 017 1993 2 15 1993 年 2 月 16 日 018 1992 2 28 1993 年 2 月 29 日 029 1993 2 28 1993 年 3 月 1 日 019 1992 2 29 1993 年 3 月 1 日 020 1993 2 29 平年 2 月没有 29 号 021 1993 2 30 Day 不在 1~31 中 022 1993 2 31 Day 不在 1~31 中 023 1993 12 -1 Day 不在 1~31 中 024 1993 12 0 Day 不在 1~31 中 025 1993 12 1 1993 年 12 月 2 日 026 1993 12 30 1993 年 12 月 31 日 027 1993 12 31 1994 年 1 月 1 日 028 1993 12 32 Day 不在 1~31 中 030 1993 1 -1 Day 不在 1~31 中 031 1993 1 0 Day 不在 1~31 中 032 1993 1 1 1993 年 1 月 2 日 033 1993 1 30 1993 年 1 月 31 日

 034 1993 1 31 1993 年 2 月 1 日 035 1993 1 32 Day 不在 1~31 中 3)测试结果:所有测试都成功,且测试的覆盖率为百分百

 3、自动售货机问题 1)分析 自动售货机中一共存在 5 个条件和 5 个结果:

 条件:C1、售货机有零钱找

  C2、投入 1 元硬币

  C3、投入 5 角硬币

  C4、按下橙汁按钮

  C5、按下啤酒按钮 结果:E1、售货机“零钱找完”灯亮

  E2、退还 1 元硬币

  E3、退还 5 角硬币

  E4、送出橙汁饮料

  E5、送出啤酒饮料 2)因果图的分析结果

 有零钱选橙汁选啤酒投5角投1元红灯亮出橙汁出啤酒找5角找1元EE选商品 钱付清VVVV应该找零钱V能够找零钱VV 4 )有效的条件组合构成的判定表

 输入 C1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 C2 1 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 C3 0 0 0 1 1 1 0 0 0 0 0 1 1 1 0 0 C4 0 1 0 0 1 0 1 0 0 1 0 0 1 0 1 0 C5 0 0 1 0 0 1 0 1 0 0 1 0 0 1 0 1

  结果 E1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 E2 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 E3 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 E4 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 E5 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 5)测试用例 编号 输入值 状态 期望值 001 (cola,1D)

 有零钱 Type Error 002 (OrangeJuice,1D)

 有零钱 送出橙汁,退 5 角 003 (Beer,1D)

 有零钱 送出啤酒,退 5 角 004 (cola,5C)

 有零钱 Type Error 005 (OrangeJuice,5C)

 有零钱 送出橙汁

 006 (Beer,5C)

 有零钱 送出啤酒 007 (OrangeJuice,4C)

 有零钱 Money Error 008 (Beer,4C)

 有零钱 Money Error 009 (cola,1D)

 无零钱 红灯亮,退 1 元 010 (OrangeJuice,1D)

 无零钱 红灯亮,退 1 元 011 (Beer,1D)

 无零钱 红灯亮,退 1 元 012 (cola,5C)

 无零钱 退 5 角 013 (OrangeJuice,5C)

 无零钱 送出橙汁 014 (Beer,5C)

 无零钱 送出啤酒 015 (OrangeJuice,4C)

 无零钱 Money Error 016 (Beer,4C)

 无零钱 Money Error 6)测试结果:所有测试都成功,且测试的覆盖率为百分百

 五、分析与讨论 该实验借助了 Eclipse+JUnit+EclEmma 进行了测试。可以将代码实现的功能过一遍。更快捷的找到自己的测试用例的不足。

 六、教师评语

 签名:

 日期:

 成绩

 附件:

 4 1、 佣金问题 5 实现代码:

 6 package test4; 7

 8 import java.util.Scanner; 9

 10 public class Commission { 11

 12

  double totalspirit=0;//白酒总销售量 13

  double totalredwine=0;//红酒总销售量 14

  double totalbeer=0;//啤酒总销售量 15

 16

  double spiritprice=168;//白酒的单价 17

  double redwineprice=120;//红酒的单价 18

  double beerprice=5;//啤酒的单价 19

 20

  double spiritsales; 21

  double redwinesales; 22

  double beersales; 23

  double sales; 24

 25

  double commission;//佣金 26

  String resultSales; 27

 28

  29

  public Commission(double spirit, double redwine, double beer) 30

  { 31

 totalspirit+=spirit; 32

 totalredwine=+redwine; 33

 totalbeer+=beer; 34

  } 35

  public String operation(double spirit, double redwine, double beer) 36

  { 37

 if(spirit>=50&&spirit<=5000) 38

 { 39

  if(redwine>=30&&redwine<=3000) 40

  { 41

 if(beer>=300&&beer<=30000) 42

 {

 43

 spiritsales=spiritprice*totalspirit; 44

 redwinesales=redwineprice*totalredwine; 45

 beersales=beerprice*totalbeer; 46

 sales=spiritsales+redwinesales+beersales; 47

  if(sales<=20000) 48

  commission=sales*0.04; 49

 else if(sales>20000&&sales<=45000) 50

  commission=20000*0.04+(sales-20000)*0.01; 51

 else 52

  commission=45000*0.01+(sales-45000)*0.005; 53

  resultSales="Current State\n" + 54

 "白酒销售量: " + totalspirit + "\n" + 55

 "红酒销售量: " + totalredwine + "\n" + 56

 "啤酒销售量: " + totalbeer + "\n" + 57

 "公司总销售额: " + sales+"\n"+ 58

 "本月获得佣金:"+commission; 59

 } 60

 else if(beer<300) 61

 { 62

  resultSales="You did not complete the company"s sales 63 index,at least a month to sell 300 bottles of beer."; 64

 } 65

 else if(beer>30000) 66

 { 67

  resultSales="Monthly supply of 30000 bottles of beer"; 68

 } 69

  } 70

  else if(redwine<30) 71

  { 72

 resultSales="You did not complete the company"s sales 73 index,at least a month to sell 30 bottles of red wine."; 74

  } 75

  else if(redwine>3000) 76

  { 77

 resultSales="Monthly supply of 3000 bottles of red wine"; 78

  } 79

 } 80

 else if(spirit<50) 81

 { 82

  resultSales="You did not complete the company"s sales index,at 83 least a month to sell 50 bottles of spirit."; 84

 } 85

 else if(spirit>5000) 86

 { 87

  resultSales="Monthly supply of 5000 bottles of spirit"; 88

 } 89

 return resultSales; 90

  } 91

  }

 92 测试代码:

 93 package test4; 94

 95 import test3.SaleMachine; 96 import junit.framework.TestCase; 97

 98 public class TestCommission extends TestCase { 99

 public void testOperation1() 100

 { 101

  Commission saleMan1 = new Commission(55,40,400); 102

  String expectedResult = "Current State\n" + 103

 "白酒销售量: " + 55.0 + "\n" + 104

 "红酒销售量: " + 40.0 + "\n" + 105

 "啤酒销售量: " + 400.0 + "\n" + 106

 "公司总销售额: " + 16040.0+"\n"+ 107

 "本月获得佣金:"+641.6; 108

  assertEquals(expectedResult, saleMan1.operation(55,40,400)); 109

 } 110

 public void testOperation2() 111

 { 112

  Commission saleMan2 = new Commission(30,40,400); 113

  String expectedResult = "You did not complete the company"s sales index,at least a 114 month to sell 50 bottles of spirit."; 115

  assertEquals(expectedResult, saleMan2.operation(30,40,400)); 116

 } 117

 public void testOperation3() 118

 { 119

  Commission saleMan3 = new Commission(5500,40,400); 120

  String expectedResult = "Monthly supply of 5000 bottles of spirit"; 121

  assertEquals(expectedResult, saleMan3.operation(5500,40,400)); 122

 } 123

 public void testOperation4() 124

 { 125

  Commission saleMan4 = new Commission(55,25,400); 126

  String expectedResult ="You did not complete the company"s sales index,at least a 127 month to sell 30 bottles of red wine."; 128

  assertEquals(expectedResult, saleMan4.operation(55,25,400)); 129

 } 130

 public void testOperation5() 131

 { 132

  Commission saleMan5 = new Commission(55,4000,400); 133

  String expectedResult = "Monthly supply of 3000 bottles of red wine"; 134

  assertEquals(expectedResult, saleMan5.operation(55,4000,400)); 135

  } 136

 public void testOperation6() 137

 { 138

  Commission saleMan6 = new Commission(55,75,250); 139

  String expectedResult ="You did not complete the company"s sales index,at least a 140 month to sell 300 bottles of beer."; 141

  assertEquals(expectedResult, saleMan6.operation(55,75,250)); 142

 } 143

 public void testOperation7() 144

 { 145

  Commission saleMan7 = new Commission(55,75,33000); 146

  String expectedResult = "Monthly supply of 30000 bottles of beer"; 147

  assertEquals(expectedResult, saleMan7.operation(55,75,33000)); 148

 } 149

 public void testOperation8() 150

 { 151

  Commission saleMan8 = new Commission(100,150,1000); 152

  String expectedResult = "Current State\n" + 153

 "白酒销售量: " + 100.0 + "\n" + 154

 "红酒销售量: " + 150.0 + "\n" + 155

 "啤酒销售量: " + 1000.0 + "\n" + 156

 "公司总销售额: " + 39800.0+"\n"+ 157

 "本月获得佣金:"+998.0; 158

  assertEquals(expectedResult, saleMan8.operation(100,150,1000)); 159

 } 160

 public void testOperation9() 161

 { 162

  Commission saleMan9 = new Commission(250,1000,5000); 163

  String expectedResult = "Current State\n" + 164

 "白酒销售量: " + 250.0 + "\n" + 165

 "红酒销售量: " + 1000.0 + "\n" + 166

 "啤酒销售量: " + 5000.0 + "\n" + 167

 "公司总销售额: " + 187000.0+"\n"+ 168

 "本月获得佣金:"+1160.0; 169

  assertEquals(expectedResult, saleMan9.operation(250,1000,5000)); 170

 } 171 } 172

 173 2、 NextDate 问题

 174 实现代码:

 175 package test4; 176

 177

 178 public class NextDay { 179

  180

  int year; 181

  int month; 182

  int day; 183

  int nextday; 184

  int nextmonth; 185

  int nextyear; 186

  String resultDate; 187

 188

  public NextDay(int year,int month,int day) 189

  { 190

 nextday = day; 191

 nextmonth=month; 192

 nextyear=year; 193

  } 194

  public String operation(int year,int month,int day) 195

  { 196

 if(1>month || month>12) 197

 { 198

  resultDate="This month not between 1 and 12"; 199

 } 200

 if(1>day || day>31) 201

 { 202

  resultDate="This day not between 1 and 31"; 203

 } 204

 else{ 205

 switch(month) 206

 { 207

 case (1): 208

 case (3): 209

 case (5): 210

 case (7): 211

 case (8): 212

 case (10): 213

 if(day<31) 214

 { 215

  nextday=day+1; 216

 } 217

 else if(day==31){ 218

  nextday=1; 219

  nextmonth=month+1; 220

 } 221

 resultDate="Tomorrow"s date 222 is:"+nextyear+"-"+nextmonth+"-"+nextday; 223

  break; 224

  225

  226

 case (4): 227

 case (6): 228

 case (9): 229

 case (11): 230

 if(day<30) 231

 { 232

  nextday=day+1; 233

 } 234

 else if(day==30) 235

 { 236

  nextday=1; 237

  nextmonth=month+1; 238

 } 239

 else if(day==31) 240

 { 241

  resultDate="Sorry.This month no 31 days!"; 242

  break; 243

 } 244

 resultDate="Tomorrow"s date 245 is:"+nextyear+"-"+nextmonth+"-"+nextday; 246

 break; 247

  248

 case (12): 249

 if(day<31) 250

 { 251

  nextday=day+1;

 252

 } 253

 else{ 254

  nextday=1; 255

  nextmonth=1; 256

  nextyear=year+1; 257

 } 258

 resultDate="Tomorrow"s date 259 is:"+nextyear+"-"+nextmonth+"-"+nextday; 260

 break; 261

 case (2): 262

  if(day<28) 263

 { 264

  nextday=day+1; 265

 } 266

 else if(day==28) 267

  { 268

  if((year%4==0&&year%100!=0)||year%400==0) 269

  { 270

 nextday=29; 271

  } 272

  else{ 273

 nextday=1; 274

 nextmonth=3; 275

  } 276

 } 277

 else if(day==29) 278

 { 279

  if((year%4==0 && year%100!=0) || year%400==0) 280

  { 281

 nextday=1; 282

 nextmonth=3; 283

  } 284

  else 285

  { 286

 resultDate="Sorry.This year"s 2nd month no 29 days!"; 287

 break; 288

  } 289

 } 290

 else if(day==30||day==31) 291

 { 292

  resultDate="Sorry.This month no 30 or 31 days!"; 293

  break; 294

 } 295

 resultDate="Tomorrow"s date 296 is:"+nextyear+"-"+nextmonth+"-"+nextday; 297

 break; 298

 } 299

  } 300

 return resultDate; 301 } 302

 303 } 304 测试代码:

 305

 package test4; 306

 307 import junit.framework.TestCase; 308

 309 public class TestNextDay extends TestCase { 310

 public void testOperation1() 311

  { 312

  NextDay next1 = new NextDay(1993,-1,15); 313

  String expectedResult ="This month not between 1 and 12"; 314

  assertEquals(expectedResult, next1.operation(1993,-1,15)); 315

 } 316

 public void testOperation2() 317

 { 318

  NextDay next2 = new NextDay(1993,0,15); 319

  String expectedResult = "This month not between 1 and 12"; 320

  assertEquals(expectedResult, next2.operation(1993,0,15)); 321

 } 322

 public void testOperation3() 323

 { 324

  NextDay next3 = new NextDay(1993,1,15); 325

  String expectedResult = "Tomorrow"s date is:"+1993+"-"+1+"-"+16; 326

  assertEquals(expectedResult, next3.operation(1993,1,15)); 327

 } 328

 public void testOperation4() 329

 { 330

  NextDay next4 = new NextDay(1993,11,15); 331

  String expectedResult = "Tomorrow"s date is:"+1993+"-"+11+"-"+16; 332

  assertEquals(expectedResult, next4.operation(1993,11,15)); 333

 } 334

 public void testOperation5() 335

 { 336

  NextDay next5 = new NextDay(1993,12,15); 337

  String expectedResult = "Tomorrow"s date is:"+1993+"-"+12+"-"+16; 338

  assertEquals(expectedResult, next5.operation(1993,12,15)); 339

 } 340

 public void testOperation6() 341

 { 342

  NextDay next6 = new NextDay(1993,13,15); 343

  String expectedResult = "This month not between 1 and 12"; 344

  assertEquals(expectedResult, next6.operation(1993,13,15)); 345

 } 346

 public void testOperation7() 347

 { 348

  NextDay next7 = new NextDay(1993,6,-1); 349

  String expectedResult = "This day not between 1 and 31"; 350

  assertEquals(expectedResult, next7.operation(1993,6,-1)); 351

 } 352

 public void testOperation8() 353

 { 354

  NextDay next8 = new NextDay(1993,6,0); 355

 String expectedResult = "This day not between 1 and 31"; 356

  assertEquals(expectedResult, next8.operation(1993,6,0)); 357

 } 358

 public void testOperation9() 359

 { 360

  NextDay next9 = new NextDay(1993,6,1); 361

  String expectedResult = "Tomorrow"s date is:"+1993+"-"+6+"-"+2; 362

  assertEquals(expectedResult, next9.operation(1993,6,1)); 363

 } 364

 public void testOperation10() 365

 { 366

  NextDay next10 = new NextDay(1993,6,15); 367

  String expectedResult = "Tomorrow"s date is:"+1993+"-"+6+"-"+16; 368

  assertEquals(expectedResult, next10.operation(1993,6,15)); 369

 } 370

 public void testOperation11() 371

 { 372

  NextDay next11 = new NextDay(1993,6,29); 373

  String expectedResult ="Tomorrow"s date is:"+1993+"-"+6+"-"+30; 374

  assertEquals(expectedResult, next11.operation(1993,6,29)); 375

 } 376

 public void testOperation12() 377

 { 378

  NextDay next12 = new NextDay(1993,6,30); 379

  String expectedResult = "Tomorrow"s date is:"+1993+"-"+7+"-"+1; 380

  assertEquals(expectedResult, next12.operation(1993,6,30)); 381

 } 382

 public void testOperation13() 383

 { 384

  NextDay next13 = new NextDay(1993,6,31); 385

  String expectedResult = "Sorry.This month no 31 days!"; 386

  assertEquals(expectedResult, next13.operation(1993,6,31)); 387

 } 388

 public void testOperation14() 389

 { 390

  NextDay next14 = new NextDay(1993,2,-1); 391

  String expectedResult = "This day not between 1 and 31"; 392

  assertEquals(expectedResult, next14.operation(1992,3,-1)); 393

 } 394

 public void testOperation15() 395

 { 396

  NextDay next15 = new NextDay(1993,2,0); 397

  String expectedResult = "This day not between 1 and 31"; 398

  assertEquals(expectedResult, next15.operation(1993,2,0)); 399

  } 400

 public void testOperation16() 401

 { 402

  NextDay next16 = new NextDay(1993,2,1); 403

  String expectedResult = "Tomorrow"s date is:"+1993+"-"+2+"-"+2; 404

  assertEquals(expectedResult, next16.operation(1993,2,1)); 405

 } 406

 public void testOperation17() 407

 { 408

  NextDay next17 = new NextDay(1993,2,15); 409

  String expectedResult ="Tomorrow"s date is:"+1993+"-"+2+"-"+16; 410

  assertEquals(expectedResult, next17.operation(1993,2,15)); 411

 } 412

 public void testOperation18() 413

 { 414

  NextDay next18 = new NextDay(1992,2,28); 415

  String expectedResult ="Tomorrow"s date is:"+1992+"-"+2+"-"+29; 416

  assertEquals(expectedResult, next18.operation(1992,2,28)); 417

 } 418

 public void testOperation29() 419

 { 420

  NextDay next29 = new NextDay(1993,2,28); 421

  String expectedResult ="Tomorrow"s date is:"+1993+"-"+3+"-"+1; 422

  assertEquals(expectedResult, next29.operation(1993,2,28)); 423

 } 424

 public void testOperation19() 425

 { 426

  NextDay next19 = new NextDay(1992,2,29); 427

  String expectedResult ="Tomorrow"s date is:"+1992+"-"+3+"-"+1; 428

  assertEquals(expectedResult, next19.operation(1992,2,29)); 429

 } 430

 public void testOperation20() 431

 { 432

  NextDay next20 = new NextDay(1993,2,29); 433

  String expectedResult ="Sorry.This year"s 2nd month no 29 days!"; 434

  assertEquals(expectedResult, next20.operation(1993,2,29)); 435

 } 436

 public void testOperation21() 437

 { 438

  NextDay next21 = new NextDay(1993,2,30); 439

  String expectedResult ="Sorry.This month no 30 or 31 days!"; 440

  assertEquals(expectedResult, next21.operation(1993,2,30)); 441

 } 442

 public void testOperation22() 443

  { 444

  NextDay next22 = new NextDay(1993,2,31); 445

  String expectedResult = "Sorry.This month no 30 or 31 days!"; 446

  assertEquals(expectedResult, next22.operation(1993,2,31)); 447

 } 448

 public void testOperation23() 449

 { 450

  NextDay next23 = new NextDay(1993,12,-1); 451

  String expectedResult = "This day not between 1 and 31"; 452

  assertEquals(expectedResult, next23.operation(1993,12,-1)); 453

 } 454

 public void testOperation24() 455

 { 456

  NextDay next24 = new NextDay(1993,12,0); 457

  String expectedResult = "This day not between 1 and 31"; 458

  assertEquals(expectedResult, next24.operation(1993,12,0)); 459

 } 460

 public void testOperation25() 461

 { 462

  NextDay next25 = new NextDay(1993,12,1); 463

  String expectedResult = "Tomorrow"s date is:"+1993+"-"+12+"-"+2; 464

  assertEquals(expectedResult, next25.operation(1993,12,1)); 465

 } 466

 public void testOperation26() 467

 { 468

  NextDay next26 = new NextDay(1993,12,30); 469

  String expectedResult = "Tomorrow"s date is:"+1993+"-"+12+"-"+31; 470

  assertEquals(expectedResult, next26.operation(1993,12,30)); 471

 } 472

 public void testOperation27() 473

 { 474

  NextDay next27 = new NextDay(1993,12,31); 475

  String expectedResult ="Tomorrow"s date is:"+1994+"-"+1+"-"+1; 476

  assertEquals(expectedResult, next27.operation(1993,12,31)); 477

 } 478

 public void testOperation28() 479

 { 480

  NextDay next28 = new NextDay(1993,12,32); 481

  String expectedResult ="This day not between 1 and 31"; 482

  assertEquals(expectedResult, next28.operation(1993,12,32)); 483

 } 484

 public void testOperation30() 485

 { 486

  NextDay next30 = new NextDay(1993,1,-1); 487

 String expectedResult ="This day not between 1 and 31"; 488

  assertEquals(expectedResult, next30.operation(1993,1,-1)); 489

 } 490

 public void testOperation31() 491

 { 492

  NextDay next31 = new NextDay(1993,1,0); 493

  String expectedResult ="This day not between 1 and 31"; 494

  assertEquals(expectedResult, next31.operation(1993,1,0)); 495

 } 496

 public void testOperation32() 497

 { 498

  NextDay next32 = new NextDay(1993,1,1); 499

  String expectedResult ="Tomorrow"s date is:"+1993+"-"+1+"-"+2; 500

  assertEquals(expectedResult, next32.operation(1993,1,1)); 501

 } 502

 public void testOperation33() 503

 { 504

  NextDay next33 = new NextDay(1993,1,30); 505

  String expectedResult ="Tomorrow"s date is:"+1993+"-"+1+"-"+31; 506

  assertEquals(expectedResult, next33.operation(1993,1,30)); 507

 } 508

 public void testOperation34() 509

 { 510

  NextDay next34 = new NextDay(1993,1,31); 511

  String expectedResult ="Tomorrow"s date is:"+1993+"-"+2+"-"+1; 512

  assertEquals(expectedResult, next34.operation(1993,1,31)); 513

 } 514

 public void testOperation35() 515

 { 516

  NextDay next35 = new NextDay(1993,1,32); 517

  String expectedResult ="This day not between 1 and 31"; 518

  assertEquals(expectedResult, next35.operation(1993,1,32)); 519

 } 520 } 521

 522 3、 自动售货机问题

 523 实现代码:

 524 package test4; 525

 526 public class SaleMachine { 527

 private int countOfBeer, countOfOrangeJuice, countOfFiveCents, 528

 countOfOneDollar; 529

 private String[] typeOfGoods = { "Beer", "OrangeJuice" }; 530

 private String resultOfDeal; 531

  532

 public SaleMachine() { 533

  initial(); 534

 } 535

 536

 public void initial() { 537

  countOfBeer = 6; 538

  countOfOrangeJuice = 6; 539

  countOfFiveCents = 6; 540

  countOfOneDollar = 6; 541

 } 542

 543

 public SaleMachine(int fiveCents, int oneDollar, int numOfBeer, 544

 int numOfOrange) 545

 546

 { 547

  countOfFiveCents = fiveCents; 548

  countOfOneDollar = oneDollar; 549

  countOfBeer = numOfBeer; 550

  countOfOrangeJuice = numOfOrange; 551

 } 552

 553

 public String operation(String type, String money) 554

 555

 { 556

  if (countOfFiveCents <= 0) { 557

 if (money.equalsIgnoreCase("5C")) { 558

  if (type.equals(typeOfGoods[0])) { 559

 resultOfDeal = "give you a bottle Beer."; 560

 return resultOfDeal; 561

  } else if (type.equals(typeOfGoods[1])) { 562

 resultOfDeal = "give you a bottle OrangeJuice."; 563

 return resultOfDeal; 564

  } else { 565

 resultOfDeal = "Back 5 Cents."; 566

 return resultOfDeal; 567

  } 568

 } 569

 if (money.equalsIgnoreCase("1D")) { 570

  if (type.equals(typeOfGoods[0])) { 571

 resultOfDeal = "Turn on the red light \n" + "Back 1 Dollar"; 572

 return resultOfDeal; 573

  } else if (type.equals(typeOfGoods[1])) { 574

 resultOfDeal = "Turn on the red light \n" + "Back 1 Dollar"; 575

  return resultOfDeal; 576

  } else { 577

 resultOfDeal = "Back 1 Dollar"; 578

 return resultOfDeal; 579

  } 580

 } else { 581

  resultOfDeal = "Money Error."; 582

  return resultOfDeal; 583

 } 584

  } else { 585

 if (money.equalsIgnoreCase("1D")) { 586

  if (type.equals(typeOfGoods[0])) { 587

 resultOfDeal = "give you a bottle Beer.\n" 588

 + "Back 5 Cents."; 589

 return resultOfDeal; 590

  } else if (type.equals(typeOfGoods[1])) { 591

 resultOfDeal = "give you a bottle OrangeJuice.\n" 592

 + "Back 5 Cents."; 593

 return resultOfDeal; 594

  } else { 595

 resultOfDeal = "Type Error"; 596

 return resultOfDeal; 597

  } 598

 } else if (money.equalsIgnoreCase("5C")) { 599

  if (type.equals(typeOfGoods[0])) { 600

 resultOfDeal = "give you a bottle Beer.\n"; 601

 return resultOfDeal; 602

  } else if (type.equals(typeOfGoods[1])) { 603

 resultOfDeal = "give you a bottle OrangeJuice.\n"; 604

 return resultOfDeal; 605

  } else { 606

 resultOfDeal = "Type Error" + "Back Money"; 607

 return resultOfDeal; 608

  } 609

 } else { 610

  resultOfDeal = "Money Error"; 611

  return resultOfDeal; 612

 } 613

  } 614

 } 615 } 616

 617 测试代码:

 618 package test4; 619

  620 import test4.SaleMachine; 621 import junit.framework.TestCase; 622

 623 public class TestSaleMachine extends TestCase { 624

 public void testOperation1() { 625

  SaleMachine saleMachine1 = new SaleMachine(); 626

  String expectedResult =

 "Type Error"; 627

  assertEquals(expectedResult, saleMachine1.operation("cola", "1D")); 628

 } 629

 630

 public void testOperation2() { 631

  SaleMachine saleMachine2 = new SaleMachine(); 632

  String expectedResult = "give you a bottle OrangeJuice.\n"+"Back 5 Cents."; 633

  assertEquals(expectedResult, 634

  saleMachine2.operation("OrangeJuice", "1D")); 635

 } 636

 637

 public void testOperation3() { 638

  SaleMachine saleMachine3 = new SaleMachine(); 639

  String expectedResult = "give you a bottle Beer.\n"+"Back 5 Cents."; 640

  assertEquals(expectedResult, saleMachine3.operation("Beer", "1D")); 641

 } 642

 643

 public void testOperation4() { 644

  SaleMachine saleMachine4 = new SaleMachine(); 645

  String expectedResult = "Type Error"+"Back Money"; 646

  assertEquals(expectedResult, saleMachine4.operation("cola", "5C")); 647

 } 648

 649

 public void testOperation5() { 650

  SaleMachine saleMachine5 = new SaleMachine(); 651

  String expectedResult = "give you a bottle OrangeJuice.\n"; 652

  assertEquals(expectedResult, 653

  saleMachine5.operation("OrangeJuice", "5C")); 654

 } 655

 656

 public void testOperation6() { 657

  SaleMachine saleMachine6 = new SaleMachine(); 658

  String expectedResult =

 "give you a bottle Beer.\n"; 659

  assertEquals(expectedResult, saleMachine6.operation("Beer", "5C")); 660

 } 661

 662

 public void testOperation7() { 663

 SaleMachine saleMachine7 = new SaleMachine(); 664

  String expectedResult ="Money Error"; 665

  assertEquals(expectedResult, 666

  saleMachine7.operation("OrangeJuice", "4C")); 667

 } 668

 669

 public void testOperation8() { 670

  SaleMachine saleMachine8 = new SaleMachine(); 671

  String expectedResult ="Money Error"; 672

  assertEquals(expectedResult, saleMachine8.operation("Beer", "4C")); 673

 } 674

 675

 public void testOperation9() { 676

  SaleMachine saleMachine9 = new SaleMachine(0, 6, 6, 6); 677

  String expectedResult = "Back 1 Dollar"; 678

  assertEquals(expectedResult, saleMachine9.operation("cola", "1D")); 679

 } 680

 681

 public void testOperation10() { 682

  SaleMachine saleMachine10 = new SaleMachine(0, 6, 6, 6); 683

  String expectedResult = "Turn on the red light \n" + "Back 1 Dollar"; 684

  assertEquals(expectedResult, 685

  saleMachine10.operation("OrangeJuice", "1D")); 686

 } 687

  688

 public void testOperation11() { 689

  SaleMachine saleMachine11 = new SaleMachine(0, 6, 6, 6); 690

  String expectedResult = "Turn on the red light \n" + "Back 1 Dollar"; 691

  assertEquals(expectedResult, 692

  saleMachine11.operation("Beer", "1D")); 693

 } 694

  695

 public void testOperation12() { 696

  SaleMachine saleMachine12 = new SaleMachine(0,6,6,6); 697

  String expectedResult ="Back 5 Cents."; 698

  assertEquals(expectedResult, saleMachine12.operation("cola", "5C")); 699

 } 700

 public void testOperation13() { 701

  SaleMachine saleMachine13 = new SaleMachine(0,6,6,6); 702

  String expectedResult = "give you a bottle OrangeJuice."; 703

  assertEquals(expectedResult, saleMachine13.operation("OrangeJuice", "5C")); 704

 } 705

 public void testOperation14() { 706

  SaleMachine saleMachine14 = new SaleMachine(0,6,6,6); 707

 String expectedResult = "give you a bottle Beer."; 708

  assertEquals(expectedResult, saleMachine14.operation("Beer", "5C")); 709

 } 710

 public void testOperation15() { 711

  SaleMachine saleMachine13 = new SaleMachine(0,6,6,6); 712

  String expectedResult = "Money Error."; 713

  assertEquals(expectedResult, saleMachine13.operation("OrangeJuice", "4C")); 714

 } 715

 public void testOperation16() { 716

  SaleMachine saleMachine16 = new SaleMachine(0,6,6,6); 717

  String expectedResult = "Money Error."; 718

  assertEquals(expectedResult, saleMachine16.operation("Beer", "4C")); 719

 } 720 } 721

 722

 723

推荐访问:实验 测试 报告

版权所有:格斯文档网 2010-2024 未经授权禁止复制或建立镜像[格斯文档网]所有资源完全免费共享

Powered by 格斯文档网 © All Rights Reserved.。浙ICP备19042928号