今天的上機課上,我嘗試了使用JUint和EclEmma對項目進行測試。java
首先是這兩個工具的安裝,JUint比較容易,只需將須要的jar包引入到項目中便可,而EclEmma則須要在Eclipse中選擇安裝新軟件來進行安裝。編程
安裝好後,是完成對待測項目的編程,此次的測試代碼是一個檢測三角形形狀的方法。個人代碼以下:ide
package com.triangleProblem; import junit.framework.Test; public class Triangle { public Triangle(){} public boolean testThreeSideAmount( int sideOne,int sideTwo,int sideThree ){ boolean isRight = false; if ( (sideOne + sideTwo > sideThree)){ isRight = true; } return isRight; } public boolean testIsTriangle(int sideOne,int sideTwo,int sideThree){ boolean isTriangle = false; if (sideOne > 0 && sideTwo > 0 && sideThree > 0){ if (testThreeSideAmount(sideOne, sideTwo, sideThree)){ if(testThreeSideAmount(sideOne, sideThree, sideTwo)){ if(testThreeSideAmount(sideTwo, sideThree, sideOne)){ isTriangle = true; } } } } return isTriangle; } public boolean testSideEqual(int sideOne,int sideTwo){ boolean equals = false; if (sideOne == sideTwo){ equals = true; } return equals; } public String test(int sideOne,int sideTwo,int sideThree){ String result; boolean isTriangle = this.testIsTriangle(sideOne,sideTwo,sideThree); if( isTriangle ){ boolean ab = this.testSideEqual(sideOne, sideTwo); boolean ac = this.testSideEqual(sideOne, sideThree); boolean bc = this.testSideEqual(sideTwo, sideThree); if(ab && ac && bc){ result = "Equilateral"; }else if (!(ab || ac || bc)){ result = "Scalene"; }else { result = "Isosceles"; } }else{ result = "Not a Triangle"; } return result; } }
這段代碼中,主要有testThreeSideAmount()、testIsTriangle()、testSideEqual()和test()四個方法,他們的功能分別是測試前兩個參數之和是否大於第三個參數、測試輸入的三個數是否能夠構成三角形、測試兩個參數是否相等、判斷三角形的形狀。工具
爲了測試這些方法,我編寫四個測試用例的代碼,分別測試每一個方法,並在每段代碼中我有對全部的可能狀況,給出了相應的測試用例,這樣一來在以後使用EclEmma進行測試時就能夠達到100%的覆蓋率了。測試
最終的測試結果以下圖:ui
從結果能夠看出28個測試用例沒有錯誤,且對目標代碼Triangle.java的覆蓋率達到了100%,這樣就完成了測試。this