[转帖] qtp中的数据库、文件操作

上一篇 / 下一篇  2008-04-22 13:43:26 / 个人分类:测试技术

1.datatable

       QTP本身程序就给我们提供了这么一个数据表,我们可以把测试数据或测试用例填入这个数据表中.

如:设计用例

  username  passwd

case1  mercury mercury

case2 xxxxxxx xxxxxx

录制脚本

For i=1 to Datatable.GetRowCount
Dialog("Login").WinEdit("Agent Name:").SetDataTable("username", dtGlobalSheet)
Dialog("Login").WinEdit("Password:").SetDataTable("passwd", dtGlobalSheet)
Dialog("Login").WinButton("OK").Click
datatable.GlobalSheet.SetNextRow
Next

        本例是验证一个登录系统,通过DataTable不同的用例设计,驱动起这段脚本,达到测试的效果.当然上面的例子中还少一个很重要的步骤,那就是结果比较.如果不能进行结果比较的自动化测试不能够称为自动化测试.
        当然我们这里主要讲的是数据驱动,所以不在对上面的例子进行补充.

2.文本文件

        我们可以把文本文件当成数据文件,通过对文本文件的读写操作,来实现数据驱动.

例:文本文件内的内容

  mercury,mercuy

读文件的代码

Function writeorderno(orderno)
Dim fso, myfile,username,passwd
Set fso=CreateObject("scrīpting.FileSystemObject")
Set myfile=fso.openTextFile("C:\testing.txt",1,false)
tmp=split(myfile.readline,",")
username=tmp(0)
passwd=tmp(1)
myfile.close
End Function

写文本文件的代码

Function writeorderno(orderno)
Dim fso, myfile
Set fso=CreateObject("scrīpting.FileSystemObject")
Set myfile=fso.openTextFile("C:\result1.txt",8,false)
myfile.writeline orderno
myfile.close
End Function

3EXCEL文件

        我们可以把EXCEL文件当成数据文件,通过对EXCEL文件的读写操作,来实现数据驱动.

        可以把EXCEL文件当作对象的方式来完成写的操作

Dim Excel,ExcelSheet
Set Excel=CreateObject("Excel.Application")
Set ExcelSheet=CreateObject("Excel.Sheet")
ExcelSheet.Application.visible=true
ExcelSheet.ActiveSheet.Cells(1,1).value=1
ExcelSheet.ActiveSheet.Cells(1,2).value=2
ExcelSheet.ActiveSheet.Cells(1,3).value=3
Excel.Save "C:\test.xls"
Set ExcelSheet=Nothing

        用ADO的方式连接EXCEL文件来做读的操作


Dim conn,input,filename
filename="D:\公基本情况(tb_gsgk)-标准格式.xls" '
Set conn= createobject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source="&filename&";Extended Properties='Excel 8.0;hdr=yes'"
Set input= createobject("ADODB.Recordset")
input.Open "select * from [公基本情况-标准格式$] " ,conn,2,2
input.close
Set input=nothing

4.数据库

        可以利用设计数据表,把测试数据和测试用例放在数据表里,用ADO或者其他任何可以访问连接数据库的方式连接数据库,来实现数据驱动

   Dim res,cmd,sql
Set Res=createobject("adodb.recordset")
Set Cmd=createobject("adodb.command")
Cmd.activeconnection="Provider=SQLOLEDB.1;Password=111111;Persist Security Info=True;User ID=sa;Initial Catalog=xhq;Data Source=192.168.191.142"  '这句话是连接数据库的数据源,要做修改
Cmd.CommandType = 1
sql="selec t * from 表 where name=username"
Cmd.CommandText = sql
Set res = Cmd.Execute()
Set res = nothing
Set cmd.ActiveConnection = nothing
Set Cmd= nothing

以上四种方法都可以帮助我们实现数据驱动,应该说数据驱动在自动化测试中运用的比较的广泛,才有必要拿出来探讨一下.


TAG: 测试技术

 

评分:0

我来说两句

日历

« 2024-04-30  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 4886
  • 日志数: 12
  • 建立时间: 2008-04-14
  • 更新时间: 2008-09-26

RSS订阅

Open Toolbar