Software-testing-foundations-lab1
Tasks:
- Install Junit(4.12), Hamcrest(1.3) with Eclipse
- Install Eclemma with Eclipse
- Write a java program for the triangle problem and test the program with Junit.
a) Description of triangle problem:
Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral, isosceles, or scalene.
Ans:
1.新版的Mac版eclipse下载的package里自带了Junit,右键单击项目文件选择bulid path->add library->Junit5即可导入;从hamcrest官网下载了hamcrest-all-1.3,还是右键单击项目文件选择properties,选择java bulid path,add external jars导入.
2.点击help菜单,选择eclipse marketplace,搜索eclemma并下载安装即可.
3.写triangle程序
package cn.tju.scs;
public class Triangle {
public int triangle(int a, int b, int c) {
if(a==b && b==c)return 2;
if(a+b>c && a-b<c) {
if(a==b || b==c || a==c)return 3;
return 1;
}
return 0;
}
}
4.右键test文件夹下相同包名的包,new一个junit test case得到测试类,实现测试类
package cn.tju.scs;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
class TriangleTest {
@Test
void testTriangle() {
assertEquals(1, new Triangle().triangle(4, 3, 5));
}
@Test
void testTri() {
assertEquals(2,new Triangle().triangle(3,3,3));
}
@Test
void testT() {
assertEquals(0,new Triangle().triangle(1,1,3));
}
@Test
void testTT() {
assertEquals(3,new Triangle().triangle(3,3,1));
}
}
5.使用junit测试,右键run as junit test
6.使用eclemma测试覆盖率,右键coverage as junit test