1. 程式人生 > >軟件測試第一次實驗: junit, hamcrest and eclemma.

軟件測試第一次實驗: junit, hamcrest and eclemma.

bar 本地 image lib junit soft jpg classpath eclipse

junit, hamcrest and eclemma.

a) junit的安裝

步驟:

1. 從http://www.junit.org/ 下載junit相應的jar包;

2. 在CLASSPATH中加入JAR包所在的路徑,如E:\Java\jar\junit\junit-4.10.jar;

3. 將junit-4.10.jar加入到項目的lib文件夾或者Libaries中;

4. Window -> Preference -> java -> JUinit(或者Window -> Show View -> Java -> JUnit),檢測該項是否存在。若存在,則安裝成功;

5. 建立測試用例:Right Click Package -> New -> Other -> Java -> JUnit -> JUnit Test Case;

6. 右鍵待測試方法, Run As -> JUnit Test。

b) hamcrest的安裝

與junit安裝一樣

c) eclemma的安裝

1) 先下載號eclemma,

2) 打開eclipse的help中install new software中的add 打開,在打開location,添加本地中的eclemma。

技術分享圖片

3) 使用時,右鍵項目,出現“Coverage As”選項,即說明Eclemma安裝成功,選擇一種方式運行即可。

b) 三角形問題的測試結果和覆蓋報告

代碼:

package triangle;

public class Triangle {

private int one;

private int two;

private int three;

public static boolean isLegal(int len1, int len2, int len3){

if(len1 <= 0 || len2 <= 0 || len3 <= 0)

return false;

if(len1 + len2 > len3 && len2 + len3 > len1 && len1 + len3 > len2)

return true;

return false;

}

public Triangle(int side_1, int side_2, int side_3){

if(isLegal(side_1, side_2, side_3)){

one = side_1;

two = side_2;

three = side_3;

}

else

one = two = three = 1;

}

public boolean isEquilatera(){

return (one == two && one == three);

}

public boolean isIsosceles(){

return (one == two || one == three || two == three);

}

public boolean isScalene(){

return !isEquilatera();

}

public static void main(String[] args){

Triangle tri = new Triangle(2,2,3);

System.out.println(tri.isEquilatera());

System.out.println(tri.isIsosceles());

System.out.println(tri.isScalene());

}

}

測試代碼:

package triangle;

import static org.junit.Assert.*;

import org.junit.Before;

import org.junit.Test;

public class TestTriangle {

private Triangle tri;

@Before

public void setUp() throws Exception {

tri=new Triangle(2,2,3);

}

@Test

public void testIsEquilatera() {

assertFalse(tri.isEquilatera());

}

@Test

public void testIsIsosceles() {

assertTrue(tri.isIsosceles());

}

@Test

public void testIsScalene() {

assertTrue(tri.isScalene());

}

}

測試結果:

技術分享圖片

覆蓋測試:

技術分享圖片

技術分享圖片

軟件測試第一次實驗: junit, hamcrest and eclemma.