首页 > 代码库 > 软件测试实验1 — Junit 安装与 triangle problem 的测试

软件测试实验1 — Junit 安装与 triangle problem 的测试

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

选中新建的项目,右键->Propertise->Java Build Path->Add External JARs

技术分享

然后选中之前下好的junit-4.12.jar与hamcrest-core-1.3.rc2.jar 

技术分享

点击OK即引入成功

技术分享

2.  Install Eclemma with Eclipse

将Eclemma压缩包解压到eclipse下的dropins文件夹。

技术分享

打开eclipse,点击help->install new software->add->local->选择你下载的eclemma,点OK安装,然后提示重启,重启后安装完成。

技术分享

重启后出现技术分享图标即表示安装完成。

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

 

Triangle.java:

 

 1 package triangle;
 2 public class Triangle {
 3     public static boolean isTriangle (int a, int b, int c){
 4         if ((a+b)>c&&(a+c)>b&&(b+c)>a){//the three number can constitute a triangle
 5             return true;
 6         }
 7         else {
 8             return false;
 9         }
10     }
11     
12     public static boolean isEquilateral (int a, int b, int c){
13         if (isTriangle(a,b,c)){
14             if ( a == b && b == c ){
15                 return true;
16             }
17             else {
18                 return false;
19             }
20         }
21         else {
22             return false;
23         }
24     }
25     
26     public static boolean isIsosceles (int a, int b, int c){
27         if (isTriangle(a,b,c)){
28             if ( a == b || a == c || b == c ){
29                 return true;
30             }
31             else {
32                 return false;
33             }
34         }
35         else {
36             return false;
37         }
38     }
39     
40     public static boolean isRightTriangle (int a, int b, int c){
41         if (isTriangle(a,b,c)){
42             if ( a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b ){
43                 return true;
44             }
45             else {
46                 return false;
47             }
48         }
49         else {
50             return false;
51         }
52     }
53     
54      public static void Triangle (int a, int b, int c) { 
55              if (isTriangle(a,b,c)){  //the three number can constitute a triangle
56                 if ( isEquilateral(a,b,c) ){
57                     System.out.println ("The triangle is a equilateral!"); 
58                 }
59                 else if ( isIsosceles(a,b,c) ){
60                     if (isRightTriangle(a,b,c)){
61                         System.out.println ("The triangle is a isosceles right triangle!");
62                     }
63                     else{
64                         System.out.println ("The triangle is a isosceles!"); 
65                     }
66                 }
67                 else if (isRightTriangle(a,b,c)){
68                     System.out.println ("The triangle is a Right triangle!");
69                 }
70                 else {
71                     System.out.println ("The triangle is a scalene!");
72                 }
73              }
74              else {
75                  System.out.println ("Can‘t constitute a triangle!");
76              }
77         } // end Triangle
78     }

TriangleTest.java(全为正确时):

 

 1 package triangle;
 2 import static org.junit.Assert.*;
 3 import org.junit.Before;
 4 import org.junit.Test;
 5 public class TriangleTest {
 6     private Triangle tri;
 7     @Before
 8     public void setUp() throws Exception {
 9         tri = new Triangle();
10     }
11     @Test
12     public void testIsTriangle() {
13         assertEquals("判断三角形模块异常",true,tri.isTriangle(3, 4, 5));
14     }
15     @Test
16     public void testIsEquilateral() {
17         assertEquals("判断等边三角形模块异常",true,tri.isEquilateral(3, 3, 3));
18     }
19     @Test
20     public void testIsIsosceles() {
21         assertEquals("判断等腰三角形模块异常",true,tri.isIsosceles(3, 3, 4));
22     }
23     @Test
24     public void testIsRightTriangle() {
25         assertEquals("判断直角三角形模块异常",true,tri.isRightTriangle(3, 4, 5));
26     }
27     @Test
28     public void testTriangle() {
29         tri.Triangle(3, 4, 5);
30     }
31 }

 

TriangleTest.java(有错误时)

 

 1 package triangle;
 2 import static org.junit.Assert.*;
 3 import org.junit.Before;
 4 import org.junit.Test;
 5 
 6 public class TriangleTest {
 7     private Triangle tri;
 8     @Before
 9     public void setUp() throws Exception {
10         tri = new Triangle();
11     }
12 
13     @Test
14     public void testIsTriangle() {
15         assertEquals("判断三角形模块异常",true,tri.isTriangle(1, 1, 5));
16     }
17 
18     @Test
19     public void testIsEquilateral() {
20         assertEquals("判断等边三角形模块异常",true,tri.isEquilateral(2, 3, 3));
21     }
22 
23     @Test
24     public void testIsIsosceles() {
25         assertEquals("判断等腰三角形模块异常",true,tri.isIsosceles(3, 3, 4));
26     }
27 
28     @Test
29     public void testIsRightTriangle() {
30         assertEquals("判断直角三角形模块异常",true,tri.isRightTriangle(3, 3, 5));
31     }
32 
33     @Test
34     public void testTriangle() {
35         tri.Triangle(3, 3, 5);
36     }
37 }

test the program with Junit:

 创建Junit测试用例:

 技术分享

技术分享

测试结果:

无错误时:

技术分享

技术分享

有错误时:

技术分享

技术分享

覆盖率:

技术分享

技术分享

技术分享

技术分享

 

软件测试实验1 — Junit 安装与 triangle problem 的测试