python 接口自动化测试--代码实现(八)
用例读入数据库:
#! /usr/bin/python # coding:utf-8 import sys,os from Engine import DataEngine reload(sys) sys.setdefaultencoding( "utf-8") from Engine.XlsEngine import XlsEngine_rd from Engine.SqlEngine import MSSQL dir=os.getcwd() #文件的绝对路径 #用例文件,casedir=[[xls文件名列表],[xls文件路径列表]] casedir = DataEngine.getDateCase('.\DataSrc') class ReadCase: def __init__(self,file): self.file=file def readCase(self): data = XlsEngine_rd(self.file) data.xlrd_open() sheets=data.xlrd_object.sheets() result_list=[] for index in range(len(sheets)):#遍历excel文件的sheets sheet = data.xlrd_object.sheet_by_index(index) rows = sheet.nrows sheet_list=[] for i in range(rows): sheet_list.append(sheet.row_values(i)) result_list.append(sheet_list) #单个sheet中的内容列表并入到结果列表中 return result_list class caseToDb(): def __init__(self,host,user,psw,dbname): self.host=host self.user=user self.psw=psw self.dbname=dbname self.con = MSSQL(self.host,self.user,self.psw,self.dbname) def InsertInterDefineSql(self,url,Descn,method,FlatType,ResultType): sql="INSERT INTO[dbo].[InterDefine]([Url],[Descn],[Method],[FlatType],[CreatDate],[EditDate],[VerSion],[IsRun],[ResultType])\ VALUES('%s','%s','%s',%d,GETDATE(),GETDATE(),1,'Y','%s')"%(url,Descn,method,FlatType,ResultType) self.con.ExecNonQuery(sql) def InsertDataCaseSql(self,InterfaceId,CaseId,AC,Data,IsRun,Expect): sql="INSERT INTO [dbo].[DataCase]([InterfaceId],[CaseId],[AC],[DataStr],[IsRun],[Expect],[CreatDate],[EditDate])\ VALUES(%d,%d,'%s','%s','%s','%s',GETDATE(),GETDATE())"%(InterfaceId,CaseId,AC,Data,IsRun,Expect) self.con.ExecNonQuery(sql) def methodId(self,method): sql="SELECT Id FROM dbo.InterDefine WHERE Method = '%s'"%method Id=self.con.ExecQuery(sql) return Id def dataCaseId(self,caseId,interfaceid): sql="SELECT Id FROM dbo.Datacase WHERE caseId = %d and interfaceid = %d"%(caseId,interfaceid) Id=self.con.ExecQuery(sql) return Id def run(): a=caseToDb('192.168.1.2','release','release','tester') try: for xlsindex in range(0,len(casedir[1])): result_list = ReadCase(casedir[1][xlsindex]).readCase() #依次获取用例文件地址 for i in range(len(result_list)):#len(result_list)为文件中的sheet数量 Descn = result_list[i][0][0]#获取单个sheet中的接口描述 URL = result_list[i][2][0] #获取单个sheet中的接口地址 METHOD = result_list[i][2][1]#获取单个sheet中的接口方法 ArgAccount = int(result_list[i][2][2])#获取接口的参数个数 ResultType = result_list[i][2][3]#获取接口的返回结果格式类型 FlatType = result_list[i][2][4]#获取接口所属端 if len(a.methodId(METHOD)) == 0:#如果接口定义表中无此方法,则插入接口定义参数 a.InsertInterDefineSql(URL,Descn,METHOD,FlatType,ResultType) for j in range(5,len(result_list[i])): Data={} CaseId=int(result_list[i][j][0])#获取用例的id Run=str(result_list[i][j][1]) Expect=str(result_list[i][j][2+ArgAccount])#获取预期结果 InterfaceId = int(a.methodId(METHOD)[0][0])#查询用例对应的接口在接口定义表中的ID AC = result_list[i][j][3+ArgAccount]#单条接口用例的检查点描述 if ArgAccount != 0: for value_index in range(2,2+ArgAccount): value = result_list[i][j][value_index] if isinstance(value,(float,int)): Data[result_list[i][4][value_index]]=int(value)#组成请求参数字典 else: Data[result_list[i][4][value_index]]=str(value)#组成请求参数字典 else: Data={} Data=str(Data).replace("'",'"') if len(a.dataCaseId(CaseId,InterfaceId)) == 0:#若用例表中无此用例ID和接口id,则插入 a.InsertDataCaseSql(InterfaceId,CaseId,AC,Data,Run,Expect) except Exception,e: print(e) # if __name__ == '__main__': # run()