首页 > 学院 > 开发设计 > 正文

Dunit的感悟

2019-11-18 18:18:12
字体:
来源:转载
供稿:网友

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 = '');

 


上一篇:Pascal精要笔记

下一篇:老文章——TWebBrowser编程简述

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
学习交流
热门图片

新闻热点

疑难解答

图片精选

网友关注