技术开发 频道

详解Python在数据库测试中的应用

  位于测例与支持库之间的是一个Testloader程序,这个程序完成这几件事情:

  1、 读取根据要执行的测试的配置文件,确定解释器的位置和将要使用哪种驱动;

  2、 读取测试用例的内容,并将对支持类的引用改为对应驱动,比如上面的脚本中的:

  from dbop import * 可能会被改为:from dbop_jdbc import *

  3、 调用对应的解释器执行这个测试用例。

  一个比较简单的例子如下:

cfg.ini:
py_path
=D:\"ACTIVE Python"\python.exe
jy_path
=C:\jython2.2.1\dmjython.bat
ny_path
=C:\pythonnet-2.0-alpha2\pythonnet-2.0-alpha2\python2.5-UCS2\python.exe
#driver
=dotnet
driver
=odbc
#driver
=jdbc
Testloader:
import os
import string
import sys
def exeascript(sc,driver):
    read_script(sc)
    replace_lib_include(sc,driver)
    write_temp_script();
    cmd
= get_executer_path()+ " " + "temp.py"
    newfile
= file("temp.py","w")
    newfile.write(all_text);
    newfile.
close()
    os.system(cmd);
    os.remove("
temp.py")
mode
=""
scriptfile
=""
lstfile
=""
arg
= sys.argv[1]
tempdriver
=sys.argv[2]
if string.upper(arg[arg.rfind('.')+1:len(arg)])=="PY":
    mode
="py"
    scriptfile
= arg
elif string.
upper(arg[arg.rfind('.')+1:len(arg)])=="LST":
    mode
="lst"
    lstfile
=arg
jyp
="";
nyp
="";
cyp
="";
ininame
= "cfg.ini"
dataList
=[]
caseList
=[]
gbldriver
="odbc"
readini(ininame)
for line in dataList:
    getini();
if mode=="lst":
#
to loop execute testcase
elif mode
=="py":
exeascript(scriptfile,tempdriver)
else:
    
print "Not support"

 

  借助上面的这个脚本,测试执行人员执行用例只需要用

  D:\py\testloader>testloader.py Testcase1.py jdbc

  这样类似的格式就可以自己指定驱动来执行测试用例了。

  如果有大量的用例,可以将这种命令写成批处理文件来执行。而不需要人工干预。

  从上面的可以看到,对于功能或者单项SQL性能,基于Python的测试用例可以提供平台无关的简单的脚本编写环境,同时由于Python本身是一门完整的脚本语言,测例编写人员自己也可以在测试用例中编写出各种结构的程序,正所谓可繁可简。

0
相关文章