软件测试实验学习笔记系列3
作者:网络转载 发布时间:[ 2013/8/6 13:56:54 ] 推荐标签:
判断三角形的java程序:
package demo2;
public class Triangle {
private double a,b,c;
public Triangle(double a,double b,double c){
this.a =a;
this.b =b;
this.c =c;
}
/*
* return value description:
* 1.等边三角形
* 2.等腰三角形
* 3.其他三角形
* -1.不是三角形
*/
public int type(){
if(isTriangle()){
if(a==b&&b==c){
return 1;
}else if(a==b||b==c||c==a){
return 2;
}else
return 3;
}
else
return -1;
}
//auxiliary method which is used to predicate weather three number consist of a triangle or not
private boolean isTriangle(){
if(Math.abs(a-b)<c&&Math.abs(a-c)<b&&Math.abs(b-c)<a
&&(a+b>c)&& (a+c >b)&& (b+c >a) )
return true;
return false;
}
}
单个的测试用例:
package demo2;
import static org.junit.Assert.*;
import org.junit.Test;
public class TriangleTest {
@Test
public void testType(){
Triangle triangle = new Triangle(12,12,4);
assertEquals(2,triangle.type());
}
}
参数化的测试用例:
package demo2;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(value = Parameterized.class)
public class ParameterTestTriangle {
private double expected;
private double valueOne;
private double valueTwo;
private double valueThree;
@Parameters
//Double is a class type,double is a basic type.
public static Collection<Object []> getTestParameters(){
return Arrays.asList(new Object [][]{
{2,12.3,13.5,13.5},
{2,10,10,4},
{2,34,5,34},
{1,6.0,6.0,6.0},
{1,23,23,23},
{-1,1.0,4.0,9.0},
{-1,10,3,15},
{3,3.0,4.0,5.0},
{3,12,24,33},
});
}
public ParameterTestTriangle(double expected, double valueOne,
double valueTwo,double valueThree){
this.expected= expected;
this.valueOne= valueOne;
this.valueTwo= valueTwo;
this.valueThree= valueThree;
}
@Test
public void testType(){
Triangle triangle = new Triangle(valueOne,valueTwo,valueThree);
assertSame("期待类型和测试类型不同!",(int)expected,triangle.type());
}
}

sales@spasvo.com