白盒测试用例练习 17页

  • 216.77 KB
  • 2022-09-01 发布

白盒测试用例练习

  • 17页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档由用户上传,淘文库整理发布,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,请立即联系网站客服。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细阅读内容确认后进行付费下载。
  4. 网站客服QQ:403074932
白盒测试用例练习1.为以下所示的程序段设计一组测试用例,要求分别满足语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、组合覆盖和路径覆盖,并画出相应的程序流程图。voidDoWork(intx,inty,intz){intk=0,j=0;if((x>3)&&(z<10)){k=x*y-1;j=sqrt(k);//语句块1}if((x==4)||(y>5)){j=x*y+10;}//语句块2j=j%3;//语句块3k=0j=0k=x*y-1j=sqrt(k)x>3andz<10}aYcNbx=4ory>5eYj=x*y+10Ndj=j%3\n由这个流程图可以看出,该程序模块有4条不同的路径:P1:(a-c-e)P2:(a-c-d)P3:(a-b-e)P4:(a-b-d)将里面的判定条件和过程记录如下:判定条件M={x>3andz<10}判定条件N={x=4ory>5}1、语句覆盖测试用例输入输出判定M的取值判定N的取值覆盖路径x=4,z=5,y=8k=31,j=0TTP1(a-c-e)2、判定覆盖p1和p4可以作为测试用例,其中p1作为取真的路径,p4作为取反的路径。测试用例输入输出判定M的取值判定N的取值覆盖路径x=4,z=5,y=8k=31,j=0TTP1(a-c-e)x=2,z=11,y=5k=0,j=0FFP4(a-b-d)也可以让测试用例测试路径P2和P3。相应的两组输入数据如下:测试用例输入输出判定M的取值判定N的取值覆盖路径x=5,z=5,y=4k=19,j=sqrt(19)%3TFP2(a-c-d)x=4,z=11,y=6k=0,j=1FTP3(a-b-e)3、条件覆盖对于M:x>3取真时T1,取假时F1;z<10取真时T2,取假时F2;对于N:x=4取真时T3,取假时F3;y>5取真时T4,取假时F4。条件:x>3,z<10,x=4,y>5条件:x<=3,z>=10,x!=4,y<=5根据条件覆盖的基本思路,和这8个条件取值,组合测试用例如表所示:测试用例输入输出取值条件具体取值条件覆盖路径x=4,z=5,y=8k=31,j=0T1,T2,T3,T4x>3,z<10,x=4,y>5P1(a-c-e)x=3,z=11,y=5k=0,j=0F1,F2,F3,F4x<=3,z>=10,x!=4,y<=5P4(a-b-d)4、判定/条件覆盖测试用例输入输出取值条件具体取值条件覆盖路径x=4,z=5,y=8k=31,j=0T1,T2,T3,T4x>3,z<10,x=4,y>5P1(a-c-e)x=3,z=11,y=5k=0,j=0F1,F2,F3,F4x<=3,z>=10,x!=4,y<=5P4(a-b-d)\n5、组合覆盖条件组合1)x>3,z<102)x>3,z>=103)x<=3,z<104)x<=3,z>=105)x=4,y>56)x=4,y<=57)x!=4,y>58)x!=4,y<=5测试用例输入输出覆盖条件取值覆盖条件组合覆盖路径x=4,z=5,y=6k=23,j=1T1,T2,T3,T41,5P1(a-c-e)x=4,z=10,y=5k=0,j=0T1,F2,T3,F42,6P2(a-c-d)x=3,z=5,y=6k=14j=1F1,T2,F3,T43,7P3(a-b-e)z=3,z=10,y=5k=0,j=2F1,F2,F3,F44,8P4(a-b-d)6、路径覆盖测试用例输入输出覆盖条件取值覆盖条件组合覆盖路径x=4,z=5,y=8k=31,j=0T1,T2,T3,T41,5P1(a-c-e)x=5,z=5,y=4k=19,j=sqrt(19)%3T1,T2,F3,F41,8P2(a-c-d)x=4,z=11,y=6k=0,j=1T1,F2,T3,T42,5P3(a-b-e)x=2,z=11,y=5k=0,j=0F1,F2,F3,F44,8P4(a-b-d)题目二:三角形问题在三角形计算中,要求输入三角型的三个边长:A、B和C。当三边不可能构成三角形时提示错误,可构成三角形时计算三角形周长。若是等腰三角形打印“等腰三角形”,若是等边三角形,则提示“等边三角形”。画出程序流程图、控制流程图、计算圈复杂度V(g),找出基本测试路径。一、核心程序代码/**判断三角形的类*/publicclassTriangleTestMethod{/**判断三角形的种类。参数a,b,c分别为三角形的三边,*返回的参数值为0,表示非三角形;*为1,表示普通三角形;*为2,表示等腰三角形;*为3,表示等边三角形。*/publicstaticintcomfirm(inta,intb,intc){if((a+b>c)&&(b+c>a)&&(a+c>b)){//判断为三角形if((a==b)&&(b==c))//判断为等边三角形\nreturn3;if((a==b)||(b==c)||(a==c))//判断为等腰三角形return2;else//判断为普通三角形return1;}else{//为非三角形return0;}}}二、程序流程图三、测试用例\n1.语句覆盖测试用例输入期望输出覆盖对象测试结果Case1a=1,b=2,c=30①,⑤0Case2a=3,b=4,c=51①,②,④,⑥②1Case3a=3,b=3,c=42①,②,④,⑦②2Case4a=3,b=4,c=53①,②,③②32.判定覆盖测试用例输入期望输出覆盖对象测试结果Case11a=1,b=2,c=30②,⑤0Case12a=3,b=4,c=51③,②,④,⑥④1Case13a=3,b=3,c=42③,②,④,⑦④2Case14a=3,b=4,c=53③,②,③④33.条件覆盖测试用例输入期望输出覆盖对象测试结果Case5a=1,b=6,c=70F1,T2,T30Case6a=7,b=1,c=60T1,F2,T30Case7a=1,b=7,c=60T1,T2,F30Case8a=3,b=3,c=42T1,T2,T3,T4,F5,T6,F7,F82Case9a=3,b=4,c=32T1,T2,T3,F4,F5,F6,F7,T82Case10a=4,b=3,c=32T1,T2,T3,F4,T5,F6,T7,F824.条件-判定覆盖测试用例输入期望输出覆盖对象测试结果Case15a=1,b=6,c=70F1,T2,T30Case16a=7,b=1,c=60T1,F2,T30Case17a=1,b=7,c=60T1,T2,F30Case18a=3,b=3,c=42T1,T2,T3,T4,F5,T6,2\nF7,F8Case19a=3,b=4,c=32T1,T2,T3,F4,F5,F6,F7,T82Case20a=4,b=3,c=32T1,T2,T3,F4,T5,F6,T7,F82Case21a=3,b=3,c=33F1,T2,T3(3,②,③)3Case22a=3,b=4,c=51T1,F2,T3(①,②,④,⑥)15.条件组合覆盖测试用例输入期望输出覆盖对象测试结果Case23a=1,b=6,c=70F1,T2,T30Case24a=7,b=1,c=60T1,F2,T30Case25a=1,b=7,c=60T1,T2,F30Case26a=3,b=3,c=42T1,T2,T3,T4,F5,T6,F7,F82Case27a=3,b=4,c=32T1,T2,T3,F4,F5,F6,F7,T82Case28a=4,b=3,c=32T1,T2,T3,F4,T5,F6,T7,F82Case29a=3,b=4,c=51T1,T2,T3,F4,F5,F6,F7,F81Case30a=3,b=4,c=32T1,T2,T3,F4,F5,F6,F7,T82备注其他条件组合,无法到达结束四、程序控制流图\n基本路径覆盖测试用例输入期望输出覆盖对象测试结果Case31a=1,b=6,c=70A->D0Case32a=7,b=6,c=10A->B->D0Case33a=1,b=7,c=60A->B->C->D0Case34a=3,b=3,c=33A->B->C->E->G->H3Case35a=3,b=3,c=42A->B->C->E->G->I2Case36a=4,b=3,c=32A->B->C->E->F->H2Case37a=3,b=4,c=32A->B->C->E->F->J->H2Case38a=3,b=4,c=51A->B->C->E->F->J->K1\n题目三:计算生日是星期几已知公元1年1月1日是星期一。编写一个程序,只要输入年月日,就能回答那天是星期几。应用逻辑覆盖方法和基路径测试方法为上面的问题设计测试用例一.程序代码#includeintcheck(intyear,intmonth,intday){intpass=1;intleap=0;if((year%4==0&&year%100!=0)||year%400==0)leap=1;if(year<1||day<1)return0;switch(month){case1:case3:case5:case7:case8:case10:case12:if(day>31)return0;break;case4:case6:case9:case11:if(day>30)return0;break;case2:if(leap==1&&day>29){return0;}if(leap==0&&day>28){return0;}break;default:return0;break;}returnpass;}voidfun(){intyear,month,day,sumday;sumday=0;printf("inputyear,month,day:\n");scanf("%d%d%d",&year,&month,&day);if(check(year,month,day)==0){printf("输入的日期无效");return;}if(month==1||month==2){month+=12;year--;}sumday=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7;if(month==13||month==14){month-=12;year++;}switch(sumday){case0:printf("%d-%d-%disMonday\n",year,month,day);break;case1:printf("%d-%d-%disTuesday\n",year,month,day);break;case2:printf("%d-%d-%disWednesday\n",year,month,day);break;case3:printf("%d-%d-%disThurday\n",year,month,day);break;case4:printf("%d-%d-%disFriday\n",year,month,day);break;case5:printf("%d-%d-%disSaturday\n",year,month,day);break;case6:printf("%d-%d-%disSunday\n",year,month,day);break;default:printf("dateerror");break;}}main(){for(inti=0;i<14;i++)fun();}二.画出程序的控制流图1.程序流程图如下:\nmonth==13||month==14nomonth==1||month==2yesmonth+=12;year--;计算星期数sumday=公式(year,month,day)noyesMonth-=12;year++;5460123456其他sumday输出语句1输出语句2输出语句3输出语句4输出语句5输出语句6输出语句7输出语句0结束78910111213141515输入年月日year,month,day23endCheck(year,month,day)01\nCheck()函数nononoyesyes得到year,month,dayyear<1||day<1no(year%4==0&&year%100!=0)||year%400==0135781012469112其他monthreturnpasspass=1;leap=0;leap=1;return0day>31day>30return0noyesreturn0yesleap==1&&day>29leap==0&&day>28return016181719202122232425262829282731303233\n2.程序控制流图如下:16191721222324313334end\n15243587612end9101113143.用基本路径的测试路径编号基本路径编号基本路径1⒃⒄⒆(21)(24)(31)(33)11(0)①2⒃⒆(34)12(0)②③④⑤⑥⑦⑧3⒃⒆(21)(34)13(0)②④⑤⑦⑨4⒃⒆(21)(22)(34)14(0)②④⑤⑦⑩5⒃⒆(21)(23)(34)15(0)②④⑤⑦(11)6⒃⒆(21)(24)(34)16(0)②④⑤⑦(12)7⒃⒆(21)(31)(34)17(0)②④⑤⑦(13)8⒃⒆(21)(22)(33)18(0)②④⑤⑦(14)9⒃⒆(21)(23)(33)10⒃⒆(21)(31)(33)\n条件:编号条件编号编号T1month==1T13year%100!=0T25month==2T2month==2T14year%100!=0T26month==3T3month==13T15year%400==0T27month==4T4month==14T16year<1T28month==5T5sumday==0T17day<1T29month==6T6sumday==1T18month==1T30month==7T7sumday==2T19day>31T31month==8T8sumday==3T20day>30T32month==9T9sumday==4T21leap==1T33month==10T10sumday==5T22leap==0T34month==11T11sumday==6T23day>29T35month==12T12year%4==0T24day>284.测试用例用例编号输入的数据预期的输出实际的输出执行的路径覆盖的条件1201221WednesdayWednesday(0)②③④⑤⑥⑦⑧T12,T13,T25,T21,T2,T4,T72201231ThurdayThurday(0)②④⑤⑦⑨T12,T13,T26,T83201246FridayFriday(0)②④⑤⑦⑩T12,T13,T27,T94201255SaturdaySaturday(0)②④⑤⑦(11)T12,T13,T28,T105201263SundaySunday(0)②④⑤⑦(12)T12,T13,T29,T1162012730MondayMonday(0)②④⑤⑦(13)T12,T13,T30,T572000815TuesdayTuesday(0)②④⑤⑦(14)T12,T15,T31,T682012931错误提示错误提示(0)①T12,T13,T20,T329201328WednesdayWednesday⒃⒄⒆(21)(24)(31)(33)T25,T22,T2,T4,T7102013012错误提示错误提示⒃⒆(34)T17112013126SaturdaySaturday⒃⒆(21)(34)T18,T1,T3,T101220131033错误提示错误提示⒃⒆(21)(22)(34)T33,T191320131131错误提示错误提示⒃⒆(21)(23)(34)T34,T20142012230错误提示错误提示⒃⒆(21)(24)(34)T12,T13,T25,T21,T23152013229错误提示错误提示⒃⒆(21)(31)(34)T25,T241620131231SaturdaySaturday⒃⒆(21)(22)(33)T35,T10172013630SundaySunday⒃⒆(21)(23)(33)T29,T11182013215FridayFriday⒃⒆(21)(24)(31)(33)T25,T22,T2,T4,T91901212错误提示错误提示⒃⒆(34)T16\n题目四:选择排序下面是选择排序的程序,其中datalist是数据表,它有两个数据成员:一是元素类型为Element的数组V,另一个是数组大小n。算法中用到两个操作,一是取某数组元素V[i]的关键码操作getKey(),一是交换两数组元素内容的操作Swap()::voidSelectSort(datalist&list){/对表list.V[0]到list.V[n-1]进行排序,n是表当前长度。for(inti=0;i