软件测试-Lab1

Tasks:

1.Install Junit(4.12), Hamcrest(1.3) with Eclipse

2.Install Eclemma with Eclipse

3.Write a java program for the triangle problem and test the program with Junit.

a)      Description of triangle problem:

Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral, isosceles, or scalene.

 

一、  安装软件

Junit、hamcrest、Eclemma的安装:

Junit1.4和hamcrest-all 1.3是在官网上下载安装的,很早上课的时候就装好了- -

Eclemma安装是办公网上下载好安装包后,我的Eclipse版本好像不支持在软件上导入,所以通过查阅资料,直接把解压文件夹放到了Eclipse安装目录下,才安装成功,其中由于版本问题还需要删除一个文件META-INF

安装成功后:打开eclipse出现最左边图标即成功。

 

二、  三角形测试

设计好所有可能情况的用例进行Junit测试,并且通过Eclemma查看覆盖率

 

测试结果如下:

代码如下:

Angel.java

package Angel;


public class Angel {
public static int result = 0;
public void JudgeAngle(int a,int b,int c)
{
if(a<=0 || b<=0 || c<=0){
System.out.println("有不存在的边");
result= -1;
}
else{
if(((a+b)>c) && ((a+c)>b && (b+c)>a)){
int r1,r2,r3;
r1 = a*a+b*b-c*c;
r2 = a*a+c*c-b*b;
r3 = c*c+b*b-a*a;
if((a==b)&&(a==c)){
System.out.println("是等边三角形");
result = 1;
}
if((a==b && a!=c)||(a==c && a!=b)||(b==c && a!=b))
{
System.out.println("是等腰三角形");
result = 2;
}
if(r1==0 || r2==0||r3==0){
System.out.println("是直角三角形");
result = 3;
}

}
else{
System.out.println("不能构成三角形");
result = 0;
}
}

}
public int getReuslt(){
return result;
}
}

测试:

package Angel;
import static org.junit.Assert.*;
import org.junit.Test;
public class AngleTest {
public static Angel angle =new Angel();

@Test
public void testTriangle0(){

angle.JudgeAngle(1, 1, 2);
assertEquals(0, angle.getReuslt());

}
@Test
public void testTriangle(){

angle.JudgeAngle(5, 5, 5);
assertEquals(1, angle.getReuslt());

}
@Test
public void testTriangle2(){

angle.JudgeAngle(5, 5, 6);
assertEquals(2, angle.getReuslt());

}
@Test
public void testTriangle3(){

angle.JudgeAngle(3, 4, 5);
assertEquals(3, angle.getReuslt());

}
@Test
public void testTriangle4(){

angle.JudgeAngle(-1, 4, 5);
assertEquals(-1, angle.getReuslt());

}
}

posted @ 2018-03-22 12:52  ANJINA  阅读(143)  评论(0)    收藏  举报