tcl集成测试环境搭建指南.docx
《tcl集成测试环境搭建指南.docx》由会员分享,可在线阅读,更多相关《tcl集成测试环境搭建指南.docx(19页珍藏版)》请在冰点文库上搜索。
tcl集成测试环境搭建指南
tcl集成测试环境搭建指南
修订记录
日期
修订版本
修改章节
修改描述
作者
2006-1-17
1.0
初稿完成
周峰
第一步:
猎取tcl8.3的安装包,安装到c:
\名目下(安装时一定要选取lib库安装)。
第二步:
利用VisualC++建立一个Win32ConsoleApplication工程,工程的名字为CounterTest。
因为被测对象有MFC类,因此该工程应该支持MFC:
第三步:
参照下面两个图,分不创建test.h和test.cpp文件。
这两个文件均为空文件。
第四步:
添加被测试模块的代码
1)把下面的代码拷贝到文件test.cpp中:
2)把下面的代码拷贝到文件test.h中
第五步:
添加tcl扩展指令代码
1)把下面的代码拷贝到文件CounterTest.cpp中:
#include"tcl.h"
#include"test.h"
第六步:
1)定义tcl讲明器:
通过使用TCL内部的数据类型Tcl_Interp定义TCL讲明器全局变量。
在文件CounterTest.cpp中定义tcl讲明器,即全局变量:
Tcl_Interp*MyInterp;
例如:
(红色部分为添加的代码)
//定义讲明器
Tcl_Interp*MyInterp;
2)创建讲明器:
通过使用TCL函数Tcl_CreateInterp()创建讲明器。
在文件CounterTest.cpp中创建tcl讲明器,例如:
(红色部分代码)
int_tmain(intargc,TCHAR*argv[],TCHAR*envp[])
{
intnRetCode=0;
//initializeMFCandprintanderroronfailure
if(!
AfxWinInit(:
:
GetModuleHandle(NULL),NULL,:
:
GetCommandLine(),0))
{
//TODO:
changeerrorcodetosuityourneeds
cerr<<_T("FatalError:
MFCinitializationfailed")<nRetCode=1;
}
else
{
//TODO:
codeyourapplication'sbehaviorhere.
//创建tcl讲明器
MyInterp=Tcl_CreateInterp();
//初始化Tcl讲明器
Tcl_Init(MyInterp);
//创建外部命令COUNTER,该外部命令能够被tcl讲明器识不,同时执行COUNTER命令的时候,直//接调用外部命令函数TclEx_Counter。
Tcl_CreateCommand(MyInterp,"COUNTER",TclEx_Counter,NULL,NULL);
intrCode;
charsScript[255];
//CStringsScript;
while
(1)
{
//通过嵌入集成测试框架的Tcl讲明器MyInterp,运行外部传入的tcl脚本
printf("请输入要执行的TCL脚本文件名:
\n");
scanf("%s",&sScript);
rCode=Tcl_EvalFile(MyInterp,(char*)sScript);
if(TCL_OK!
=rCode)
{
printf("ThereareerrorsinyourTclFile\n");
}
else
{
printf("TestingSucceed!
\n");
}
//CStringstrHello;
//strHello.LoadString(IDS_HELLO);
//cout<<(LPCTSTR)strHello<}
}
returnnRetCode;
}
3)初始化讲明器:
通过使用TCL函数Tcl_Init()初始化讲明器。
在文件CounterTest.cpp中初始化tcl讲明器,例如:
(红色部分代码)
int_tmain(intargc,TCHAR*argv[],TCHAR*envp[])
{
intnRetCode=0;
//initializeMFCandprintanderroronfailure
if(!
AfxWinInit(:
:
GetModuleHandle(NULL),NULL,:
:
GetCommandLine(),0))
{
//TODO:
changeerrorcodetosuityourneeds
cerr<<_T("FatalError:
MFCinitializationfailed")<nRetCode=1;
}
else
{
//TODO:
codeyourapplication'sbehaviorhere.
//创建tcl讲明器
MyInterp=Tcl_CreateInterp();
//初始化Tcl讲明器
Tcl_Init(MyInterp);
//创建外部命令COUNTER,该外部命令能够被tcl讲明器识不,同时执行COUNTER命令的时候,直//接调用外部命令函数TclEx_Counter。
Tcl_CreateCommand(MyInterp,"COUNTER",TclEx_Counter,NULL,NULL);
intrCode;
charsScript[255];
//CStringsScript;
while
(1)
{
//通过嵌入集成测试框架的Tcl讲明器MyInterp,运行外部传入的tcl脚本
printf("请输入要执行的TCL脚本文件名:
\n");
scanf("%s",&sScript);
rCode=Tcl_EvalFile(MyInterp,(char*)sScript);
if(TCL_OK!
=rCode)
{
printf("ThereareerrorsinyourTclFile\n");
}
else
{
printf("TestingSucceed!
\n");
}
//CStringstrHello;
//strHello.LoadString(IDS_HELLO);
//cout<<(LPCTSTR)strHello<}
}
returnnRetCode;
}
4)向讲明器注册扩展指令:
通过使用TCL函数Tcl_CreateCommand()向讲明器注册自定义扩展指令。
在文件CounterTest.cpp中注册扩展指令,例如:
(红色部分代码)
int_tmain(intargc,TCHAR*argv[],TCHAR*envp[])
{
intnRetCode=0;
//initializeMFCandprintanderroronfailure
if(!
AfxWinInit(:
:
GetModuleHandle(NULL),NULL,:
:
GetCommandLine(),0))
{
//TODO:
changeerrorcodetosuityourneeds
cerr<<_T("FatalError:
MFCinitializationfailed")<nRetCode=1;
}
else
{
//TODO:
codeyourapplication'sbehaviorhere.
//创建tcl讲明器
MyInterp=Tcl_CreateInterp();
//初始化Tcl讲明器
Tcl_Init(MyInterp);
//注册扩展指令COUNTER,该扩展指令能够被tcl讲明器识不,同时执行COUNTER的时候,直截了当调用扩展指令对应处理函数TclEx_Counter。
Tcl_CreateCommand(MyInterp,"COUNTER",TclEx_Counter,NULL,NULL);
intrCode;
charsScript[255];
//CStringsScript;
while
(1)
{
//通过嵌入集成测试框架的Tcl讲明器MyInterp,运行外部传入的tcl脚本
printf("请输入要执行的TCL脚本文件名:
\n");
scanf("%s",&sScript);
rCode=Tcl_EvalFile(MyInterp,(char*)sScript);
if(TCL_OK!
=rCode)
{
printf("ThereareerrorsinyourTclFile\n");
}
else
{
printf("TestingSucceed!
\n");
}
//CStringstrHello;
//strHello.LoadString(IDS_HELLO);
//cout<<(LPCTSTR)strHello<}
}
returnnRetCode;
}
5)定义扩展指令COUNTER对应的处理函数TclEx_Counter()
#include"stdafx.h"
#include"CounterTest.h"
#include"tcl.h"
#include"test.h"
intTclEx_Counter(ClientDataclientData,Tcl_Interp*interp,intargc,char*argv[])
{
returnTCL_OK;
}
6)执行外部传入的TCL脚本文件:
通过使用TCL函数Tcl_EvalFile()执行外部传入的TCL脚本文件。
在文件CounterTest.cpp中添加执行外部脚本文件的代码,例如:
(红色部分代码)
int_tmain(intargc,TCHAR*argv[],TCHAR*envp[])
{
intnRetCode=0;
//initializeMFCandprintanderroronfailure
if(!
AfxWinInit(:
:
GetModuleHandle(NULL),NULL,:
:
GetCommandLine(),0))
{
//TODO:
changeerrorcodetosuityourneeds
cerr<<_T("FatalError:
MFCinitializationfailed")<nRetCode=1;
}
else
{
//TODO:
codeyourapplication'sbehaviorhere.
//创建tcl讲明器
MyInterp=Tcl_CreateInterp();
//初始化Tcl讲明器
Tcl_Init(MyInterp);
//创建外部命令COUNTER,该外部命令能够被tcl讲明器识不,同时执行COUNTER命令的时候,直//接调用外部命令函数TclEx_Counter。
Tcl_CreateCommand(MyInterp,"COUNTER",TclEx_Counter,NULL,NULL);
intrCode;
charsScript[255];
//CStringsScript;
while
(1)
{
//通过嵌入集成测试框架的Tcl讲明器MyInterp,运行外部传入的tcl脚本
printf("请输入要执行的TCL脚本文件名:
\n");
scanf("%s",&sScript);
rCode=Tcl_EvalFile(MyInterp,(char*)sScript);
if(TCL_OK!
=rCode)
{
printf("ThereareerrorsinyourTclFile\n");
}
else
{
printf("TestingSucceed!
\n");
}
//CStringstrHello;
//strHello.LoadString(IDS_HELLO);
//cout<<(LPCTSTR)strHello<}
}
returnnRetCode;
}
第七步:
添加tcl头文件和库文件,并设置相应的头文件和库文件路径
1)如下图所示,采纳如下方法添加tcl83包中的tcl.h和tcl83.lib
2)在[tool…]->[Option…]->[Directory….]菜单中,设置tcl83包的头文件路径:
3)在[tool…]->[Option…]->[Directory….]菜单中,设置tcl83包的库文件路径:
第九步:
实现扩展指令。
参考代码:
intTclEx_Counter(ClientDataclientData,Tcl_Interp*interp,intargc,char*argv[])
{
//定义实际输出
intaRet;//实际输出空行行数
intbRet;//实际输出注释行行数
intcRet;//实际输出代码行行数
intdRet;//实际输出总行行数
//定义期望输出
intaExceptedRet;//期望输出空行行数
intbExceptedRet;//期望输出注释行行数
intcExceptedRet;//期望输出代码行行数
intdExceptedRet;//期望输出总行行数
//定义被测对象
CTestMytest;
//第一步:
检查参数个数
if(10!
=argc)
{
interp->result="Errorparanumber!
";
returnTCL_OK;
}
//第二步:
取出参数
Mytest.g_szStatFileName=argv[1];//取文件路径
//str=argv[1];
//取统计空行标志位
if(TCL_OK!
=Tcl_GetInt(interp,argv[2],&g_bStatBlankLineFlag))
{
interp->result="para2error";
returnTCL_OK;
}
//取统计注释行标志位
if(TCL_OK!
=Tcl_GetInt(interp,argv[3],&g_bStatCommLineFlag))
{
interp->result="para3error";
returnTCL_OK;
}
//取统计代码行标志位
if(TCL_OK!
=Tcl_GetInt(interp,argv[4],&g_bStatCodeLineFlag))
{
interp->result="para4error";
returnTCL_OK;
}
//取统计总行行标志位
if(TCL_OK!
=Tcl_GetInt(interp,argv[5],&g_bStatTotalLineFlag))
{
interp->result="para5error";
returnTCL_OK;
}
//取期望输出空行行数
if(TCL_OK!
=Tcl_GetInt(interp,argv[6],&aExceptedRet))
{
interp->result="para6error";
returnTCL_OK;
}
//取期望输出注释行行数
if(TCL_OK!
=Tcl_GetInt(interp,argv[7],&bExceptedRet))
{
interp->result="para7error";
returnTCL_OK;
}
//取期望输出代码行行数
if(TCL_OK!
=Tcl_GetInt(interp,argv[8],&cExceptedRet))
{
interp->result="para8error";
returnTCL_OK;
}
//取期望输出总行行数
if(TCL_OK!
=Tcl_GetInt(interp,argv[9],&dExceptedRet))
{
interp->result="para9error";
returnTCL_OK;
}
//第三步:
调用被测函数
Mytest.MainStatFun();
aRet=g_iBlankLineNum;
bRet=g_iCommLineNum;
cRet=g_iCodeLineNum;
dRet=g_iTotalLineNum;
//第四步:
测试结果比较
if((aRet!
=aExceptedRet)||(bRet!
=bExceptedRet)
||(cRet!
=cExceptedRet)||(dRet!
=dExceptedRet))
{
interp->result="TestFailed!
";
}
else
{
interp->result="TestSucceded!
";
}
returnTCL_OK;
}