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/