Delphi2009&2007中,测试工程的初级使用(DUnit)

Delphi2007之后,编辑器内含了Dunit,要使用它,应该按下面的步骤:

1.首先准备“被测试工程”

2.再建立“测试工程”和“项目组工程”。

3.给“测试工程”添加“测试单元文件”。(选择好“被测单元”后,“测试单元”是自动建立基本测试类的,只需要添加测试代码)

测试代码中,测试方法是一些以“Check”开头的过程。

 

比如:

“被测单元”如下:

 

unit uXXXMath;   
   
interface   
uses SysUtils;   
   
type   
   
TXXXMath =  class   
public   
   function getSum( const a,b:Integer):Integer;//求和函数   
end;   
   
implementation   
   
{  TXXXMath  }   
   
function TXXXMath.getSum( const a, b: Integer): Integer;   
begin   
  Result := a+b;   
end;   
   
end.  

测试单元时这个样子的:

 

unit TestuXXXMath;   
{   
  
  Delphi DUnit Test Case  
  ----------------------  
  This unit contains a skeleton test case class generated by the Test Case Wizard.  
  Modify the generated code to correctly setup and call the methods from the unit   
  being tested.  
  
}   
   
interface   
   
uses   
  TestFramework, SysUtils, uXXXMath;   
   
type   
  // Test methods  for  class TXXXMath   
     
  TestTXXXMath =  class(TTestCase)   
  strict  private   
    FXXXMath: TXXXMath;   
   public   
     procedure SetUp;  override;   
     procedure TearDown;  override;   
   published   
     procedure TestgetSum;   
   end;   
   
implementation   
   
procedure TestTXXXMath.SetUp;   
begin   
  FXXXMath := TXXXMath.Create;   
end;   
   
procedure TestTXXXMath.TearDown;   
begin   
  FXXXMath.Free;   
  FXXXMath :=  nil;   
end;   
   
procedure TestTXXXMath.TestgetSum;   
var   
  ReturnValue: Integer;   
  b: Integer;   
  a: Integer;   
begin   
  // TODO: Setup method call parameters   
  a :=  10;    //我们加上的   
  b :=  20;   
  ReturnValue := FXXXMath.getSum(a, b);   
  CheckEquals(a+b,ReturnValue, ' 结果不对呀!出错了? ');//我们加上的   
   
  // TODO: Validate method results   
end;   
   
initialization   
  // Register any test cases  with the test runner   
  RegisterTest(TestTXXXMath.Suite);   
end.   

时间关系,以上举例来自http://blog.csdn.net/xieyunc/article/details/4140575,见谅!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值