您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 >
Delphi單元測試工具Dunit介紹
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2013/3/7 13:32:36 ] 推薦標簽:

    定義TestCase,測試類定義代碼如下:

    TTestCaseFirst = class(TTestCase)

    private

    BeTestForm : TBeTestForm; //要測試的類

    protected

    procedure SetUp; override; //初始化類

    procedure TearDown; override; //清除數(shù)據(jù)

    published

    procedure TestFirst; //第一個測試方法

    procedure TestSecond; //第二個測試方法

    end;

    在定義測試方法時候注意,Dunit是通過RTTI(RunTime Type Information)來尋找并自動注冊測試方面的,具體實現(xiàn)是通過代碼

    TestFramework.RegisterTest(TTestCaseFirst.Suite);

    這段代碼將在后面提到,TtestCaseFirst.Suit在尋找的規(guī)則是:

    1、測試方法是沒有參數(shù)的Procedure

    2、測試方法被申明為Published

    SetUp,TearDown是在運行測試方法前、后運行的,所有一般把要測試的類的初始化及清除放在這兩個過程中。

    以下是實現(xiàn)的代碼:

    procedure TTestCaseFirst.SetUp;

    begin

    BeTestForm := TBeTestForm.Create(Nil);

    end;

    procedure TTestCaseFirst.TearDown;

    begin

    BeTestForm.Destroy;

    end;

    procedure TTestCaseFirst.TestFirst; //第一個測試方法

    begin

    Check(BeTestForm.BeTestFunction(1,3) = 3,'First Test fail');

    end;

    procedure TTestCaseFirst.TestSecond; //第二個測試方法

    begin

    Check(BeTestForm.BeTestFunction(1,3)=4,'Second Test fail');

    end;

    //Register TestCase

    initialization

    TestFramework.RegisterTest(TTestCaseFirst.Suite);

    end.

    Check是TestCase類提供的一個方法。以下是TestCase的實現(xiàn)代碼:

    procedure TTestCase.Check(condition :boolean; msg :string);
    begin
    if (not condition) then
    Fail(msg, CallerAddr);
    End;

    如果Check沒有通過的話,Dunit將報錯。錯誤提示在第二個參數(shù)中定義,其他有關(guān)類及方法的定義請看連機文檔,文檔放在

    Dunit安裝目錄docAPIIDH_Library_DUnit_-_Xtreme_Unit_Testing_for_Delphi.htm

    Initialzation代碼完成測試單元的注冊。

    修改Project主文件
    運行前的后一步是修改Project主文件TestProject.dpr。先使用菜單Project->View Source打開TestProject.dpr.

    修改后的代碼如下:

    program TestProject;

    uses

    Forms,

    TestFrameWork,

    GUITestRunner,

    TestUnit in 'TestUnit.pas';

    {$R *.res}

    begin

    Application.Initialize;

    //Application.Run;

    GUITestRunner.RunRegisteredTests;

    end.

    上面的加粗代碼是要增加和修改。

    運行測試例子
    運行的測試結(jié)果如下:

    使用TestSuite
    使用TestSuite的目的是對TestCase進行分類管理,如果我們再增加一個TestCase 如下

    TTestCaseSecond = class(TTestCase)

    published

    procedure TestThrid;

    end;

    添加TestThrid實現(xiàn)代碼后,在initialization代碼處增加

    TestFramework.RegisterTest(TTestCaseSecond.Suite);

    運行以后我們可以看到結(jié)果如下:

    如果我們將initialization處的代碼改為如下:

    initialization

    TestFramework.RegisterTest('Simple suite',TTestCaseFirst.Suite);

    TestFramework.RegisterTest('Simple suite',TTestCaseSecond.Suite);

    end.

上一頁1234下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd