1
Dunit初步详解2

3
文章出处:51testing论坛 作者:磊哥 发布时间:2006-06-194

5
本文讲解了Dunit的最基本使用方法,是我再初识Dunit的一点积攒,现在总结出来供Dunit学习者起步之用,至于更深入的研究还靠读者们的细心研究与不断的实践再实践!本文如有讲解错误之处还请读者朋友们积极提出,我们共同讨论,共同进步!6
如有转载请注明作者及出处。7
Dunit初步详解8

9
一、安装Dunit10
将dunit-9.2.1(本文以dunit-9.2.1为例)解压缩到文件夹F:\DUnit案例\dunit-9.2.1,11
(dunit-9.2.1无需安装,它提供的是测试框架和一些测试类,只需要在Delphi中调用即可)12
主要类型:13
TestFramework.pas 框架本身14
TestExtensions.pas 可用来扩充测试案例的 Decorator 类別15
GUITesting.pas 用来测试使用者介面的类別16
TextTestRunner.pas 在主控台模式下执行测试的函式17
GUITestRunner.pas 此框架的图形化使用者界面18
GUITestRunner.dfm GUITestRunner Form19
二、设计测试案例20
本文以Delphi 6开发环境为例,在这里我介绍两种单元测试案例:21
一种是简单的不需调用其他Project的测试案例TestCase1;22
另一种是调用其他Project中函数的测试案例TestCase2。23
下面就开始我们的Dunit之旅:24
TestCase125
1.首先将Dunit的路径加载到Delphi中,26
Tools ->Environment ->Options ->Library->Library path,27
注意:一定要把路径名给到src文件夹下。28
2.新建一个项目,关闭Delphi自动启动的Form1,Unit1.新建一个没有Form的项目,File->New->Unit,保存:将项目保存为Project1Test.dpr,Unit1保存为Project1TestCases.pas。29
在Project1TestCases.pas中敲入如下代码:30
(你可以用如下代码替换掉Project1TestCases.pas中的代码,假如你很懒的话!)31
unit Project1TestCases;32
interface33
uses34
TestFrameWork; // TestFrameWork是每个测试用例都必须使用的类35
type36
TTestCaseFirst = class(TTestCase) // TTestCase包含在TestFrameWork中37
published38
procedure TestFirst; // 声明一个测试用例 39
end;40
implementation41
procedure TTestCaseFirst.TestFirst;42
begin43
Check(1 + 1 = 2, 'Catastrophic arithmetic failure!');44
end;45
initialization46
TestFramework.RegisterTest(TTestCaseFirst.Suite); // TestFramework.RegisterTest 程序会把传入的测试案例组件注冊到此框架的注冊系统里47
end.48
3.修改Project主文件,点击Project ->View Source,查看项目的源码。把 TestFrameWork 以及 GUITestRunner 加到 uses 子句里,然后清除预制的 Application 程序代码,並以下面的程序码取代:49
program Project1Test;50
uses51
Forms,52
TestFrameWork,53
GUITestRunner,54
Project1TestCases in 'Project1TestCases.pas';55
{$R *.RES}56
begin57
Application.Initialize;58
GUITestRunner.RunRegisteredTests;59
end.60
4.Ok了,现在开始运行程序,将会出现DUnit 的 GUITestRunner 窗体,点击一下Run按钮,将会执行我们的测试用例。61
这里我们只有一个测试用例,测试用例执行正确。62
TestCase263
1. 首先,同样我们要将Dunit的路径加载到Delphi中,然后我们建立一个别测试的Project,并命名为BeTestProject.dpr,将From1命名为BeTestForm,Unit1命名为BeTestUnit.pas。64
2. 在BeTestUnit中敲入代码如下:65
unit BeTestUnit;66
interface67
uses68
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,69
Dialogs;70
type71
TForm1 = class(TForm)72
private73
{ Private declarations }74
public75
function BeTestFunction(i,j:integer):integer;76
{ Public declarations }77
end;78
var79
BeTestForm: TForm1;80
implementation81
function TForm1.BeTestFunction(i,j:integer):integer;82
begin83
result:=i*j;84
end;85
{$R *.dfm}86
end.87
3.在BeTestProject的源码如下:88
program BeTestProject;89
uses90
Forms,91
BeTestUnit in 'BeTestUnit.pas' {Form1};92
{$R *.res}93
begin94
Application.Initialize;95
Application.CreateForm(TForm1, BeTestForm);96
Application.Run;97
end.98
注:由于此被测单元代码简单易懂,这里就不进行注释了!99
4.下面编写用例,先新建一个项目,关闭Delphi自动启动的Form1,Unit1.新建一个没有Form的File->New->Unit,保存:将项目保存为TestCaseProject.dpr,Unit1保存为TestUnit.pas。100
在TestUnit中敲入如下代码:101
unit TestUnit;102
interface103
uses104
TestFrameWork,BeTestUnit;105
Type106
TTestCaseFirst=class(TTestCase)107
published108
procedure TestFirst;109
procedure TestSecond;110
end;111
implementation112
procedure TTestCaseFirst.TestFirst;113
begin114
Check(BeTestForm.BeTestFunction(1,3)=3,'First Test fail');115
end;116
procedure TTestCaseFirst.TestSecond;117
begin118
Check(BeTestForm.BeTestFunction(1,3)=6,'Second Test fail');119
end;120
initialization121
TestFramework.RegisterTest(TTestCaseFirst.Suite);122
end.123
5.修改Project主文件,点击Project->View Source,查看项目的源码。並以下面的程序码取代:124
program TestCaseProject;125
uses126
Forms,127
TestFrameWork,128
GUITestRunner,129
TestUnit in 'TestUnit.pas';130
{$R *.res}131
begin132
Application.Initialize;133
//Application.Run;134
GUITestRunner.RUnRegisteredTests;135
end.136
6.一切搞定,注意一点(很重要):被测单元和测试用例一定要保存在同一个目录下!137
下面开始运行我们的TestCase.,点击运行按钮。138
我们这里有一个TestSecond是错误的,所以执行中会有Failures出现!139
下面附上两个案例的源码文件!140

141

142


144

145
Dunit的路径加载到Delphi中 146

147
148

149
TestCase1 运行结果150

151
152

153
TestCase2 运行结果154

155
156

157

158

浙公网安备 33010602011771号