首页 > 代码库 > 软件测试上机实验(一)
软件测试上机实验(一)
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
- Help –- Install New Software –Add
- Input the name eclemma and the Location with http://update.eclemma.org.
- 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
软件测试上机实验(一)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。