磊哥 发表于 2006-6-15 16:30:59

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;

end;

implementation

procedure TTestCaseFirst.TestFirst;
begin
Check(BeTestForm.BeTestFunction(1,3)=3,'First Test fail');
end;

procedure TTestCaseFirst.TestSecond;
begin
Check(BeTestForm.BeTestFunction(1,3)=6,'Second Test fail');
end;

initialization
TestFramework.RegisterTest(TTestCaseFirst.Suite);

end.

5.修改Project主文件,点击Project->View Source,查看项目的源码。並以下面的程序码取代:
program TestCaseProject;

uses
Forms,
TestFrameWork,
GUITestRunner,
TestUnit in 'TestUnit.pas';

{$R *.res}

begin
Application.Initialize;
//Application.Run;
GUITestRunner.RUnRegisteredTests;
end.

6.一切搞定,注意一点(很重要):被测单元和测试用例一定要保存在同一个目录下!
下面开始运行我们的TestCase.,点击运行按钮。
我们这里有一个TestSecond是错误的,所以执行中会有Failures出现!

下面附上两个案例的源码文件!

[ 本帖最后由 磊哥 于 2006-6-29 15:43 编辑 ]

磊哥 发表于 2006-6-29 15:43:41

不知道最近学习Dunit的人还多吗??

有什么问题大家一起来交流吧!!!!

gwell 发表于 2007-4-9 19:10:12

delphi

youqi 发表于 2007-5-15 13:17:00

很好

下了拿回去学习sdlkfj2

joffre 发表于 2007-7-3 16:23:28

学习学习。谢谢

xieph 发表于 2009-2-23 11:33:06

谢谢楼主,下载学习一下
页: [1]
查看完整版本: Dunit初步详解