Dunit的感悟

2016-02-19 12:49 10 1 收藏

有一种朋友不在生活里,却在生命力;有一种陪伴不在身边,却在心间。图老师即在大家的生活中又在身边。这么贴心的服务你感受到了吗?话不多说下面就和大家分享Dunit的感悟吧。

【 tulaoshi.com - 编程语言 】

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

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

           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

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  Result := TTestSuite.Create;

  Result.AddTest(RegistryUnitTest.Suite);

end;

begin

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  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

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  Suite := TTestSuite.Create(TTestRegistry);

end;

 

在第二个项目中Project文件如下(其它部分与第一项目相同)

function MasterTestSuite: ITestSuite;   //请注意该函数与单元文件的关系

begin

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  Result := TTestSuite.Create;

  Result.AddTest(RegistryUnitTest.Suite);

end;

begin

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  GUITestRunner.RunTest(MasterTestSuite);

end.

 

单元文件(其他部分与第一项目中单元文件相同)

function Suite: ITestSuite;    //请注意该函数与Project文件的关系

begin

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  Suite := TTestRegistry.Suite;

end;

 

在第三个项目中Project文件如下(其它部分与第一项目相同):

function MasterTestSuite: ITestSuite;

begin

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  Result := TTestSuite.Create;

  Result.AddTest(RegistryUnitTest.Suite);

end;

 

begin

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  GUITestRunner.RunTest(MasterTestSuite);

end.

单元文件(其他部分与第一项目中单元文件相同)

function Suite: ITestSuite;    //请注意该函数与Project文件的关系

begin

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  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在源码附加examplesestexception目录中有一个如何测试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 = '');

 

来源:http://www.tulaoshi.com/n/20160219/1602073.html

延伸阅读
标签: 网络游戏
《大冲锋》玩家心情杂谈 玩后的体会与感悟 转载自官方论坛 作者:沙小加 玩《 大冲锋 》有一段日子了,总体感觉还是不错的,无论是画面或人物,都比以往的 游戏 要好看一些,可就是当你被对方杀死都后,还要有个定格,有定格也行,可这定格的时间也太长了点吧!?在加上读秒,感觉时间就更长了。希望可以把时...
标签: 电脑入门
感悟幸福的签名:是不是爱的越深,越容易出现矛盾 用莪扪的爱来证明不是虚幻。 莪无法忘却迩旳笑脸,更无法摆脱记忆旳纠缠。 爱只是个字罢啦,能代表什么???  不拉黑你,是因为我想看你每天的心情  跟自己说;算了吧 人家现在过的好不好,跟你有什么关系。  贫僧我不能谱渡众生,但贫僧我能祸害苍生   在我...
  之前用的是Java那一套东西,有Eclipse什么都搞定了。现在因为要用Delphi改一个即时通讯软件的缘故,想着怎么能把重构和单元测试那一套搬到Delphi这边来。书上说给现有的代码加单元测试能够加深对代码的理解,并且可以作为改善代码的基础,这不正是我要做的事情吗?于是,为了搭建这么一个敏捷平台,我以Delphi2005和DUnit进行了一点...
标签: Delphi
  之前用的是Java那一套东西,有Eclipse什么都搞定了。现在因为要用Delphi改一个即时通讯软件的缘故,想着怎么能把重构和单元测试那一套搬到Delphi这边来。书上说给现有的代码加单元测试能够加深对代码的理解,并且可以作为改善代码的基础,这不正是我要做的事情吗?于是,为了搭建这么一个敏捷平台,我以Delphi2005和DUnit进行了一点...
标签: SQLServer
想必大家都知道MSSQL中SA权限是什么,可以说是至高无上。今天我就它的危害再谈点儿,我所讲的是配合NBSI上传功能得到WebShell。在讲之前先说几个条件,否则得到Shell是有难度的。 1.存在SQL注入,并且数据库类型是MSSQL。 2.连接数据库的权限必须是SA。 3.后台必须有文件上传的程序。 好了,我们找到一个网址hxxp://www.****...

经验教程

201

收藏

41
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部