程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Dunit的感悟

Dunit的感悟

編輯:Delphi

Dunit的感悟<?XML:namespace prefix = o ns = "urn:schemas-microsoft-com:office:Office" />

 

DunitTextTestRunner方式測試

在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;

 

DunitException測試:

TexceptionTestCase沒有實現,但是Dunit在源碼附加examples estexception目錄中有一個如何測試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 = '');

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved