python实现接口自动化_python 实现接口自动化1
#coding="utf-8"
importxlrdfrom xlutils.copy importcopyimportrequestsfrom util.log importloggerfrom util.db_util importDBUtil#xlrd:讀取Excel文件數(shù)據(jù)#xlwt:寫(xiě)入Excel 數(shù)據(jù),缺點(diǎn)是無(wú)法復(fù)用,寫(xiě)入會(huì)全部覆蓋,無(wú)法追加數(shù)據(jù),為了方便用戶,寫(xiě)入的話,比較推薦xlutils模塊,它可以復(fù)制原excel#formatting_info=True,保留Excel的原格式,這樣xlutils寫(xiě)入后格式不變#xlrd模塊0.8版本后不支持以xlsx為后綴名文件,所以excel要用xls格式,不能會(huì)打不開(kāi)
if __name__ == '__main__':
db=DBUtil()
db.get_con()
dir_path=r'E:\PyCharmWorkSpace\AutoInterfaceTest\testFile\test_interface005.xls'run_sheet_name="用例"summary_sheet_name="總結(jié)"
#統(tǒng)計(jì)成功數(shù),失敗數(shù)
all_cases =0
statistics_success=0
statistics_fail=0#前置標(biāo)志
pre_flag =Falsetry:#目錄加r可以取消轉(zhuǎn)義,不加r的話\改為\\即可
workbook=xlrd.open_workbook(dir_path,formatting_info=True)exceptFileNotFoundError:print ("File is not found.") #文件不存在
exceptPermissionError:print ( "You don't have permission to access this file.") #文件存在無(wú)權(quán)限訪問(wèn),例如文件被打開(kāi)時(shí)無(wú)法寫(xiě)入
else:
table= workbook.sheet_by_name(run_sheet_name) #根據(jù)sheet名字獲取sheet
new_workbook= copy(workbook) #復(fù)制文件,這樣將結(jié)果寫(xiě)入excel
writeSheet = new_workbook.get_sheet(run_sheet_name) #獲取寫(xiě)入用例sheet
writeSheet_summary=new_workbook.get_sheet(summary_sheet_name)#獲取寫(xiě)入總結(jié)sheet
for i in range(1, table.nrows):
request_method= table.cell(i, 2).value
url= table.cell(i, 3).value
params= table.cell(i, 4).value
expected_results=table.cell(i,5).value
db_operations= table.cell(i, 9).value
sql= table.cell(i, 10).value
pre_request_method=table.cell(i, 12).value
pre_url= table.cell(i, 13).value
pre_params= table.cell(i, 14).valueif( request_method!="" and url!="" and params!="" and expected_results!=""):
logger.info("********************************************************************************************************************")
all_cases+=1
if(pre_request_method!="" and pre_url!=""and pre_params!=""):
logger.info("第" + str(i) + "個(gè)用例前置請(qǐng)求 url:" + pre_url + "請(qǐng)求參數(shù):" +pre_params)if (pre_request_method == "get"):try:
res= requests.get(url=pre_url, params=pre_params)exceptException as result:
logger.info("第" + str(i) + "個(gè)用例前置請(qǐng)求異常:" +str(result))else:
logger.info("第" + str(i) + "個(gè)用例前置請(qǐng)求結(jié)果:" +res.text)
res_json= res.json() #將返回參數(shù)轉(zhuǎn)為json串,取某字段值,result_json[父元素1][子元素2]
pre_response_token = res_json["data"]["token"]
headers= {'Authorization-Qkids': pre_response_token}
pre_flag=Trueelif (pre_request_method == "post"):try:
res= requests.post(url=pre_url, data=pre_params.encode())exceptException as result:
logger.info("第" + str(i) + "個(gè)用例前置請(qǐng)求異常:" +str(result))else:
logger.info("第" + str(i) + "個(gè)用例前置請(qǐng)求結(jié)果:" +res.text)
res_json= res.json() #將返回參數(shù)轉(zhuǎn)為json串,取某字段值,result_json[父元素1][子元素2]
pre_response_token = res_json["data"]["token"]
headers= {'Authorization-Qkids':pre_response_token}
pre_flag=True
logger.info(pre_response_token)else:
logger.info("前置請(qǐng)求方式格式不正確")
headers= {'Authorization-Qkids': pre_response_token}elif(pre_request_method=="" and pre_url==""and pre_params==""):#不做任何操作
pass
else:
logger.info("第" + str(i) + "個(gè)用例:前置請(qǐng)求方式/前置url/前置請(qǐng)求參數(shù)未填")#前置請(qǐng)求就算報(bào)錯(cuò),第二個(gè)請(qǐng)求也會(huì)有相應(yīng)錯(cuò)誤提示,所以下面這段代碼不用放到前置后面,同級(jí)即可
logger.info("第" + str(i) + "個(gè)用例請(qǐng)求 url:" + url + "請(qǐng)求參數(shù):" +params)if (request_method == "get"):try:if(pre_flag==True):
res= requests.get(url=url, params=params,headers=headers)else:
res= requests.get(url=url, params=params)exceptException as result:
statistics_fail+= 1logger.info("第" + str(i) + "個(gè)用例異常:" +str(result))
writeSheet.write(i,8, "N")else:
logger.info("第" + str(i) + "個(gè)用例結(jié)果:" +res.text)
writeSheet.write(i,6, res.text) #寫(xiě)入整個(gè)返回結(jié)果
res_json = res.json() #將返回參數(shù)轉(zhuǎn)為json串,取某字段值,result_json[父元素1][子元素2]
response_message = res_json["message"]
writeSheet.write(i,7, response_message) #寫(xiě)入返回的message
if (expected_results ==response_message):
logger.info("結(jié)果比對(duì):Y")
writeSheet.write(i,8, "Y")
statistics_success+= 1
else:
logger.info("結(jié)果比對(duì):N")
writeSheet.write(i,8, "N")
statistics_fail+= 1
elif (request_method == "post"):try:if(pre_flag==True):#假如請(qǐng)求body里面有漢字,需對(duì)data進(jìn)行encode(),僅用于post請(qǐng)求
res = requests.post(url=url, data=params.encode(),headers=headers)else:
res= requests.post(url=url, data=params.encode())exceptException as result:
statistics_fail+= 1logger.info("第" + str(i) + "個(gè)用例異常:" +str(result))
writeSheet.write(i,8, "N")else:
logger.info("第" + str(i) + "個(gè)用例結(jié)果:" +res.text)
writeSheet.write(i,6, res.text) #寫(xiě)入
res_json = res.json() #將返回參數(shù)轉(zhuǎn)為json串,取某字段值方式:result_json[父元素1][子元素2],例如res_json[data][name]
response_message = res_json["message"]
writeSheet.write(i,7, response_message)if (expected_results == response_message): #比對(duì)預(yù)期結(jié)果與返回結(jié)果
logger.info("結(jié)果比對(duì):Y")
writeSheet.write(i,8, "Y")
statistics_success+= 1
else:
logger.info("結(jié)果比對(duì):N")
writeSheet.write(i,8, "N")
statistics_fail+= 1
else:
logger.info("請(qǐng)求方式格式不正確")if (db_operations != "" and sql != ""):if (db_operations in (1, 2, 3, 4)):
db_result=db.other_data(db_operations, sql)
writeSheet.write(i,11, db_result)else:
logger.info("數(shù)據(jù)庫(kù)操作填寫(xiě)不符合規(guī)則")elif (db_operations == "" and sql == ""):pass
else:
logger.info("數(shù)據(jù)庫(kù)操作/數(shù)據(jù)庫(kù)sql未填")elif (request_method == "" and url == "" and params == "" and expected_results == ""):#不做任何操作
pass
else:
logger.info("第" + str(i) + "個(gè)用例:請(qǐng)求方式/url/請(qǐng)求參數(shù)/期望結(jié)果未填")#%轉(zhuǎn)義方式:%%,其他使用\
logger.info("---------------------------------------------------------------------------------------------------------------------------------------------")
summary_str="總的請(qǐng)求用例數(shù)%d, 已通過(guò)%d,不通過(guò)%d, 通過(guò)比例%.2f%%"%(all_cases,statistics_success,statistics_fail,(statistics_success/all_cases)*100)
logger.info(summary_str)
logger.info("---------------------------------------------------------------------------------------------------------------------------------------------")
writeSheet_summary.write(0,1,summary_str)
new_workbook.save(dir_path)#最后將寫(xiě)的保存
db.close_database()
總結(jié)
以上是生活随笔為你收集整理的python实现接口自动化_python 实现接口自动化1的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: labelme实例分割_MaskRCNN
- 下一篇: 三字经带拼音a4打印版_人教版八年级下册