Dunit初步详解

发表于:2007-4-12 16:00

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:磊哥    来源:51testing论坛

本文讲解了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出现!

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



附件:案例.rar(2006-6-15 16:30, 653.91 K)


Dunit的路径加载到Delphi中



TestCase1 运行结果



TestCase2 运行结果

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号