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.
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.
{
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,见谅!