OpenCTF組件測試框架有助於在Delphi應用程序中為所有(視覺和非視覺)組件構建自動測試。它基於DUNIT框架,並且與現有的測試項目充分集成。
此示例測試項目包含一種形式和一個框架。
program FormTests;
uses
OpenCTF,
ctfConfig, // configure OpenCTF with our test classes
GUITestRunner,
TestForm in ' TestForm.pas ' { Form1 } ,
TestFrame in ' TestFrame.pas ' { Frame1: TFrame } ;
begin
OpenCTF.RegisterFormClasses([TForm1, TFrame1]);
RunRegisteredTests;
end .該框架使用自定義的TcomponentHandler子類來構建組件測試。
在此示例中,我們只想測試tomagelist組件,因此我們覆蓋構造函數以指定要測試的類。
TImageListTests = class (TComponentHandler)
protected
procedure AddTests ; override;
public
constructor Create;
end ;
...
constructor TImageListTests.Create;
begin
inherited Create(Controls.TImageList); // test only this class (and subclasses)
end ;
procedure TImageListTests.AddTests ;
begin
inherited ;
CurrentSuite.AddTest(TImageListMustContainImages.Create(CurrentComponent)); // see below
// add more tests ...
end ;
我們的自定義測試類來自TCOMPONENT -TONTEST(又是Dunit TabStractTest)。因此,我們可以使用所有Checkequals / checknotequals / ... Dunit的方法。對於我們的示例,我們僅測試成像庫中圖像的數量不等於零。請注意,對於表單上的每個組件,將創建一個tcomponent -Test實例,其組件屬性指向正在測試的組件。
TImageListMustContainImages = class (TComponentTest)
protected
procedure RunTest (testResult: TTestResult); override;
end ;
procedure TImageListMustContainImages.RunTest ;
begin
inherited ;
CheckNotEquals( 0 , (Component as TImageList).Count, ' ImageList is empty ' );
end ;現在,我們需要將測試添加到我們的設置中。在註冊表單類之前必鬚髮生這種情況。
program FormTests;
uses
OpenCTF,
ctfTestControls, // unit containing my custom TImageList tests
GUITestRunner,
TestForm in ' TestForm.pas ' { Form1 } ,
TestFrame in ' TestFrame.pas ' { Frame1: TFrame } ;
begin
OpenCTF.Add(TImageListTests.Create);
OpenCTF.RegisterFormClasses([TForm1, TFrame1]);
RunRegisteredTests;
end .您可以在項目源文件(DPR)中放置配置,而可以編寫一個單元並在此處包含所有測試(和必需的單元)。
program FormTests;
uses
OpenCTF,
ctfConfig, // unit containing my custom configuration
GUITestRunner,
TestForm in ' TestForm.pas ' { Form1 } ,
TestFrame in ' TestFrame.pas ' { Frame1: TFrame } ;
begin
OpenCTF.RegisterFormClasses([TForm1, TFrame1]);
RunRegisteredTests;
end .配置示例在初始化部分中執行所有設置。這樣可以保存在項目源代碼中的鍵入,僅添加CTFConfig單元,並且將添加測試將被添加到框架中。
unit ctfConfig;
interface
implementation
uses
OpenCTF,
ctfTestForm, ctfTestNames, ctfTestTabOrder, ctfTestMenus,
StdCtrls, ExtCtrls, Forms, Classes;
initialization
OpenCTF.Add(TBasicFormTests.Create);
OpenCTF.Add(TComponentNameTests.Create);
OpenCTF.Add(TTabOrderTests.Create);
OpenCTF.Add(TMenuItemTests.Create);
end . 在某些情況下,可能會跳過某些課程的測試。例如,每個組件必須具有非默認名稱('button1'和'button2')的嚴格規則可能太嚴格了。
該配置允許從特定測試中排除某些類。這是初始化部分,有其他排除:
initialization
OpenCTF.Add(TBasicFormTests.Create);
OpenCTF.Add((TComponentNameTests.Create)
.Exclude(StdCtrls.TLabel) // exclude TLabel
.Exclude(Forms.TFrame) // exclude TFrame
.Exclude(ExtCtrls.TPanel) // exclude TPanel
);
OpenCTF.Add(TTabOrderTests.Create);
OpenCTF.Add(TMenuItemTests.Create);
end . 與Delphi 2009進行了編譯和測試。
使用框架需要兼容的Delphi和Dunit版本。
https://michaeljustin.github.io/openctf/
可以在不運行構建腳本的情況下使用OpenTCTF。構建腳本對OpenCTF開發人員很有用。他們生成API文檔,編譯示例Projets,並構建一個源分發軟件包。
Apache Ant:https://ant.apache.org
doxygen:http://www.doxygen.nl
pas2dox 0.41:https://sourceforge.net/projects/pas2ddox/