现在位置:主页>资源共享> 文章内容

Delphi单元测试工具Dunit介绍

我要投稿更新日期:2008-07-22 点击:
Dunit基本介绍
    Dunit是Xunit家族中的一员,用于Dephi的单元测试。是Extreme Programming测试实现Xtreme Testing的一种工具。Dunit是一个Free的测试工具,没有代码覆盖率功能。
Dunit的官方Web Site 是https://sourceforge.net/projects/dunit/。
使用Dunit应该先看看Dunit安装目录下的doc\README.html。本文也是参看Readme写的。
配置测试环境
在使用Dunit前应该将下载的Dunit解压。然后后将Dunit的路径加到菜单  Tools->Environment Options 里面的Library->Library Path中。


Dunit的主要文件

File  Description
TestFramework.pas  The framework itself.
TestExtensions.pas  Decorator classes that may be used to extend test cases.
GUITesting.pas  Classes for testing user interfaces (Forms and Dialogs).
TextTestRunner.pas  Routines to run tests in console mode.
GUITestRunner.pas  The graphical user interface to the framework..
GUITestRunner.dfm  The GUITestRunner Form

Dunit基本实现方法(GUI方式)
Dunit的基本实现思路是将被测试代码(单元)与测试代码(单元)分开。提供一个FrameWork及一个运行界面。 所有的测试单元都应继承TtestCase。
运行GUI界面

   运行TestCase

这里要注意的一点是SetUp方法和TearDown是每个测试方法运行时都被调用的,如果想要只运行一次Setup及TearDown,应该使用TtestSetup类,具体情况后面《Dunit附加功能》一节。
创建一个简单的例子
创建一个被测试的Project
创建一个名为BeTestProject的Project,将确省的Unit1保存为BeTestUnit.pas文件。把确省的TForm1改名为BeTestForm中增加一个Public的函数BeTestFunction,BeTestFunction代码如下:
function BeTestForm.BeTestFunction(i,j:integer):integer;
begin
  Result:=i*j;
end;

创建一个测试Project
创建新的Project
再创建一个Project,命名为TestProject。如果没有和BeTestProject放在同一目录,将BeTestProject的存放路径加到加到菜单  Tools->Environment Options 里面的Library->Library Path中。


编写TestCase
删除确省的Unit1(Form1),创建一个的Unit,注意不是Form.


将创建的Unit保存为TestUnit,在interface中加入以下代码
uses
      TestFrameWork,BeTestUnit;
    TestFrameWork是每个TestCase都必须使用的,后面要使用的TtestCase等类的定义都在TestFrameWork中。BeTestUnit是将要被测试单元。
   
定义TestCase,测试类定义代码如下:
TTestCaseFirst = class(TTestCase)
private
   BeTestForm : TBeTestForm;  //要测试的类
protected
   procedure SetUp; override;  //初始化类
   procedure TearDown; override;  //清除数据
published
   procedure TestFirst;      //第一个测试方法
   procedure TestSecond;     //第二个测试方法
end;
在定义测试方法时候注意,Dunit是通过RTTI(RunTime Type Information)来寻找并自动注册测试方面的,具体实现是通过代码
TestFramework.RegisterTest(TTestCaseFirst.Suite);
这段代码将在后面提到,TtestCaseFirst.Suit在寻找的规则是:
1、  测试方法是没有参数的Procedure
2、  测试方法被申明为Published

SetUp,TearDown是在运行测试方法前、后运行的,所有一般把要测试的类的初始化及清除放在这两个过程中。

以下是实现的代码:

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的实现代码:
procedure TTestCase.Check(condition :boolean; msg :string);begin    if (not condition) then        Fail(msg, CallerAddr);End;
如果Check没有通过的话,Dunit将报错。错误提示就在第二个参数中定义,其他有关类及方法的定义请看连机文档,文档放在
Dunit安装目录\doc\API\IDH_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.
上面的加粗代码是要增加和修改。

运行测试例子
  运行的测试结果如下:





上一页12 3 下一页
所有评论

评论列表


我也评论来评论! 点击此处参与本文评论

注意:本站采用匿名评论,请各位网友注意自己的言行