实验报告
实验内容:
1.Install Junit(4.12), Hamcrest(1.3) with Eclipse
2.Install Eclemma with Eclipse
3.Write a java program for the triangle problem and test the program with Junit.
实验步骤:
1.在eclipse中安装Junit、Hamcrest和eclemma。
下载Junit-4.12.jar、hamcrest-core-1.3.jar、和hamcrest-library-1.3.jar。
打开eclipse,新建一个project,在eclipse的菜单栏中,选择Project->Properties,在弹出的框的左侧选择“Java Bulid Path”,然后点击“Add External JARs”,把下载的几个包添加进来。如图:
安装eclemma:选择eclipse菜单中的“Help”,选择"Eclipse marketplace...",搜索eclemma,点击install开始安装。
安装完成后重启eclipse。看到快捷按钮中的,说明eclemma安装成功。
2.程序代码和测试代码:可查看https://github.com/realpby/SWTesting/blob/master/First%20Lab
项目的目录如图:
triCalculate.java文件中是实现三角形问题的代码。testCalculate.java中是测试程序的代码。
triCalculate.java:
package triangle;public class triCalculate {private static int tri =0;public void triangle(int a,int b, int c){if(a<=0||b<=0||c<=0){tri=0;}else{if(((a+b)>c) && ((a+c)>b && (b+c)>a)){if((a == b) &&(a == c)){tri = 3;//equilateral }if((a==b && a!=c)||(a==c && a!=b)||(b==c && a!=b)){tri = 2;//isosceles }if(a!=b && a!=c && b!=c){tri = 1;//scalene }}else{tri=0;//三边长度无法构成三角形 }} }public int getReuslt(){return tri;}public void clear(){tri = 0;} }
testCalculate.java:
package triangle; import static org.junit.Assert.*; import org.junit.Test;public class testCalculate {private static triCalculate cal = new triCalculate();@Testpublic void testTriangle(){cal.triangle(-5, 5, 5);assertEquals(0, cal.getReuslt());cal.triangle(5, 5, 5);assertEquals(3, cal.getReuslt());cal.triangle(5, 5, 6);assertEquals(2, cal.getReuslt());cal.triangle(3, 4, 5);assertEquals(1, cal.getReuslt());cal.triangle(12, 5, 5);assertEquals(0, cal.getReuslt());cal.triangle(10, 5, 5);assertEquals(0, cal.getReuslt());cal.triangle(11, 5, 5);assertEquals(3, cal.getReuslt());}}
3.三角问题测试的测试结果和覆盖报告
截图如下:
测试程序中(11,5,5)这组数据不为3,所以出现一个failure。在上图的右边可以看到Junit的报告,在下方可以看到Coverage的报告。