您的位置:软件测试 > 开源软件测试 > 开源单元测试工具 > junit
Junit与Eclemma的安装及应用
作者:梦碎 发布时间:[ 2017/4/6 15:40:55 ] 推荐标签:单元测试 Junit

  一、junit和hamcrest安装及引入
  1.输入网址http://junit.org/junit4/下载junit及hamcrest
  2.打开Eclipse新建一个project
  3.右键点击build path 引进junit

  二、eclemma安装
  点击help-->Eclipse Marketplace-->在find中输入eclemma-->install-->重启Eclipse
  三、三角形问题及junit、hamcrest、eclemma的使用
  1、编写三角形判断程序
public class triangle_problem {
String triangle_juidje(double a,double b, double c){
if(a == b && a == c){
String str = new String();
str = "the trangle is equilateral";
return str;
}
if((a+b)>c &&(a+c)>b &&(b+c)>a){
if((a == b && a != c)|| (a==c && a != b) || (b == c && b != a)){
String str = new String();
str = "the trangle is isosceles";
return str;
}
if(a!=b && b!=c){
String str = new String();
str = "the trangle is scalene";
return str;
}
}
else{
String str = new String();
str = "the three edges can't make up a trangle!";
return str;
}
return null;
}
}
  2、junit类生成
  选中类-->new-->jUnit Test Case

  3、编辑junit内容
import static org.junit.Assert.*;
import org.junit.Test;
public class triangle_problemTest {
private triangle_problem tri;
@Test
public void testequilateral() {
tri = new triangle_problem();
assertEquals("the trangle is equilateral",tri.triangle_juidje(3,3,3));
}
@Test
public void testisosceles() {
tri = new triangle_problem();
assertEquals("the trangle is isosceles",tri.triangle_juidje(2,3,2));
}
@Test
public void testscalene() {
tri = new triangle_problem();
assertEquals("the trangle is scalene",tri.triangle_juidje(2,3,4));
}
@Test
public void testnottrangle() {
tri = new triangle_problem();
assertEquals("the three edges can't make up a trangle!",tri.triangle_juidje(2,3,5));
}
}
  4、运行junit文件

  5、运行eclemma

软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd