在QTP Test中利用vbs和cmd实现重新启动QTP
关于qtp如何自启动,这个问题之前在iQuickTest上面问过zzxxbb123,不过他给的答案比较概括一点。所以自己就想办法弄的具体了点。以下是之前写的文章,也是最早在51autotest上面放的。链接
http://www.51autotest.com/home.php?mod=space&uid=2535&do=blog&id=145
连续长时间执行过QTP或使用QC连续长时间调用QTP的朋友应该会知道,QTP在长时间执行过程中会产生内存泄露。而QTP自身也有一个工具Remote Agent来实现每执行N次来重新启动测试工具释放内存。
但是,不知道是不是我用破解版的原因,如果使用RunTest来调用QC执行QTP时,这个设置的Remote Agent会使得测试失败,并卡死在某一步
。所以在这里需要进行手动清理QTP长时间运行过程中产生的内存泄露。

在这里,我所知的清理内存有两种方式,一种是利用一些内存清理工具,一种是通过脚本控制QTP重新启动。
首先说说内存清理工具。今天在群里下到了一个兄弟共享的内存清理工具memempty.exe,它可以进行定时清理。但是由于QTP执行每个Test的时间都不相同,并且QTP在执行过程中,这个工具也无法有效的清理干净内存部分。对这些不是很懂,点下就行了,免得出丑。

使用脚本控制,这里我用的是创建外部vbs并通过cmd命令来执行的方式实现的,脚本如下:
dim restartcode
set fso = CreateObject("Scripting.FileSystemObject")
set file = fso.CreateTextFile("D:\RestartQTP.vbs")
restartcode = "'浣跨敤涓€涓瓑寰呮椂闂翠娇寰梣tp鑳借繑鍥瀜c涓€涓墽琛屾垚鍔熺殑缁撴灉"&vbnewline
restartcode = restartcode &"WScript.Sleep 3000"&vbnewline
restartcode = restartcode &"'浣跨敤AOM鏉ユ帶鍒秖tp鐨勯噸鍚搷浣�"&vbnewline
restartcode = restartcode &"Dim qtp"&vbnewline
restartcode = restartcode &"Set qtp = CreateObject(""QuickTest.Application"")"&vbnewline
restartcode = restartcode &"qtp.Quit"&vbnewline
restartcode = restartcode &"WScript.sleep 2000"&vbnewline
restartcode = restartcode &"qtp.Launch"&vbnewline
restartcode = restartcode &"qtp.visible = true"&vbnewline
restartcode = restartcode &"set qtp = nothing"&vbnewline
restartcode = restartcode &"set fso = CreateObject(""Scripting.FileSystemObject"")"&vbnewline
restartcode = restartcode &"fso.deletefile ""D:\RestartQTP.vbs"""&vbnewline
restartcode = restartcode &"set fso = nothing"&vbnewline
file.write restartcode
set fso = nothing
set WshShell = CreateObject("WScript.Shell")
WshShell.run "cmd.exe /c D:\RestartQTP.vbs"
set WshShell = nothing
这样就可以实现重新启动QTP了,不过在执行下一个test之前需要一个等待时间,这个在调用脚本中很简单,就不罗嗦了
set file = fso.CreateTextFile("D:\RestartQTP.vbs")
restartcode = "'浣跨敤涓€涓瓑寰呮椂闂翠娇寰梣tp鑳借繑鍥瀜c涓€涓墽琛屾垚鍔熺殑缁撴灉"&vbnewline
restartcode = restartcode &"WScript.Sleep 3000"&vbnewline
restartcode = restartcode &"'浣跨敤AOM鏉ユ帶鍒秖tp鐨勯噸鍚搷浣�"&vbnewline
restartcode = restartcode &"Dim qtp"&vbnewline
restartcode = restartcode &"Set qtp = CreateObject(""QuickTest.Application"")"&vbnewline
restartcode = restartcode &"qtp.Quit"&vbnewline
restartcode = restartcode &"WScript.sleep 2000"&vbnewline
restartcode = restartcode &"qtp.Launch"&vbnewline
restartcode = restartcode &"qtp.visible = true"&vbnewline
restartcode = restartcode &"set qtp = nothing"&vbnewline
restartcode = restartcode &"set fso = CreateObject(""Scripting.FileSystemObject"")"&vbnewline
restartcode = restartcode &"fso.deletefile ""D:\RestartQTP.vbs"""&vbnewline
restartcode = restartcode &"set fso = nothing"&vbnewline
file.write restartcode
set fso = nothing
set WshShell = CreateObject("WScript.Shell")
WshShell.run "cmd.exe /c D:\RestartQTP.vbs"
set WshShell = nothing
这样就可以实现重新启动QTP了,不过在执行下一个test之前需要一个等待时间,这个在调用脚本中很简单,就不罗嗦了

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类