DUnit的原理是什么,如何来测试单元??
unit BeTestUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TBeTestForm = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
function BeTestFunction(I, J: Integer): Integer;
end;
var
BeTestForm: TBeTestForm;
implementation
{$R *.dfm}
{ TBeTestForm }
function TBeTestForm.BeTestFunction(I, J: Integer): Integer;
begin
Result := I * J;
end;
end.
--------------------------------------------------------------------------
unit TestUnit;
interface
uses
TestFrameWork, BeTestUnit, Dialogs;
type
TTestCaseFirst = class(TTestCase)
private
BeTestForm: TBeTestForm;
protected
procedure SetUp; override;
procedure TearDown; override;
published
procedure TestFirst;
procedure TestSecond;
end;
TTestCaseSecond = class(TTestCase)
published
procedure TestThird;
end;
implementation
{ TTestCaseFirst }
procedure TTestCaseFirst.SetUp;
begin
BeTestForm := TBeTestForm.Create(nil);
end;
procedure TTestCaseFirst.TearDown;
begin
BeTestForm.Destroy;
end;
procedure TTestCaseFirst.TestFirst;
begin
Check(BeTestForm.BeTestFunction(1, 3) = 3, 'First Test fail');
end;
procedure TTestCaseFirst.TestSecond;
begin
Check(BeTestForm.BeTestFunction(1, 3) = 3, 'Second Test fail');
end;
{ TTestCaseSecond }
procedure TTestCaseSecond.TestThird;
begin
Check(BeTestForm.BeTestFunction(1, 3) = 3, 'Third Test fail');
end;
initialization
TestFramework.RegisterTest(TTestCaseFirst.Suite);
TestFramework.RegisterTest(TTestCaseSecond.Suite);
end.
----------------------------------------------------------------
根据相关文档做了个测试的,
Check(BeTestForm.BeTestFunction(1, 3) = 3, 'Third Test fail');
通过这里测试
TBeTestForm.BeTestFunction(I, J: Integer)
过程是否正确。
但感觉没有什么意义,可能是我没有理解DUnit的使用意图,到底DUnit应该如何来测试单元???
问题点数:20、回复次数:3Top
1 楼LocustWei(LocustWei)回复于 2005-08-03 18:55:04 得分 0
单元测试,测试的是程序逻辑,并不关心业务。即设置各种边界看看程序能否正确处理。Top
2 楼jbmaster()回复于 2005-08-04 09:44:30 得分 0
那我也可以通过自己的方式来测试:
if BeTestForm.BeTestFunction(1, 3) = 3 then
...
else
...;
那也挺方便的,为什么需要使用DUnit来测试单元呢??Top
3 楼LocustWei(LocustWei)回复于 2005-08-04 10:28:17 得分 0
DUnit 只是一个辅助工具可以让你少写点单元测试代码(大量测试),顺便计算程序运行时间等。如果这些你都要自己做当然也没有问题。Top




