之前用的是java那一套东西,有eclipse什么都搞定了。现在因为要用delphi改一个即时通讯软件的缘故,想着怎么能把重构和单元测试那一套搬到delphi这边来。 书上说给现有的代码加单元测试能够加深对代码的理解,并且可以作为改善代码的基础,这不正是我要做的事情吗?于是,为了搭建这么一个敏捷平台,我以delphi2005和dunit进行了一点小小的尝试,并把结果记录于下,与大家分享。
1、下载delphi2005
因为borland公司在中国不出售专业版本,鉴于架构师和企业版的天价,所以只能从网络上下载一个d版的做个人使用。delphi2005体积比较大,而且update1也需要cd,为避免麻烦我就不直接提供下载地址了,有兴趣的朋友可以用google搜一下,肯定会有所的。
2、下载delphi2005的sp1
据说打过这个补丁之后能快一些,但是这个补丁安装起来可慢了,还需要用光盘。下载地址如下:
http://www.delphifans.com/softview/970.html
3、下载dunit
https://sourceforge.net/projects/dunit/
4、安装delphi2005+sp1
注意一下keygen中有一个register以及安装的时候一定要同时安装.net和win32版本的delphi,c#builder则无所谓。因为如果只安装win32版本会导致重构功能不可用(这是delphi2005的一个bug,表现就是用重构的时候出现.net的异常窗口)。
5、精简delphi2005
因为上一步的时候同时安装了.net版本,导致delphi启动的时候变得非常慢。这个可以通过修改注册表,删掉一些ide的包来实现。具体的做法是在delphi的快捷方式目标后面加上参数-rxxx,也就是bds.exe -rxxx。这个xxx任由你指定,然后你就会在hkey_current_user\software\borland\xxx(这个就是你选定的名字xxx)\3.0\known ide packages中发现那些该死的包。注意不能和网络上别的win32纯化分子那样删得太干净了,那样重构就没法用了。我自己试验多次之后得出的一个比较精简的列表如下 "$(bds)\\bin\\vclmenudesigner90.bpl"="(untitled)"
"$(bds)\\bin\\win32debugproide90.bpl"="(untitled)"
"$(bds)\\bin\\htmide90.bpl"="html designer package"
"$(bds)\\bin\\iteidew3290.bpl"="borland integrated translation environment for win32"
"$(bds)\\bin\\srcmanide90.bpl"="(untitled)"
"$(bds)\\bin\\todoide90.bpl"="borland todo"
"$(bds)\\bin\\htmlhelp290.bpl"="borland htmlhelp viewer"
"$(bds)\\bin\\idefilefilters90.bpl"="ide file filters"
"$(bds)\\bin\\startpageide90.bpl"="borland start page ide package"
"$(bds)\\bin\\refactoride90.bpl"="borland core refactoring package"
"$(bds)\\bin\\dbkdebugide90.bpl"="(untitled)"
"$(bds)\\bin\\exceptiondiag90.bpl"="(untitled)"
"$(bds)\\bin\\deployide90.bpl"="deployment manager"
"$(bds)\\bin\\plugview90.bpl"="pluggable tree view package"
"$(bds)\\bin\\coreproide90.bpl"="core ide pro package"
"$(bds)\\bin\\idetools90.bpl"="build tools"
"$(bds)\\bin\\unittestide90.bpl"="(untitled)"
"$(bds)\\bin\\historyide90.bpl"="(untitled)"
"$(bds)\\bin\\htmltidy90.bpl"="html tidy formatter"
"$(bds)\\bin\\htmlfmt90.bpl"="html internal formatter"
"$(bds)\\bin\\mlcc90.bpl"="markup language code completion package"
"$(bds)\\bin\\delphivclide90.bpl"="delphi vcl designer ide package"
"$(bds)\\bin\\delphicoreproide90.bpl"="(untitled)"
"$(bds)\\bin\\win32debugide90.bpl"="(untitled)"
"$(bds)\\bin\\htmlide90.bpl"="(untitled)"
"$(bds)\\bin\\delphide90.bpl"="delphi win32 ide personality"
"$(bds)\\bin\\mtspro90.bpl"="(untitled)"
"$(bds)\\bin\\mtsent90.bpl"="(untitled)"
"$(bds)\\bin\\iteid.net90.bpl"="borland integrated translation environment for .net"
"$(bds)\\bin\.netimportwiz90.bpl"="(untitled)"
"$(bds)\\bin\\dataexplorer90.bpl"="(untitled)"
虽然启动还是有些慢,不过总算能忍受了。
6、编译并安装dunit
用你知道的办法把dunit编译成dcu,放入你指定的目录。并把该目录添加到delphi的tools->environment options->delphi options->library-win32的library path中。
另外把dunit.exe给编译出来
7、建立工程
我推荐的方式是写delphi程序的时候把界面和后台分开,具体就是开两个项目,一个普通的vcl项目,一个dll项目。不,不,我的意思并不是让你用dll这种方式来共享代码。这个dll项目包含文件是所有后台的内容,以及对后台的测试。产生的dll给dunit用,dunit能够从其中读出测试并运行。而实际上gui部分的那个工程是包含了所有的文件的,也就是说分与其说分两个工程,不如说是在原来的单个工程的基础上加上了一个专门用于测试的dll工程。
项目分为三块:gui,core,testcore。工程一包括gui+core,工程二包括core+testcore。工程一的输出是win32exe程序。工程二输出是win32dll程序。
然后可以把这两个工程放到一个project group中。
8、编写测试
工程一和普通写dephi程序没有什么两样,要记得把gui的功能分出来后台,让后台testable。测试都是写在工程二中的,我把工程二的名字就叫做core。下面就在core中添加一个最基本的测试,检查1+1是不是等于2。
建立testcase unit testbasic;
interface
uses
testframework;
type
tbasic = class
public
function add(a, b: integer): integer;
end;
ttestbasic = class(ttestcase)
private
fbasic: tbasic;
public
procedure setup; override;
procedure teardown; override;
published
procedure testadd;
end;
implementation
function tbasic.add(a, b: integer): integer;
begin
result := a + b;
end;
procedure ttestbasic.setup;
begin
fbasic := tbasic.create;
end;
procedure ttestbasic.teardown;
begin
fbasic.free;
end;
procedure ttestbasic.testadd;
begin
checkequals(2, fbasic.add(1, 1));
end;
initialization
registertest('', ttestbasic.suite);
end.
修改core.bdsproj(kao,什么古怪的后缀啊)
把begin end.改成 exports
registeredtests name 'test';
end.
并在uses部分添加testframework。
dunit就是通过这个export的函数在dll中找出我们注册的testcase的。
9、运行dunit
把dunit添加到tools菜单下吧,那样方便许多。只是参数不好填写。我没有用里面的macro,直接填了绝对路径。如果你和我一样这样直接填的绝对路径,那么从tools菜单下选dunit就能直接看到一个测试列表了,点运行就会看到一个绿灯亮起了。
如果你不是把dunit添加到tools菜单,那么就要从file->load test的文件选择窗口中找到core项目编译出来的dll,core.dll。
至此重构功能和单元测试都有了。剩下的就是给既有代码添加测试,然后慢慢重构的事情了。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器