结对编程2——单元测试(093,094)
201421123093 何琴琴 201421123094 翁彬妹
题目描述:
上一周大家为四则运算程序设计了2-3个新功能,本次在隔了一周之后,我们循序渐进地进阶。本次目标:
- 把计算模块提取出来,单独创建一个类。
- 针对提取出来的计算类的接口函数做单元测试。
-
参考助教的示例:
需求分析:
1.加减乘除运算模块化;
2.整数加减乘除的测试;
3.真分数加减乘除的测试;
4.公约数的测试;
5.测试计算类对于各种参数的支持
测试框架:
项目用JAVA语言在Eclipse上开发,然后使用JUNIT4进行测试,EclEmma进行代码覆盖率的检测。
测试用例:
第一:
对加减乘除运算模块化
public class intcal { private String result ; public String intcal(int b) { char operation[]={'+','-','×','÷'}; int x=(int)(Math.random()*100); int y=(int)(Math.random()*99+1); if(x<y) { int j; j=x; x=y; y=j; } if( operation[b]== operation[0]) { result= String.valueOf(x+y); } else if( operation[b]== operation[1]) { result= String.valueOf(x-y); } else if( operation[b]== operation[2]) { result= String.valueOf(x*y); } else if( operation[b]== operation[3]) { if(x%y!=0) { int t=GCD(x,y); result=x/t+ String.valueOf('/')+y/t; } result= String.valueOf(x/y); } else if( operation[b]!=operation[0]&&operation[b]!=operation[1]&&operation[b]!=operation[2]&&operation[b]!=operation[3]) { result="error"; } return result; } public String zfscal(int b) { char operation[]={'+','-','×','÷'}; int x1=(int)(Math.random()*100); int y1=(int)(Math.random()*99+1); int x2=(int)(Math.random()*100); int y2=(int)(Math.random()*99+1); if(x1*y2<x2*y1) { int j; j=x1; x1=x2; x2=j; j=y1; y1=y2; y2=y1; } if( operation[b]== operation[0]) { if((x1*y2+x2*y1)%y1*y2!=0) { result=(x1*y2+x2*y1)/GCD((x1*y2+x2*y1),y1*y2) +String.valueOf('/')+y1*y2/GCD((x1*y2+x2*y1),y1*y2); } else { result=String.valueOf((x1*y2+x2*y1)/y1*y2); }//x1/GCD(x1,y1)+ String.valueOf('/')+y1/GCD(x1,y1)+x2/GCD(x1,y1)+ String.valueOf('/')+y1/GCD(x1,y1); } else if( operation[b]== operation[1]) { if((x1*y2-x2*y1)%y1*y2!=0) { result=(x1*y2-x2*y1)/GCD((x1*y2-x2*y1),y1*y2) +String.valueOf('/')+y1*y2/GCD((x1*y2-x2*y1),y1*y2); } else { result=String.valueOf((x1*y2-x2*y1)/y1*y2); } } else if( operation[b]== operation[2]) { if(x1*x2%y1*y2!=0) { result= x1*x2/GCD(x1*x2,y1*y2)+String.valueOf('/')+ y1*y2/GCD(x1*x2,y1*y2); } else { result=String.valueOf(x1*x2/y1*y2); } } else if( operation[b]== operation[3]) { if(x2==0) { x2=1; } if(x1*y2%x2*y1!=0) { result= x1*y2/GCD(x1*y2,x2*y1)+String.valueOf('/')+ x2*y1/GCD(x1*y2,x2*y1); } else { result=String.valueOf(x1*y2/x2*y1); } } else if( operation[b]!=operation[0]&&operation[b]!=operation[1]&&operation[b]!=operation[2]&&operation[b]!=operation[3]) { result="error"; } return result; } public static int GCD(int m, int n) {//计算最大公约数 while (m % n != 0) { int t = m % n; m = n; n = t; } return n; } }
第二
对相关参数函数进行注释
比如
/** * @param * 整数的加减乘除方法 * b=0做加法b=1做减法b=2做乘法b=3做除法 */ public String intcalculator(int b,int x,int y) { char operation[]={'+','-','×','÷'}; //int x=(int)(Math.random()*100);//整数1 //int y=(int)(Math.random()*99+1);//整数2 if( operation[b]== operation[0]) { result= String.valueOf(x+y); } else if( operation[b]== operation[1]) { result= String.valueOf(x-y); } else if( operation[b]== operation[2]) { result= String.valueOf(x*y); } else if( operation[b]== operation[3]) { if(x%y!=0) { int t=GCD(x,y); result=x/t+ String.valueOf('/')+y/t; } else result= String.valueOf(x/y); } else if( operation[b]!=operation[0]&&operation[b]!=operation[1]&&operation[b]!=operation[2]&&operation[b]!=operation[3]) { result="error"; } return result; }
第三
嘿嘿!不想在去重新修改界面了!
开始进行单元测试
1.对公约数进行测试测试通过
public void testGCD() { intcal case1=new intcal(); /* * 测试公约数函数是否正确*/ assertEquals(case1.GCD(2, 2),2); assertEquals(case1.GCD(0, 2),2);/*特殊数字*/ //assertEquals(case1.GCD(2, 0),2);/*错误情况故所以应注意公约数两个数的取值*/ }
2.对整数的加减乘除进行测试
对整数加减乘除的测试类和函数的编写啦并进行测试通过
//intcalculator(int b, int x, int y) //b=0做加法b=1做减法b=2做乘法b=3做除法,X整数1,Y整数2 public void testintcal1() { intcal case2=new intcal(); assertEquals(case2.intcalculator(0, 3, 5),"8");//测试加法 assertEquals(case2.intcalculator(0, 3, -5),"-2");//测试加法 assertEquals(case2.intcalculator(1, 5, 3),"2");//测试减法 assertEquals(case2.intcalculator(1, 5, 6),"1");//测试减法 assertEquals(case2.intcalculator(2, 5, 0),"0");//测试乘法 assertEquals(case2.intcalculator(2, 5, 3),"15");//测试乘法 assertEquals(case2.intcalculator(3, 6, 5),"6/5");//测试除法 assertEquals(case2.intcalculator(3, 6, 3),"2");//测试除法 }
3.对真分数的加减乘除进行测试,测试通过
//public String zfscalculator(int b,int x1,int x2,int y1,int y2) b=0做加法b=1做减法b=2做乘法b=3做除法 x1/1y1 运算 x2/y2 public void testintcal2() { intcal case3=new intcal(); assertEquals(case3.zfscalculator(0, 1, 2, 2, 4),"1");//测试加法"1/2+2/4" assertEquals(case3.zfscalculator(0, 1, 4, 2, 4),"3/4");//测试加法 assertEquals(case3.zfscalculator(1, 1, 3,1,3),"0");//测试减法 assertEquals(case3.zfscalculator(1, 2, 6,1,6),"1/6");//测试减法 assertEquals(case3.zfscalculator(2, 2, 4,2,4),"1/4");//测试乘法 assertEquals(case3.zfscalculator(2, 0,0,1, 3),"0");//测试乘法 assertEquals(case3.zfscalculator(3, 6, 5,6,5),"1");//测试除法 assertEquals(case3.zfscalculator(3, 6, 3,9,2),"9/4");//测试除法//除数不能为零 }
截图:
测试要求:
1.通过单元测试代码,测试加法是否能正确工作;
答:测试成功。
2.通过单元测试代码,测试加减乘除功能。
答:测试成功。
3.通过单元测试代码,测试计算类对于各种参数的支持:
a. 输入是有错误的,例如 “1 ++ 2”,
答:我的测试结果输入错误将没法通过(笑哭)!
b. 在数值范围是 -1000 .. 1000 的时候,传进去 “10000 + 32768”,
答:在整数运算的过程中定义的是整形,其中是有一定范围的如果输入超出范围会崩溃(呵呵!)
c. 或者是 “ 248 / 0” 怎么办?
答:在写函数的运算过程中有考虑这个问题
int y=(int)(Math.random()*99+1);//不为零整数2
int y=(int)(Math.random()*99+1);//不为零整数2 if(x<y) { int j; j=x; x=y; y=j; if(y==0) { y=1; } }
d. 怎么告诉函数的调用者 “你错了”? 把返回的字符串定义为 “-1” 来表示?
else if( operation[b]!=operation[0]&&operation[b]!=operation[1]&&operation[b]!=operation[2]&&operation[b]!=operation[3]) { result="error"; } return result;
想要改成传string的参数:public String intcalculator(String Suanshi)
private String result ; private String m="0" ; private String n="0"; public String intcalculator(String Suanshi) { char[] suan =Suanshi.toCharArray(); for (int i = 0; i < suan.length; i++) { System.out.println(suan[i]); } char b = 0; int x,y; int t=0; for (int i = 0; i < suan.length; i++) { if(suan[i]=='+'||suan[i]=='-'||suan[i]=='×'||suan[i]=='÷') { b=suan[i]; t=i+1; break; } else { m=m+suan[i]; } } for (int i = t; i < suan.length; i++) { n=n+suan[i]; } System.out.println(n); x=Integer.parseInt(m); y=Integer.parseInt(n); System.out.println(x+" "+y); char operation[]={'+','-','×','÷'}; //int x=(int)(Math.random()*100);//整数1 //int y=(int)(Math.random()*99+1);//整数2 if( b== operation[0]) { result= String.valueOf(x+y); } else if( b== operation[1]) { result= String.valueOf(x-y); } else if( b== operation[2]) { result= String.valueOf(x*y); } else if( b== operation[3]) { if(x%y!=0) { int j=GCD(x,y); result=x/j+ String.valueOf('/')+y/j; } else result= String.valueOf(x/y); } else if( b!=operation[0]&&b!=operation[1]&&b!=operation[2]&&b!=operation[3]) { result="error"; } return result; }
但是遇到测试“6-5” 成功“6×5”失败
不知道什么原因!!!(放弃><)。
小结与感受:
这次的实验彬妹作为领航员,我作为导航员。开始做模块分离的时候,晕车了,之前做的代码是多个界面的,其中又有很多重复的代码比如四则运算,还有就是参数多,导致我们的进度被拖延了。从这次单元测试,我深切感受到了模块化的重要性。单元测试可以将自己的代码一个一个类的进行检查,可以找到哪些代码是错误了,有助于我更好的修正代码。
coding地址:https://coding.net/u/wengbm/p/093094/git
PSP:
PSP2.1 | Personal Software Process Stages | Time (%) Senior Student | Time (%) |
Planning | 计划 | 3h | 2h |
· Estimate | 估计这个任务需要多少时间 | 60h | 48h |
Development | 开发 | 24h | 20h |
· Analysis | 需求分析(包括学习新技术) | 2h | 2h |
· Coding Standard | 代码规范 | 1h | 1h |
· Design | 具体设计 | 2h | 1h |
· Coding | 具体编码 | 18h | 20h |
· Code Review | 代码复审 | 1h | 1h |
· Test | 测试(自我测试,修改代码,提交修改) | 5h | 3h |