Tcl单元测试环境搭建指南

上一篇 / 下一篇  2008-04-28 14:17:37

 
H5B5t,sDC#[r0第一步 :51Testing软件测试网;kh\l FM M
获取 tcl8.3 的安装包,安装到 c:\ 目录下,注意:在安装的过程中需要选择头文件和库文件。
"?N{ X k1l F0 51Testing软件测试网"_k2@kFl0V.f"Fu
第二步 :
3D t!Y4R2s&^0利用 Visual C++ 建立一个基于对话框的工程,工程的名字为 utsample
5s(q%|7V:hoo-? V0 51Testing软件测试网+p~pfMk
第一步:
.q%kf v4p_ q0获取tcl8.3的安装包,安装到c:\目录下,注意:在安装的过程中需要选择头文件和库文件。51Testing软件测试网4_8~&@]:Cc o-QG8x7q
 
1{.k2Mkr,T Y0第二步:
v#kB*F:Pgz0利用Visual C++建立一个基于对话框的工程,工程的名字为utsample
yUkI|051Testing软件测试网uS qk1~W
 51Testing软件测试网5YdeL7PH*sj
51Testing软件测试网H]6zo.|#`*|3pcW
 
Y'pr)@Ue`0第三步:
c*B0I N{]R0参照下图,创建utsample.h文件.51Testing软件测试网"Q`Z _Bs3^Ds-Z1p

%BS d1IX"^A-m0 
g/E~.g^g0 
u1Y}pnV0第四步:51Testing软件测试网X;ug"d_ b\
1)把下面的代码拷贝到utsample.h中51Testing软件测试网 eLx&o/k G
#include "tcl.h"51Testing软件测试网)Z$?_a"f |m\
 51Testing软件测试网;N-oBHO3u*u ]6w
#define RET_ERR false51Testing软件测试网9P!\Lm A N U?*z j:L
#define RET_OK  true
Uz KVFbCU { T0#define MAX_WORD_LEN 3251Testing软件测试网AU'K+QG_E
 
d3s;S t/q QB0int GetWordFromStr(char *pStr,char *pDestStr,int iPos);
5nsk(]Bz0int Tcl_EXGetWord(ClientData clientData,               51Testing软件测试网 _NY-c ?`5?\
          Tcl_Interp * interp,51Testing软件测试网Ys:U h `(f;Zby
          int argc,   char* argv[]);
[^Q7c5L&VV"\ d0 51Testing软件测试网G6G;k }(A!?h E
int  Tcl_AppInit(Tcl_Interp *interp);51Testing软件测试网#dyX r[jw
 51Testing软件测试网-s-c;B1m[1BKK!\O
2)把下面代码拷贝到utsample.cpp中
}'nz;??3].j0#include "stdafx.h"
-|g#Q DGA,MAT*`0#include "utsample.h"
+l2l@:fiA0#include "memory.h"51Testing软件测试网4G ~ g9hJRZ y1_
 51Testing软件测试网%d9xd9H$`
 51Testing软件测试网R\ n)H | F
int main(int argc, char* argv[])51Testing软件测试网AYI6qU { P
{51Testing软件测试网S^\7]iz8Y
    Tcl_Main(argc, argv,Tcl_AppInit);51Testing软件测试网s!?iq+HiNi
    return 0;51Testing软件测试网feq xU u8WW
}
0_7~ s~V i*Mp&|0 
g7pg!cYRS0/*GetWordFromStr函数的扩展指令函数*/51Testing软件测试网,^5g9JeC_b\]0N
int Tcl_EXGetWord(ClientData clientData,               51Testing软件测试网`7Q kC5K
          Tcl_Interp * interp,51Testing软件测试网4Pz[5bv'~d
          int argc,   char* argv[])51Testing软件测试网i'}:_6`Ni c}z
{51Testing软件测试网}r {(a.V7U_$A
    int iPos;51Testing软件测试网C U8aI7ivZ
    bool rCode;
NRJ"a5ek2e0   51Testing软件测试网 A }9UO.@+[B
    char pDestStr[32];51Testing软件测试网"hZ6ho1j/sy
    memset(pDestStr,0,32);51Testing软件测试网k:ze_3u[s
 
0@:KFw8AI%l0    if(3 != argc)
_;[ p/Y F9gp S o ]0    {
s D[(P(Wo6Y0        printf("the paras number is wrong\n");
s r*Y$DH0IcsT0        return TCL_OK;51Testing软件测试网8@/GY#d j#s
    }51Testing软件测试网iS0a+m.P2j8F z5Q
 51Testing软件测试网\aT{-^3TF"b8DB
    if (TCL_OK != Tcl_GetInt(interp,argv[2],&iPos))51Testing软件测试网%X&jXjI[LHBh W
    {51Testing软件测试网$C^CrRx.i
        printf("the para 2 is wrong\n");51Testing软件测试网(?y QZ$j;{ d;q v4e
        return TCL_OK;
-l!M xh J,Cy"A0    }
6mt*A Pp`,d5go0 51Testing软件测试网J~V:~|b { I
    rCode = GetWordFromStr(argv[1],pDestStr,iPos);51Testing软件测试网+B3] \)A+O X n,Ya
 
.M9TrM}Y!G)W0    if(rCode == RET_OK)
-]$IX2jED6b0    {
f RB_&hqRq0        printf("The Destine string is %s\n",pDestStr);
!?#g!e6sqU%p0    }
ECX'G;Q#[-z@0    else51Testing软件测试网T5xb&~|6d0tf
    {
h2z&Jb:eh:D#C0wP0        printf("It's fail to get the string!\n");51Testing软件测试网$q#NuC#|2]m
    }51Testing软件测试网I$P)?nO5EkO
 
3}aLucF^ O+B0    return TCL_OK;
qE Gnr{|"R4j0}
({6a1F4h5e3p6c0 
3]&W2O*E`D0/*tcl解释器和扩展指令的初始化函数*/51Testing软件测试网3|+w~;u"_\yN
int Tcl_AppInit(Tcl_Interp *interp)
,K:v/J F8j#W^0
Zx/v6@ Mr8v p0    /*创建interp解释器可以识别的扩展指令,指令的名字是GetWord,执行该指令,直接调用Tcl_EXGetWord扩展指令函数,通过传递参数,可以执行单元测试用例*/
uh/u!^W-{(R8aR0    Tcl_CreateCommand(interp, "GetWord",51Testing软件测试网L:OQ8g Og p6v }X\
           Tcl_EXGetWord, (ClientData)NULL,
9x+Ly1I k~,I0?0           (Tcl_CmdDeleteProc *)NULL);
S:g:xio zIV5E0    return TCL_OK;
/pm5J,Wx5]+b%Y0}51Testing软件测试网a Kx/pj%`.hl
 51Testing软件测试网8K"y0Y&N+h(_WT
3)把被测试函数GetWordFromStr的函数体拷贝到utsample.cpp中51Testing软件测试网K&l&|#S*Jth
 51Testing软件测试网3Db4YE:ly^
第五步:51Testing软件测试网1Or0H+|3ak2|p j#@
添加tcl头文件和库文件,并设置相应的头文件和库文件路径51Testing软件测试网G5Pi Go9U
1)如下图所示,采用如下方法添加tcl83包中的tcl.h和tcl83.lib51Testing软件测试网|Jfcym+}Y|
51Testing软件测试网 BW;MQu4] h P-_
 
Zl4h zW02)在[tool…]->[Option…]->[Directory….]菜单中,设置tcl83包的头文件路径:
| G%xzN1k0
GM+wba tSpj0 
#` sW"z ~J03)在[tool…]->[Option…]->[Directory….]菜单中,设置tcl83包的库文件路径:51Testing软件测试网8Y5]2[)F3W]

:c6e'IZdLf\0 51Testing软件测试网qac \Y1s:hS!b7z
第六步:
&dc-]/wj&t0编译通过后,运行,出现控制台程序,输入GetWord “wo ai zhong guo” 3
rfLKW.c.K0则出现如下的结果界面
_Z`'Kz ed|n051Testing软件测试网#w2|5t:x(? [7QMra-O%l
 
q]2k cp$M7zJ0备注:51Testing软件测试网sY7G4Z+f,L\K:KP
在嵌入式系统,如果不希望使用上述控制台程序的方式,可以自己构造tcl解释器,然后初始化解释器,然后利用解释器创建扩展指令,并且用自己创建的解释器运行tcl脚本。相关的代码见下面:
_g5Lz_|4w4cZ0 
-S_*N*vm*v0/*定义tcl解释器*/51Testing软件测试网7CS9nZ&na%\G$N
Tcl_Interp* MyInterp;
6]:s%C:rj%l(qtx9y0/*调用tcl的内部函数,创建解释器*/
nCs1c6U%iv {0MyInterp = Tcl_CreateInterp();
Noj+np;n([/d*u6s0/*调用tcl的内部函数,初始化tcl解释器*/51Testing软件测试网Q k(}0H"{n[0i.g
Tcl_Init(MyInterp);51Testing软件测试网 t qc!b6M/B1RR/\z0wV
 51Testing软件测试网P{M:e n'y[7`2d y
//通过那嵌入集成测试框架的Tcl解释器MyInterp,运行tcl脚本E:/test.tcl
K.DH7V7DDc0rCode = Tcl_EvalFile(MyInterp,"E:/test.tcl" );51Testing软件测试网1RuA;b6|ZV~
if  (TCL_OK != rCode )
9P B ?^Q`7IEQ8S0{
Q So8X j W5V'Z0    AfxMessageBox("There are errors in your Tcl File");51Testing软件测试网 YI gr-d$z%qXE8N7?
}51Testing软件测试网_J q*XG{K
 51Testing软件测试网8Q#dg(F X$C]4}
创建扩展指令的方式参见本文前面的描述
p!N0HJ-ee0

5m*V"cG jz y0 
3FZ!Q Ty0V$_/C0
Zm'm1S0G~ z'of*w9YES0 51Testing软件测试网 r1zg ru3S!shC
第三步:51Testing软件测试网(M*I4~T)r\%c)~|1z
参照下图,创建 utsample.h 文件 .51Testing软件测试网!Aim&J Aq6tEh

2ywVnNF5LR#r OC"w0 
Q$\@2jv.qw6N'P0 
lp,LAZ Z0第四步:
Y f+O3I+H8X01 )把下面的代码拷贝到 utsample.h 中
k\y i d#NPu0#include "tcl.h"
Qqyo cg~5pV8G S0 
5~-T Q"oQ'r*t{I0#define RET_ERR false
},g:xd$gt*e'g0#define RET_OK  true
5zA2VJ;J*Vi0#define MAX_WORD_LEN 32
|J6nr1I#bzn0 51Testing软件测试网/p REfXZH
int GetWordFromStr(char *pStr,char *pDestStr,int iPos);51Testing软件测试网;OI%o8j*F$`,A I!_0`P6G
int Tcl_EXGetWord(ClientData clientData,               51Testing软件测试网e#ti$p,] Re#x
          Tcl_Interp * interp,51Testing软件测试网S!_L4Z @
          int argc,   char* argv[]);
;h9A!}QuUBjz0 51Testing软件测试网Ed]JBRpj+~
int  Tcl_AppInit(Tcl_Interp *interp);
E`wB4MP:H0 51Testing软件测试网 K _m,of]
2 )把下面代码拷贝到 utsample.cpp 中51Testing软件测试网 oV$`2IR1W`A
#include "stdafx.h"
5Veh1o n4omZ4f0#include "utsample.h"51Testing软件测试网 h.p8b:X C1Y!wa
#include "memory.h"51Testing软件测试网 ?3H*fbkE-? ]
 51Testing软件测试网*j5Z{/^r&f!j
 51Testing软件测试网Z#eG9NQt7U
int main(int argc, char* argv[])
)Q:H3rn/{|w0{
`Ym Sy9L)D#^)r0    Tcl_Main(argc, argv,Tcl_AppInit);
2v6]b8q1HWm/j g%z0    return 0;
u;FU dt5z6A0}51Testing软件测试网8K5Z Ds.\e0l5I
 
5TadH0T crso/t0/*GetWordFromStr 函数的扩展指令函数 */
v#^mq hOQA0int Tcl_EXGetWord(ClientData clientData,               51Testing软件测试网ytxZA~(_%U
          Tcl_Interp * interp,
!B+b O0I.Ms ]1R `,l0          int argc,   char* argv[])
Z O'j%Rz3}*SK0{51Testing软件测试网+e&T9n?5L
    int iPos;51Testing软件测试网SL'I\ r~;c@t
    bool rCode;51Testing软件测试网6we/a [5]Z
   
T jM*a)l0    char pDestStr[32];
'a G'`!`:vT5KTcn0    memset(pDestStr,0,32);51Testing软件测试网v_jvDmRB6B
 
#N_uf-}6n$r0    if(3 != argc)
Jt8B{2h x0    {
h3X#Pxu!H$r)H0        printf("the paras number is wrong\n");
Fs[G:s~(Z0        return TCL_OK;51Testing软件测试网R0Chd.whGd
    }51Testing软件测试网7E QH:|9TjL2\
 
s`)b7kT!|^he0    if (TCL_OK != Tcl_GetInt(interp,argv[2],&iPos))51Testing软件测试网8l:br2F?S
    {51Testing软件测试网 y(W+M{u{
        printf("the para 2 is wrong\n");51Testing软件测试网+Ry0l4mW}}*k
        return TCL_OK;
,]T+MF!M1\7`0    }
L;Q"eUA2^9u0s0 
R^F)R!Dan5sgI0b0    rCode = GetWordFromStr(argv[1],pDestStr,iPos);51Testing软件测试网/@$~l.]4K,uXP
 51Testing软件测试网o oKWt9vQj2t
    if(rCode == RET_OK)51Testing软件测试网R:v+Q)Wp$v
    {51Testing软件测试网mOG} If1A
        printf("The Destine string is %s\n",pDestStr);51Testing软件测试网*q/OU"^l4p7J2N!GP
    }51Testing软件测试网|4|6pJz9ovzz7}
    else51Testing软件测试网d"sPV;RD1NE
    {51Testing软件测试网,n iov2|6r/]|(r
        printf("It's fail to get the string!\n");
$?pY P(I:s@c5t m2F5}0    }
W.r}F&\w;V6A+l3j0 51Testing软件测试网@#m!Q\fn%[\
    return TCL_OK;
_ RL u'?LiA0}51Testing软件测试网 E3?N rEPt\_2s
 51Testing软件测试网+]rXXlo
/*tcl 解释器和扩展指令的初始化函数 */
\j;J[7wDp0int Tcl_AppInit(Tcl_Interp *interp)
Vpx#g8`0
q,cJ)e&n@0    /* 创建 interp 解释器可以识别的扩展指令,指令的名字是 GetWord ,执行该指令,直接调用 Tcl_EXGetWord 扩展指令函数,通过传递参数,可以执行单元测试用例 */
7Xi n'U/zC0    Tcl_CreateCommand(interp, "GetWord",
ol4g U'R.X"F h.b0           Tcl_EXGetWord,(ClientData)NULL,
'z%e8s:k9X WZ~&j0           (Tcl_CmdDeleteProc *)NULL);
q-b!v1BX(n A0    return TCL_OK;
s-pEJL1De3D0}51Testing软件测试网*t/j D3bOFv]2Npb)a
 
F(D y2B {q'm03 )把被测试函数 GetWordFromStr 的函数体拷贝到 utsample.cpp 中51Testing软件测试网3gO:W4M+^+M Zp,R3}
 51Testing软件测试网j O*S{hqNM-fVA ]O
第五步:51Testing软件测试网0Sd7Vm\ w8[7^M]
添加 tcl 头文件和库文件,并设置相应的头文件和库文件路径51Testing软件测试网CEC$i5s8]/D
1 )如下图所示,采用如下方法添加 tcl83 包中的 tcl.h 和 tcl83.lib51Testing软件测试网.pw6hai)N3q

P"T#iH.rB3x0P2jB0 
B(XORVV B5aWP02 )在 [tool…]->[Option…]->[Directory….] 菜单中,设置 tcl83 包的头文件路径:51Testing软件测试网.~L*Ufs"gO,tn!_

!J[*KP@H.A6Q&~C0 
m#w8J a_x!Ty.q03 )在 [tool…]->[Option…]->[Directory….] 菜单中,设置 tcl83 包的库文件路径:
F'CRw$Lx vy'U0
&N9h2Z2J|0 51Testing软件测试网;t(l+xF)DW
第六步:51Testing软件测试网U(W1^0F| ] Fa
编译通过后,运行,出现控制台程序,输入 GetWord “wo ai zhong guo” 3
%f#r L&^ s `Y3Zh$M/v0则出现如下的结果界面51Testing软件测试网/A dF%@&n*o
51Testing软件测试网6\!eM(bT
 51Testing软件测试网.Zi"@M#]7j
备注:
7I1^gl:X0在嵌入式系统,如果不希望使用上述控制台程序的方式,可以自己构造 tcl 解释器,然后初始化解释器,然后利用解释器创建扩展指令,并且用自己创建的解释器运行 tcl 脚本。相关的代码见下面:51Testing软件测试网v/YQ;G ]U6x
 51Testing软件测试网 y#x7^E+i7Q{
/* 定义 tcl 解释器 */51Testing软件测试网7GB:TTGE
Tcl_Interp* MyInterp;51Testing软件测试网f,TV^&Wk m"{rq6C
/* 调用 tcl 的内部函数,创建解释器 */
R9Ul,j%]b KT Ny0MyInterp = Tcl_CreateInterp();51Testing软件测试网9pB o/J8fw j
/* 调用 tcl 的内部函数,初始化 tcl 解释器 */51Testing软件测试网| A5o7s%P$k [/D+?c*i
Tcl_Init(MyInterp);
Xq0Zp:K0 51Testing软件测试网de,WA#@ o [tv^!_
// 通过那嵌入集成测试框架的 Tcl 解释器 MyInterp ,运行 tcl 脚本 E:/test.tcl51Testing软件测试网zl7lyI
rCode = Tcl_EvalFile(MyInterp,"E:/test.tcl" );51Testing软件测试网8F2Tu#x6D1~6\8aV)y
if  (TCL_OK != rCode )
#w)U3Pg*zk2c/s0{51Testing软件测试网V N5a8v ]UK"k$[y!m
    AfxMessageBox("There are errors in your Tcl File");51Testing软件测试网7Vzw0S*s%P&F8Kn
}

TAG:

引用 删除 zhanghaiyan   /   2009-02-26 23:33:03

写的很好,我一直都记不住整个过程,谢谢!
 

评分:0

我来说两句

Open Toolbar