单元测试之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.dprUnit1保存为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.

 

4Ok了,现在开始运行程序,将会出现DUnit GUITestRunner 窗体,点击一下Run按钮,将会执行我们的测试用例。

这里我们只有一个测试用例,测试用例执行正确。

 

TestCase2

 

1.  首先,同样我们要将Dunit的路径加载到Delphi中,然后我们建立一个别测试的Project,并命名为BeTestProject.dpr,将From1命名为BeTestFormUnit1命名为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.新建一个没有FormFile->New->Unit,保存:将项目保存为TestCaseProject.dprUnit1保存为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出现!

 

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

如有转载请著名作者及出处。

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值