Software Testing(软件测试实验一)Lab1 Junit and Eclemma
Install Junit(4.12), Hamcrest(1.3) with Eclipse
Download the Junit and Hamcrest jars from the official site and add them to the project.
First right click on the project item and select the "Build path"->"Configure Build path"->"Libraries"->"Add external JARs", select the jars that you have downloaded.
Click "Apply" and "OK" and the Junit and Hamcrest have been added to the project.
Install Eclemma with Eclipse
Download the eclemma zip and unzip it.
Select "Help"->"Install New Software"->"Add"->"local" and select the directory where your eclemma locates.
Finally, restart the eclpise. If there is a icon refers to coverage run, it indiciates that you have install thte eclemma successfully.
Write a java program for the triangle problem and test program with Junit.
The class Triangle:
package cn.tju.scs.wyh; public class Triangle { public static int isTri(int a, int b, int c){ int sum; double pow; int max = Math.max(a, b); max = Math.max(max, c); if(max == a){ sum = b + c; pow = Math.pow(b, 2) + Math.pow(c, 2); } else if(max == b){ sum = a + c; pow = Math.pow(a, 2) + Math.pow(c, 2); } else{ sum = b + a; pow = Math.pow(b, 2) + Math.pow(a, 2); } if(Math.pow(max, 2) == pow){ //直角三角形 return 3; } else{ if(max < sum){ if(a==b && b==c){ //等边三角形 return 1; } if(a==b || b==c || a==c){ //等腰三角形 return 2; } else //斜边三角形 return 0; } } //不是三角形 return -1; } }
The Triangle test class:
package cn.tju.scs.wyh; import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class Triangle_test{ private int input1; private int input2; private int input3; private int expected; private Triangle triangle = null; public Triangle_test(int input1, int input2, int input3, int expected){ this.input1 = input1; this.input2 = input2; this.input3 = input3; this.expected = expected; } @Before public void setUp(){ triangle = new Triangle(); } @Parameters public static Collection<Object[]> getData(){ return Arrays.asList(new Object[][]{ {5, 6, 7, 0}, {6, 7, 5, 0}, {7, 6, 5, 0}, {1, 1, 2, -1}, {2, 2, 3, 2}, {1, 1, 1, 1}, {3, 4, 5, 3} }); } @Test public void testIsTri(){ assertEquals(this.expected, triangle.isTri(this.input1, this.input2, this.input3)); } }
Here is the URL of my code in the gtihub: https://github.com/wazxser/Software-Test/tree/master/Triangle
The test realize a Prime path coverage
Here is the result:
The coverage example of the Triangle class:
The coverage example of the triangle_test class: