软件测试作业三—— 上机实验判断三角形的类型
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.all包到java bulid path里;
2.在help -- install from site 输入http://update.eclemma.org即为安装eclemma插件;
3.创建目录如下:
4. triangle.java
1 package cn.lxd.tju; 2 3 public class triangle { 4 private int a; 5 private int b; 6 private int c; 7 public triangle(){ 8 9 } 10 11 public int judge(int i , int j , int p){ 12 13 //如果是等边三角形,则返回1 14 if(i == j && j == p){ 15 return 1 ; 16 } 17 //如果是等腰三角形,则返回2 18 else if(i == j || j == p || i == p){ 19 return 2; 20 } 21 //如果是斜角三角形,则返回3 22 else{ 23 return 3; 24 } 25 } 26 }
5. test.java
1 package cn.lxd.tju; 2 3 import static org.junit.Assert.*; 4 import java.util.Arrays; 5 import java.util.Collection; 6 import org.junit.Before; 7 import org.junit.Test; 8 import org.junit.runner.RunWith; 9 import org.junit.runners.Parameterized; 10 import org.junit.runners.Parameterized.Parameters; 11 12 @RunWith(Parameterized.class) 13 public class test { 14 private triangle mytri; 15 16 private int input1; 17 private int input2; 18 private int input3; 19 private int expected; 20 21 public test(int a, int b, int c, int d){ 22 this.input1 = a; 23 this.input2 = b; 24 this.input3 = c; 25 this.expected = d; 26 } 27 28 @Before 29 public void setUp(){ 30 mytri = new triangle(); 31 } 32 33 @Parameters 34 public static Collection<Object[]> getData(){ 35 return Arrays.asList(new Object[][]{ 36 {3,4,5,3}, 37 {3,3,2,2}, 38 {9,9,9,1} 39 }); 40 } 41 42 @Test 43 public void JudgeTri(){ 44 assertEquals(this.expected, mytri.judge(input1, input2, input3)); 45 } 46 }
6.测试结果如下: