Dunit初步詳解

發表于:2011-12-22來源:未知作者:娃娃點擊數: 標簽:dunit
本文講解了Dunit的最基本使用方法,是我再初識Dunit的一點積攢,現在總結出來供Dunit學習者起步之用,至于更深入的研究還靠讀者們的細心研究與不斷的實踐再實踐!本文如有講解錯誤之處還請讀者朋友們積極提出,我們共同討論,共同進步! 如有轉載請注

  本文講解了Dunit的最基本使用方法,是我再初識Dunit的一點積攢,現在總結出來供Dunit學習者起步之用,至于更深入的研究還靠讀者們的細心研究與不斷的實踐再實踐!本文如有講解錯誤之處還請讀者朋友們積極提出,我們共同討論,共同進步!

  如有轉載請注明作者及出處。

  Dunit初步詳解

  一、安裝Dunit

  將dunit-9.2.1(本文以dunit-9.2.1為例)解壓縮到文件夾F:\DUnit案例\dunit-9.2.1,

  (dunit-9.2.1無需安裝,它提供的是測試框架和一些測試類,只需要在Delphi中調用即可)

  主要類型:

  TestFramework.pas 框架本身

  TestExtensions.pas 可用來擴充測試案例的 Decorator 類別

  GUITesting.pas 用來測試使用者介面的類別

  TextTestRunner.pas 在主控臺模式下執行測試的函式

  GUITestRunner.pas 此框架的圖形化使用者界面

  GUITestRunner.dfm GUITestRunner Form

  二、設計測試案例

  本文以Delphi 6開發環境為例,在這里我介紹兩種單元測試案例:

  一種是簡單的不需調用其他Project的測試案例TestCase1;

  另一種是調用其他Project中函數的測試案例TestCase2。

  下面就開始我們的Dunit之旅:

  TestCase1

  1.首先將Dunit的路徑加載到Delphi中,

  Tools ->Environment ->Options ->Library->Library path,

  注意:一定要把路徑名給到src文件夾下。

  2.新建一個項目,關閉Delphi自動啟動的Form1,Unit1.新建一個沒有Form的項目,File->New->Unit,保存:將項目保存為Project1Test.dpr,Unit1保存為Project1TestCases.pas。

  在Project1TestCases.pas中敲入如下代碼:

  (你可以用如下代碼替換掉Project1TestCases.pas中的代碼,假如你很懶的話!)

  unit Project1TestCases;

  interface

  uses

  TestFrameWork; // TestFrameWork是每個測試用例都必須使用的類

  type

  TTestCaseFirst = class(TTestCase) // TTestCase包含在TestFrameWork中

  published

  procedure TestFirst; // 聲明一個測試用例

  end;

  implementation

  procedure TTestCaseFirst.TestFirst;

  begin

  Check(1 + 1 = 2, 'Catastrophic arithmetic failure!');

  end;

  initialization

  TestFramework.RegisterTest(TTestCaseFirst.Suite); // TestFramework.RegisterTest 程序會把傳入的測試案例組件注冊到此框架的注冊系統里

  end.

  3.修改Project主文件,點擊Project ->View Source,查看項目的源碼。把 TestFrameWork 以及 GUITestRunner 加到 uses 子句里,然后清除預制的 Application 程序代碼,並以下面的程序碼取代:

  program Project1Test;

  uses

  Forms,

  TestFrameWork,

  GUITestRunner,

  Project1TestCases in 'Project1TestCases.pas';

  {$R *.RES}

  begin

  Application.Initialize;

  GUITestRunner.RunRegisteredTests;

  end.

  4.Ok了,現在開始運行程序,將會出現DUnit 的 GUITestRunner 窗體,點擊一下Run按鈕,將會執行我們的測試用例。

  這里我們只有一個測試用例,測試用例執行正確。

  TestCase2

  1. 首先,同樣我們要將Dunit的路徑加載到Delphi中,然后我們建立一個別測試的Project,并命名為BeTestProject.dpr,將From1命名為BeTestForm,Unit1命名為BeTestUnit.pas。

  2. 在BeTestUnit中敲入代碼如下:

  unit BeTestUnit;

  interface

  uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs;

  type

  TForm1 = class(TForm)

  private

  { Private declarations }

  public

  function BeTestFunction(i,j:integer):integer;

  { Public declarations }

  end;

  var

  BeTestForm: TForm1;

  implementation

  function TForm1.BeTestFunction(i,j:integer):integer;

  begin

  result:=i*j;

  end;

  {$R *.dfm}

  end.

  3.在BeTestProject的源碼如下:

  program BeTestProject;

  uses

  Forms,

  BeTestUnit in 'BeTestUnit.pas' {Form1};

  {$R *.res}

  begin

  Application.Initialize;

  Application.CreateForm(TForm1, BeTestForm);

  Application.Run;

  end.

  注:由于此被測單元代碼簡單易懂,這里就不進行注釋了!

  4.下面編寫用例,先新建一個項目,關閉Delphi自動啟動的Form1,Unit1.新建一個沒有Form的File->New->Unit,保存:將項目保存為TestCaseProject.dpr,Unit1保存為TestUnit.pas。

  在TestUnit中敲入如下代碼:

  unit TestUnit;

  interface

  uses

  TestFrameWork,BeTestUnit;

  Type

  TTestCaseFirst=class(TTestCase)

  published

  procedure TestFirst;

  procedure TestSecond;

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97