今天开始学习Dunit
先说下载,Dunit 的官方Web Site 是https://sourceforge.net/projects/dunit/,我用的就是从那下的,不收费。
使用 Dunit前 应该先看看Dunit 安装目录下的doc\README_CHT.html繁体中文的,写的很好。
再说说安装,中文的:工具->环境选项->库->库路径->将Dunit路径添加里面,还要添加Dunit下src文件夹的路径。英文的Tools->Environment Options->Library->Library Path.
--------------------------------------------------------
现在我新建一个要被测试的工程名叫SUM_Project
窗口叫Form_Main,单元改名为Form_Main_Unit
在窗口放三个Edit 名字叫 number_Edit1、number_Edit2、SUM_Edit
和一个Button 名字叫 SUM_Button
代码如下
unit Form_Main_Unit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm_Main = class(TForm)
number_Edit1: TEdit;
number_Edit2: TEdit;
SUM_Button: TButton;
SUM_Edit: TEdit;
procedure SUM_ButtonClick(Sender: TObject);
private
{ Private declarations }
public
function SUM_Function(i,j:integer):integer;
end;
var
Form_Main: TForm_Main;
implementation
{$R *.dfm}
procedure TForm_Main.SUM_ButtonClick(Sender: TObject);
var
Int_num1,Int_num2,Int_sum:integer ;//第一个数,第二个数,和
begin
if (number_Edit1.Text='')or(number_Edit2.Text='') then
begin
//如果number_Edit1或number_Edit2有一个为空,输出error
ShowMessage('error');
end
else
begin
Int_num1:=StrToInt(number_Edit1.Text);
Int_num2:=StrToInt(number_Edit2.Text);
Int_sum:=SUM_Function(Int_num1,Int_num2);
SUM_Edit.Text:=IntToStr(Int_sum);
end
end;
function TForm_Main.SUM_Function(i,j:integer):integer;
begin
Result:=i+j;
end;
end.
好,现在开始建测试工程
不要窗口,单元名为Test_Sum
要说明的是,如果被测试的工程和测试工程不在一个文件夹下,要在工具->环境选项->库->库路径 中添加被测试工程的路径,要不然会提示找不到
代码如下
unit Test_Sum;
interface
uses
TestFrameWork,TestExtensions,Form_Main_Unit,SysUtils;
//加载测试框架,,要测试的类,类型转换用
type
TTestCaseFirst=class(TTestCase) //建立测试用例
private
TestForm:TForm_Main; //建立要测试对象的实例TestForm??
protected
procedure SetUp;override; //初始化
procedure TearDown;override; //清除数据
published
procedure Testinitial; //测试初始状态
procedure TestSUM_Function; //测试SUM_Function函数方法
procedure TestSUM_ButtonClick; //测试SUM_ButtonClick方法
end;
implementation
procedure TTestCaseFirst.SetUp;//
begin
TestForm:=TForm_Main.Create(Nil); //初始化TestForm
end;
procedure TTestCaseFirst.TearDown;//
begin
TestForm.Destroy;// 销毁TestForm
end;
procedure TTestCaseFirst.Testinitial;//测试初始状态
begin
CheckEquals(TestForm.number_Edit1.Text,'','number_Edit1'); //测试number_Edit1中是否为空
CheckEquals(TestForm.number_Edit2.Text,'','number_Edit2'); //测试number_Edit2中是否为空
CheckEquals(TestForm.SUM_Edit.Text,'','number_Edit3'); //测试SUM_Edit中是否为空
end;
procedure TTestCaseFirst.TestSUM_Function; //测试SUM_Function函数
begin
Check(TestForm.SUM_Function(1,2)=3,
'SUM_Function 1+2=3');
//1+2=3
end;
procedure TTestCaseFirst.TestSUM_ButtonClick; //测试SUM_ButtonClick函数
begin
TestForm.number_Edit1.Text:='1'; //在number_Edit1中填入数
TestForm.number_Edit2.Text:='2'; //在number_Edit2中填入数
TestForm.SUM_ButtonClick(TestForm); //执行SUM_ButtonClick函数
Check(TestForm.SUM_Edit.Text=
IntToStr(TestForm.SUM_Function(1,2)),
'SUM_ButtonClick=IntToStr(TestForm.SUM_Function)');
//判断SUM_Edit中的值是否和用SUM_Function得出的值经过类型转换后得出的值一样
CheckEquals(TestForm.SUM_Edit.Text,
'3',
'判断SUM_Edit中的值是否为字符3');
//判断SUM_Edit中的值是否为字符'3'
CheckNotEquals(TestForm.number_Edit1.Text,'','number_Edit1'); //测试number_Edit1中是否有值
CheckNotEquals(TestForm.number_Edit2.Text,'','number_Edit2'); //测试number_Edit2中是否有值
CheckNotEquals(TestForm.SUM_Edit.Text,'','number_Edit3'); //测试SUM_Edit中是否有值
end;
initialization
TestFramework.RegisterTest(TTestCaseFirst.Suite);
end.
将文件的代码的uses中添加
TestFrameWork,
GUITestRunner,
在begin中把Application.Run;注释掉,添加
GUITestRunner.RunRegisteredTests;
运行,如下
--------------------------------------------------------
知识:
与JUnit、NUnit不同,DUnit中没有提供Assert类,相关的方法都被定义到了TTestCase类中,由于测试类都要继承TTestCase,因此在测试类中可以直接调用这些方法。方法有16个:
1.Check()方法,最常用的方法,用来查看表达式是否为true,为true则测试成功,反之失败。
2.CheckTrue()和 CheckFalse()方法,用来查看变量是否为false或true,如果CheckFalse ()查看的变量的值是false则测试成功,如果是true则失败,CheckTrue ()与之相反。
3.CheckEquals()和CheckNotEquals()方法,用来查看两个对象的值是否相等或不等。
4.CheckEqualsBin()、CheckNotEqualsBin()方法,用来比较两个无符号32位整数,并在输出信息时以二进制格式输出数值。
5.CheckEqualsHex()和CheckNotEqualsHex()方法,用来比较两个无符号32位整数,并在输出信息时以十六进制格式输出数值。
6.CheckEqualsMem()和CheckNotEqualsMem()方法,按指定范围Length比较两个指针所指向的内存中所保存的内容是否相同或不同,可以参考Delphi中的CompareMem函数。
7.CheckEqualsString()和CheckNotEqualsString()方法,比较两个字符串是否相同。
8.CheckEqualsWideString()和CheckNotEqualsWideString()方法,比较两个字符串是否相同,该字符串以Unicode格式编码。
9.CheckNull()和CheckNotNull()方法,查看对象是否为空和不为空。
10.CheckSame()方法,用来比较两个对象是否指向同一内存地址。
11.CheckException()方法,用来查看指定测试方法返回的异常与指定的异常类型是否相同。
12.CheckInherits()方法,查看指定的前一个类型是否是后一个类型的基类。例如:
CheckInherits(TObject, TTestCase);
13.CheckIs()方法,查看对象的类型是否兼容于指定类型,例如TObject是VCL中所有类型的基类,因此所有类型的对象都兼容于TObject。
14.CheckMethodIsNotEmpty()方法,查看方法是否非空,即方法中是否有代码。
15.Fail()方法,意为失败,用来抛出错误。我个人认为有两个用途:首先是在测试驱动开发中,由于测试用例都是在被测试的类之前编写,而写成时又不清楚其正确与否,此时就可以使用Fail方法抛出错误进行模拟;其次是抛出意外的错误,比如要测试的内容是从数据库中读取的数据是否正确,而导致错误的原因却是数据库连接失败。
16.FailEquals()、FailNotEquals()和FailNotSame()方法,功能与Fail方法一样,但带格式,很多Check()方法都直接使用了这些方法。