Dunit的感悟
2006-02-04 13:50:41 来源:WEB开发网Dunit的感悟
Dunit的TextTestRunner方式测试
在Dunit的TextTestRunner测试方式中需在工程文件中引用TextTestRunner而非GUITestRunner。
在Dunit的TextTestRunner测试方式中,Dunit提供了TRunnerExitBehavior数据类型,在dunit中TRunnerExitBehavior的定义如下:
TRunnerExitBehavior = ( rxbContinue, rxbPause, rxbHaltOnFailures);
从该数据类型的定义可得知,该数据类型定义了TextTestRunner的退出行为,即何种方式结束当前的测试。只需在TextTestRunner执行RunRegisteredTests(ExitBehavior)时把需要的退出行为作为参数传入,即可控制TextTestRunne的退出行为。具体如下:
if FindCmdLineSwitch('p', ['-', '/'], true) then
ExitBehavior := rxbPause
else if FindCmdLineSwitch('h', ['-', '/'], true) then
ExitBehavior := rxbHaltOnFailures
else
ExitBehavior := rxbContinue;
TextTestRunner.RunRegisteredTests(ExitBehavior);
TestCase的多种Registration方式
可用Test Suites,在Dunit的Examples的Registry用三个项目描述了不同的Registration
在第一个项目中PRoject文件如下:
program RegistryTest;
uses
TestFramework,
GUITestRunner,
RegistryUnitTest;
{$R *.RES}
function MasterTestSuite: ITestSuite; //请注意该函数与单元文件的关系
begin
Result := TTestSuite.Create;
Result.AddTest(RegistryUnitTest.Suite);
end;
begin
GUITestRunner.RunTest(MasterTestSuite);
end.
单元文件如下:
type
TTestRegistry = class(TTestCase)
private
FInstallPath: string;
FModData: string;
FReg: TRegistry;
public
procedure Setup; override;
procedure TearDown; override;
published
procedure TestRegistrySample;
end;
function Suite: ITestSuite;
implementation
function Suite: ITestSuite; //请注意该函数与Project文件的关系
begin
Suite := TTestSuite.Create(TTestRegistry);
end;
在第二个项目中Project文件如下(其它部分与第一项目相同):
function MasterTestSuite: ITestSuite; //请注意该函数与单元文件的关系
begin
Result := TTestSuite.Create;
Result.AddTest(RegistryUnitTest.Suite);
end;
begin
GUITestRunner.RunTest(MasterTestSuite);
end.
单元文件(其他部分与第一项目中单元文件相同)
function Suite: ITestSuite; //请注意该函数与Project文件的关系
begin
Suite := TTestRegistry.Suite;
end;
在第三个项目中Project文件如下(其它部分与第一项目相同):
function MasterTestSuite: ITestSuite;
begin
Result := TTestSuite.Create;
Result.AddTest(RegistryUnitTest.Suite);
end;
begin
GUITestRunner.RunTest(MasterTestSuite);
end.
单元文件(其他部分与第一项目中单元文件相同)
function Suite: ITestSuite; //请注意该函数与Project文件的关系
begin
Suite := TTestSuite.Create(TTestRegistry);
end;
Registration的相关方法:
procedure RegisterTest(SuitePath: string; test: ITest); overload;
procedure RegisterTest(test: ITest); overload;
procedure RegisterTests(SuitePath: string; const Tests: array of ITest); overload;
procedure RegisterTests(const Tests: array of ITest); overload;
function RegisteredTests: ITestSuite;
procedure ClearRegistry;
Dunit的Exception测试:
TexceptionTestCase没有实现,但是Dunit在源码附加\examples\testexception目录中有一个如何测试Exception的例子。主要的实现在procedure TTestMyObject.CheckException和procedure TTestMyObjectOverrideRunTest.RunTest中。
在异常测试的例子中主要有三个方法,除前面所说的两个外还有一个assert方法
procedure TTestMyObject.CheckException(AMethod: TTestMethod;
AExceptionClass: ExceptionClass);
begin
try
AMethod;
fail('Expected exception not raised');
except
on E: Exception do
begin
if E.ClassType <> AExceptionClass then
raise;
end
end;
end;
procedure TTestMyObject.testMyObject;
begin
try
FMyObject.DoSomething;
except
assert(false);
end;
end;
procedure TTestMyObjectOverrideRunTest.RunTest(testResult :TTestResult);
begin
try
inherited runTest(testResult);
if FExpectedException <> nil then
fail('Excepted Exception did not occur');
except
on E: Exception do
begin
if FExpectedException = nil then
raise
else
if E.ClassType <> FExpectedException then
raise;
end;
end;
{ clear the exception until the next test registers an Exception }
FExpectedException := nil;
end;
Check的相关方法:
procedure Check(condition: boolean; msg: string = '');
procedure CheckEquals(expected, actual: extended; msg: string = '');
procedure CheckEquals(expected, actual: extended; delta: extended; msg: string = '');
procedure CheckEquals(expected, actual: integer; msg: string = '');
procedure CheckEquals(expected, actual: string; msg: string = '');
procedure CheckEquals(expected, actual: boolean; msg: string = '');
procedure CheckEqualsBin(expected, actual: longWord; msg: string = '';
digits: integer=32);
procedure CheckEqualsHex(expected, actual: longword; msg: string = '';
digits: integer=8);
procedure CheckNotEquals(expected, actual: integer; msg: string = '');
procedure CheckNotEquals(expected: extended; actual: extended; delta: extended = 0;
msg: string = '');
procedure CheckNotEquals(expected, actual: string; msg: string = '');
procedure CheckNotEquals(expected, actual: boolean; msg: string = '');
procedure CheckNotEqualsBin(expected, actual: longword; msg: string = '';
digits: integer=32);
procedure CheckNotEqualsHex(expected, actual: longword; msg: string = '';
digits: integer=8);
procedure CheckNotNull(obj :IUnknown; msg :string = '');
procedure CheckNull(obj: IUnknown; msg: string = '');
procedure CheckSame(expected, actual: IUnknown; msg: string = '');
procedure CheckSame(expected, actual: TObject; msg: string = '');
procedure CheckNotNull(obj: TObject; msg: string = '');
procedure CheckNull(obj: TObject; msg: string = '');
procedure CheckException(AMethod: TTestMethod; AExceptionClass: TClass;
msg :string = '');
procedure CheckEquals( expected, actual: TClass; msg: string = '');
procedure CheckInherits(expected, actual: TClass; msg: string = '');
procedure CheckIs(obj :TObject; klass: TClass; msg: string = '');
- ››Dunit的感悟
更多精彩
赞助商链接