软件测试实验学习笔记系列3--单元测试

发表于:2013-8-06 11:03

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:xiajian2010    来源:51Testing软件测试网采编

分享:

  判断三角形的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());
 }
}

53/5<12345>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号