码迷,mamicode.com
首页 > 其他好文 > 详细

软件测试上机实验(一)

时间:2017-03-10 16:39:20      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:arm   pat   alt   under   sim   emma   with   nal   test   

Practice on Junit and Eclemma

1.Install Junit and Eclemma

  1.1. Install Junit

  • Download necessary JARs packages

 技术分享技术分享 

  • Click right key on the project – Properties – Java Build Path – Libraries – Add External JARs
  • Add Junit and harmcrest-all-*

 技术分享

  1.2. Install Eclemma

 技术分享

  • Select the eclemma and complete click Next
  • Reboot the eclipse to complete the installation

2.Using Junit and eclemma

  • Under src folder, create com.Triangle package
  • Write Triangle.java
 1 package com.Triangle;
 2 
 3 public class Triangle {
 4     
 5     int a, b, c;
 6     
 7     public Triangle(int x, int y, int z){
 8         a = x;
 9         b = y;
10         c = z;
11     }
12     
13     public String isPositive(){
14         String msg = "";
15         if(a > 0 && b > 0 && c > 0){
16             msg = "positive";
17         }else{
18             if(a <= 0)
19                 msg += "a is negative";
20             if(b <= 0)
21                 msg += "b is negative";
22             if(c <= 0)
23                 msg += "c is negative";
24         }
25         //System.out.println(msg);
26         return msg;
27     }
28     
29     public String isTriangle(){
30         String msg = "";
31         if(a+b > c && a+c > b && b+c > a){
32             msg = "triangle";
33         }else{
34             msg = "not a triangle";
35         }
36         //System.out.println(msg);
37         return msg;
38     }
39     
40     public String isIsosceles(){
41         String msg = "";
42         if(a == b || b == c || c == a){
43             msg = "isosceles";
44         }else{
45             msg = "not isosceles";
46         }
47         //System.out.println(msg);
48         return msg;
49     }
50     
51     public String isEquilateral(){
52         String msg = "";
53         if(a == b & b == c){
54             msg = "equilateral";
55         }else{
56             msg = "not equilateral";
57         }
58         //System.out.println(msg);
59         return msg;
60     }
61     
62     public String judge(){
63         String msg = "";
64         msg = this.isPositive();
65         if(msg.equals("positive")){
66             msg = this.isTriangle();
67             if(msg.equals("triangle")){
68                 msg = this.isIsosceles();
69                 if(msg.equals("isosceles")){
70                     msg = this.isEquilateral();
71                     if(!msg.equals("equilateral")){
72                         return "isosceles";
73                     }
74                 }else{
75                     return "triangle";
76                 }
77             }
78         }
79         return msg;
80     }
81 }

 

  • Create new source folder test and create com.Triangle package similarly
  • Write several classes for test
 1 package com.Triangle;
 2 
 3 import static org.junit.Assert.*;
 4  
 5 import java.util.Arrays;
 6 import java.util.Collection;
 7 
 8 import org.junit.After;
 9 import org.junit.Before;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.Parameterized;
13 import org.junit.runners.Parameterized.Parameters;
14  
15 import com.Triangle.Triangle;
16 
17 @RunWith(Parameterized.class)
18 public class JudgeTest {
19     private int a;
20     private int b;
21     private int c;
22     private String expected;
23     private Triangle triangle;
24     
25     public JudgeTest(int a, int b, int c, String expected){
26         this.a = a;
27         this.b = b;
28         this.c = c;
29         this.expected = expected;
30     }
31     
32     @Before
33     public void setUp(){
34         System.out.println("Before Judge Test");
35         this.triangle = new Triangle(this.a, this.b, this.c);
36     }
37     
38     @After
39     public void tearDown(){
40         System.out.println("After Judge Test");
41     }
42     
43     
44     @Parameters
45     public static Collection<Object[]> getData(){
46         return Arrays.asList(new Object[][]{
47             {3,3,3,"equilateral"},
48             {3,4,3, "isosceles"},
49             {2,3,4, "triangle"},
50             {1,2,3, "not a triangle"},
51             {-1,1,2, "a is negative"},
52             {1,-2,1, "b is negative"},
53             {1,1,-2, "c is negative"}
54         });
55     }
56     
57     @Test
58     public void testAdd(){
59         assertEquals(this.expected, triangle.judge());
60     }
61 }

 

  • In order to practice @Runwith(Suite.class), I also write some other test classes

 

 1 package com.Triangle;
 2 
 3 import static org.junit.Assert.*;
 4  
 5 import java.util.Arrays;
 6 import java.util.Collection;
 7 
 8 import org.junit.After;
 9 import org.junit.Before;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.Parameterized;
13 import org.junit.runners.Parameterized.Parameters;
14  
15 import com.Triangle.Triangle;
16 
17 @RunWith(Parameterized.class)
18 public class PositiveTest {
19     private int a;
20     private int b;
21     private int c;
22     private String expected;
23     private Triangle triangle;
24     
25     public PositiveTest(int a, int b, int c, String expected){
26         this.a = a;
27         this.b = b;
28         this.c = c;
29         this.expected = expected;
30     }
31     
32     @Before
33     public void setUp(){
34         System.out.println("Before Positive Test");
35         this.triangle = new Triangle(this.a, this.b, this.c);
36     }
37     
38     @After
39     public void tearDown(){
40         System.out.println("After Positive Test");
41     }
42     
43     @Parameters
44     public static Collection<Object[]> getData(){
45         return Arrays.asList(new Object[][]{
46             {3,3,3,"positive"}
47         });
48     }
49     
50     @Test
51     public void testAdd(){
52         assertEquals(this.expected, triangle.isPositive());
53     }
54 }
 1 package com.Triangle;
 2 
 3 import static org.junit.Assert.*;
 4  
 5 import java.util.Arrays;
 6 import java.util.Collection;
 7  
 8 import org.junit.Before;
 9 import org.junit.After;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.Parameterized;
13 import org.junit.runners.Parameterized.Parameters;
14  
15 import com.Triangle.Triangle;
16 
17 @RunWith(Parameterized.class)
18 public class TriangleTest {
19     private int a;
20     private int b;
21     private int c;
22     private String expected;
23     private Triangle triangle;
24     
25     public TriangleTest(int a, int b, int c, String expected){
26         this.a = a;
27         this.b = b;
28         this.c = c;
29         this.expected = expected;
30     }
31     
32     @Before
33     public void setUp(){
34         System.out.println("Before Triangle Test");
35         this.triangle = new Triangle(this.a, this.b, this.c);
36     }
37     
38     @After
39     public void tearDown(){
40         System.out.println("After Triangle Test");
41     }
42     
43     @Parameters
44     public static Collection<Object[]> getData(){
45         return Arrays.asList(new Object[][]{
46             {3,3,3,"triangle"}
47         });
48     }
49     
50     @Test
51     public void testAdd(){
52         assertEquals(this.expected, triangle.isTriangle());
53     }
54 }
 1 package com.Triangle;
 2 
 3 import static org.junit.Assert.*;
 4  
 5 import java.util.Arrays;
 6 import java.util.Collection;
 7 
 8 import org.junit.After;
 9 import org.junit.Before;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.Parameterized;
13 import org.junit.runners.Parameterized.Parameters;
14  
15 import com.Triangle.Triangle;
16 
17 @RunWith(Parameterized.class)
18 public class IsoscelesTest {
19     private int a;
20     private int b;
21     private int c;
22     private String expected;
23     private Triangle triangle;
24     
25     public IsoscelesTest(int a, int b, int c, String expected){
26         this.a = a;
27         this.b = b;
28         this.c = c;
29         this.expected = expected;
30     }
31     
32     @Before
33     public void setUp(){
34         System.out.println("Before Isosceles Test");
35         this.triangle = new Triangle(this.a, this.b, this.c);
36     }
37     
38     @After
39     public void tearDown(){
40         System.out.println("After Isosceles Test");
41     }
42     
43     @Parameters
44     public static Collection<Object[]> getData(){
45         return Arrays.asList(new Object[][]{
46             {3,3,3,"isosceles"}
47         });
48     }
49     
50     @Test
51     public void testAdd(){
52         assertEquals(this.expected, triangle.isIsosceles());
53     }
54 }
 1 package com.Triangle;
 2 
 3 import static org.junit.Assert.*;
 4  
 5 import java.util.Arrays;
 6 import java.util.Collection;
 7 
 8 import org.junit.After;
 9 import org.junit.Before;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.Parameterized;
13 import org.junit.runners.Parameterized.Parameters;
14  
15 import com.Triangle.Triangle;
16 
17 @RunWith(Parameterized.class)
18 public class EquilateralTest {
19     private int a;
20     private int b;
21     private int c;
22     private String expected;
23     private Triangle triangle;
24     
25     public EquilateralTest(int a, int b, int c, String expected){
26         this.a = a;
27         this.b = b;
28         this.c = c;
29         this.expected = expected;
30     }
31     
32     @Before
33     public void setUp(){
34         System.out.println("Before Equilateral Test");
35         this.triangle = new Triangle(this.a, this.b, this.c);
36     }
37     
38     @After
39     public void tearDown(){
40         System.out.println("After Equilateral Test");
41     }
42     
43     
44     @Parameters
45     public static Collection<Object[]> getData(){
46         return Arrays.asList(new Object[][]{
47             {3,3,3,"equilateral"}
48         });
49     }
50     
51     @Test
52     public void testAdd(){
53         assertEquals(this.expected, triangle.isEquilateral());
54     }
55 }

 SuiteTest.java

 1 package com.Triangle;
 2 
 3 import org.junit.runner.RunWith;
 4 import org.junit.runners.Suite;
 5 import org.junit.runners.Suite.SuiteClasses;
 6 
 7 @RunWith(Suite.class)
 8 @SuiteClasses({ PositiveTest.class, TriangleTest.class , IsoscelesTest.class, EquilateralTest.class, JudgeTest.class})
 9 public class SuiteTest {
10 
11 }

 

  • Result

 Click right key on SuiteTest.java -> Coverage as -> JUnit Test

技术分享

 

Except SuiteTest.java itself hasn‘t been covered, other classes have been covered already.

If run PositiveTest.java individually, there will be more paths that won‘t be covered

技术分享

 Green: All Covered

 Red: All Missed

 Yellow: Some Branches Missed

软件测试上机实验(一)

标签:arm   pat   alt   under   sim   emma   with   nal   test   

原文地址:http://www.cnblogs.com/ph0en1x/p/6531186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!