一、三角形问题逻辑覆盖法
1 Scanner input=new Scanner(System.in);
2 double a=input.nextDouble();
3 double b=input.nextDouble();
4 double c=input.nextDouble();
5 boolean isTriangle=true;
6 if(a<1||a>200||b<1||b>200||c<1||c>200){
7 System.out.println("输入数据非法\n");}
8 else if((a+b)<=c||(a+c)<=b||(c+b)<=a){
9 System.out.println("非三角形\n");}
10 else if(a==b&&b==c&==c){
11 System.out.println("等边三角形\n");}
12 else if(a==b||b==c||a==c){
13 if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b){
14 System.out.println("等腰直角三角形\n");
15 }else{System.out.println("等腰三角形\n");}}
16 else if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b){
17 System.out.println("直角三角形\n");}
18 else System.out.println("一般三角形\n");
程序结构图
语句:1、2、3、4、5、7、9、11、14、15、17、18
路径:共七条路径
(1)1-2-3-4-5-6-7
(2)1-2-3-4-5-6-8-9
(3)1-2-3-4-5-6-8-10-11
(4)1-2-3-4-5-6-8-10-12-13-14
(5)1-2-3-4-5-6-8-10-12-13-15
(6)1-2-3-4-5-6-8-10-12-16-17
(7)1-2-3-4-5-6-8-10-12-16-18
判定:
(1)a<1||a>200||b<1||b>200||c<1||c>200 (节点6)
(2)(a+b)<=c||(a+c)<=b||(c+b)<=a (节点8)
(3)a==b&&b==c (节点10)
(4)a==b||b==c||a==c (节点12)
(5)a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b (节点13)
(6)a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b (节点16)
条件:
(1)a<1||a>200||b<1||b>200||c<1||c>200 (1.1-1.6)
(2)(a+b)<=c||(a+c)<=b||(c+b)<=a (2.1-2.3)
(3)a==b&&b==c (3.1,3.2)
(4)a==b||b==c||a==c (4.1,4.2,4.3)
(5)a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b (5.1,5.2,5.3)
(6)a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b (6.1,6.2,6.3)
1、语句覆盖/路径覆盖
2、判定覆盖
3、条件覆盖/判定条件覆盖
二、NextDate问题基路径方法
1. Scanner input=new Scanner(System.in);
2. int year=input.nextInt(),input.nextInt(),day=input.nextInt();
3. int tomorrowday=day,tomorrowmonth=month,tomorrowyear=year;
4. if(year<1900||year>2100||month<1||month>12||day<1||day>31) {
5. System.out.printf("输入日期非法");System.exit(-1);}
6. else if((month==4||month==6||month==9||month==11)&&(day==31)){
7. System.out.printf("输入日期不存在\n");System.exit(-1);}
8. else if((year%400==0||(year%100!=0&&year%4==0))&&month==2&&day>29){
9. System.out.printf("输入日期不存在\n");System.exit(-1);}
10. else if((!(year%400==0||(year%100!=0&&year%4==0))&&month==2&&day>28)){
11. System.out.printf("输入日期不存在\n");System.exit(-1);}
12. else if((month==4||month==6||month==9||month==11)&&day==30)
13. { tomorrowmonth++;
14. tomorrowday=1; }
15. else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31)
16. { tomorrowmonth++;
17. tomorrowday=1;}
18. else if(month==12&&day==31)
19. { tomorrowyear++;
20. tomorrowmonth=1;
21. tomorrowday=1;}
22. else if(month==2&&(year%400==0||(year%100!=0&&year%4==0))&&day==29)
23. { tomorrowmonth++;
24. tomorrowday=1;}
25. else if(month==2&&(!(year%400==0||(year%100!=0&&year%4==0)))&&day==28)
26. { tomorrowmonth++;
27. tomorrowday=1;}
28. else{ tomorrowday++;}
29. System.out.println("nextday is\n"+tomorrowyear+"/"+tomorrowmonth+"/"+tomorrowday);
程序结构图
基路径数量:
分支节点:4、6、8、10、12、15、18、22、25
基路径数量9+1=10
基路径:
(1)最长的路径:Start-1-2-3-4-6-8-10-12-15-18-22-25-26-27-29-End
(2)节点4翻转:Start-1-2-3-4-5-29-End
(3)节点6翻转:Start-1-2-3-4-6-7-29-End
(4)节点8翻转:Start-1-2-3-4-6-8-9-29-End
(5)节点10翻转:Start-1-2-3-4-6-8-10-11-29-End
(6)节点12翻转:Start-1-2-3-4-6-8-10-12-13-14-29-End
(7)节点15翻转:Start-1-2-3-4-6-8-10-12-15-16-17-29-End
(8)节点18翻转:Start-1-2-3-4-6-8-10-12-15-18-19-20-21-29-End
(9)节点22翻转:Start-1-2-3-4-6-8-10-12-15-18-22-23-24-29-End
(10)节点25翻转:Start-1-2-3-4-6-8-10-12-15-18-22-25-28-29-End
三、佣金问题数据流测试方法
1. double lockprice=45,stockprice=30,barrelprice=25;
2. double totallocks=0,totalstocks=0,totalbarrels=0;
3. double commission;
4. Scanner input=new Scanner(System.in);
5. System.out.print("Input the locks,stocks and barrels:\n");
6. int locks=input.nextInt(),stocks=input.nextInt(),barrels=input.nextInt();
7. if(1>locks||locks>70||1>=stocks||locks>=80||1>=barrels||barrels>=90){
8. System.out.println("输入错误");System.exit(-1);}
9. totallocks=totallocks+locks;
10. totalstocks=totalstocks+stocks;
11. totalbarrels=totalbarrels+barrels;
12. double locksales=lockprice*totallocks,stocksales=stockprice*totalstocks, barrelsales=barrelprice*totalbarrels,sales=locksales+stocksales+barrelsales;
13. if(sales>1800){
14. commission=0.10*1000;
15. commission=commission+0.15*800;
16. commission=commission+0.20*(sales-1800);}
17. else if(sales>1000){
18. commission=0.10*1000;
19. commission=commission+0.15*(sales-1000);}
20. else commission=0.10*sales;
21. System.out.print("commission is $:"+commission);
程序流程图
变量的定义节点和使用节点
备注:蓝色为计算使用,红色为谓词使用
定义—使用路径:
locks:
(1)6-7 是定义—清除路径
(2)2-3-4-5-6-7-9 是定义—清除路径
stocks:
(1)6-7 是定义—清除路径
(2)6-7-9-10 是定义—清除路径
barrels:
(1)6-7 是定义—清除路径
(2)6-7-9-10-11 是定义—清除路径
sales:
(1)12-13 是定义—清除路径
(2)12-13-14-15-16 是定义—清除路径
(3)12-13-17 是定义—清除路径
(4)12-13-17-18-19 是定义—清除路径
(5)12-13-17-20 是定义—清除路径
commission:
(1)14-15-16-21 不是定义—清除路径
(2)15-16-21 不是定义—清除路径
(3)16-21 是定义—清除路径
(4)18-19-21 不是定义—清除路径
(5)19-21 是定义—清除路径
(6)20-21 是定义—清除路径
分析:locks、stocks、barrels与sales没有非定义清除路径,commission却有3条非定义—清除路径,所以该变量在使用之前可能被定义两次或多次,可将14-15-16三条语句合并成一条一句,18-19两条语句合并成一条语句,减少缺陷。
第二篇:软件测试,黑盒测试
实验二 黑盒测试
班级:计科11102班 姓名: 学号:201117010221
实验日期:2014.11.13 成绩: 教师签字:
一、实验目的
1. 了解VS工作环境和基本操作
2. 掌握使用TDD的一般流程和掌握黑盒测试的用例设计
二、实验内容和基本要求
1. 熟悉TDD的基本流程;
2. 运用VS编写测试类。
三、实验重点和难点
掌握用黑盒测试用例的设计。
四、实验环境
一个装有VS和NUnit软件的计算机。
五、实验步骤
1. 安装NUnit 2. 新建项目(类库) 3. 编写测试代码: TestYearMonth.cs
4. 编写程序: YearMonth.cs 5. 添加引用:nunit.framework 6. 生成dll
7. 运行测试 ①NUnit GUI; ②NUnit的命令行; ③Visual Studio的插件
Nunit的使用: 添加被测程序集 File?Open Project 运行测试 选中单元测试?Run
注意: VS2010使用.net 4.0,所以nunit.framework.dll也需要是支持4.0的
六、部分测试代码
七、实验结果
图1