当前位置:主页>Delphi教程>文章内容
Dunit的感悟
来源: 作者: 发布时间:2007-04-29  

Dunit的感悟

 

DunitTextTestRunner方式测试

DunitTextTestRunner测试方式中需在工程文件中引用TextTestRunner而非GUITestRunner

DunitTextTestRunner测试方式中,Dunit提供了TRunnerExitBehavior数据类型,在dunitTRunnerExitBehavior的定义如下:

TRunnerExitBehavior = ( rxbContinue, rxbPause, rxbHaltOnFailures);

从该数据类型的定义可得知,该数据类型定义了TextTestRunner的退出行为,即何种方式结束当前的测试。只需在TextTestRunner执行RunRegisteredTestsExitBehavior)时把需要的退出行为作为参数传入,即可控制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,DunitExamplesRegistry用三个项目描述了不同的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;

 

DunitException测试:

TexceptionTestCase没有实现,但是Dunit源码附加\examples\testexception目录中有一个如何测试Exception的例子。主要的实现在procedure TTestMyObject.CheckExceptionprocedure 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 = '');

 


 
上一篇:DELPHI中动态获得SQLSERVER数据库名   下一篇:利用Delphi中的画布画树
 
  相关文章
·DELPHI中动态获得SQLSERVER数据库名
·利用Delphi中的画布画树
·在Delphi中调用CHM帮助文件
·Delphi的编码规范
·Delphi多层开发方案比较
·WANT的心得
·delphi中将UCS2编码的字符串转化为GB23
·Delphi中实现界面与业务逻辑的分离
·老文章——TWebBrowser编程简述
·精确计算PI小数点后800位小数
·delphi中的XML解析控件TXMLDocument的
·INI文件的读写
 
【关闭窗口】
推荐本站资源
最新文章