Software Testing, Lab 1
一.实验要求:
- 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.
二.实验过程
1.JUnit、Hamcrest的安装:
从网上下载junit-4.12.jar 和 hamcrest-all-1.3.jar文件。
2.
1)在Eclipse中新建项目 (File - new - javaproject)
右键点击项目,选择Build Path - Configure Build Path
然后点击Libraries - Add External JARs,选择jar包打开,确认
2)Eclemma的安装:
在菜单栏选择Help - Eclipse Marketplace,搜索Eclemma,点击Install安装,重启Eclipse
3.代码编写
1)triangle.java
package hw;
public class triangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public int tri(int a,int b,int c){
int m=0;
if(a+b>c&&a+c>b&&b+c>a){
if(a==b&&b==c) m=1; //equilateral
else if(a==b||b==c||a==c) m=2; //isosceles
else m=3; //scalene
}
else m=-1;
return m;
}
}
2)triangleTest.java
package hw;
import static org.junit.Assert.*;
import org.junit.Test;
public class triangleTest {
triangle t = new triangle();
@Test
public void test() {
int f = t.tri(1,1,1);
assertEquals( 1 , f );
f = t.tri(2,2,3);
assertEquals( 2 , f );
f = t.tri(2,3,4);
assertEquals( 3 , f );
f = t.tri(1,1,4);
assertEquals( -1 , f );
}
}
三.实验结果
各种情况的测试用例各一个,测试结果均通过
equilateral triangle返回1, isosceles triangle返回2,scalene triangle返回3,不能构成三角形则返回-1
测试结果如图