python 介面測試response返回資料對比
背景:之前寫的介面測試一直沒有支援無限巢狀對比key,上次testerhome逛論壇,有人分享了他的框架,看了一下,有些地方不合適我這邊自己修改了一下,部署在jenkins上跑完效果還不錯,拿出來分享一下。ps:還是要多看看別人寫的,新學了不少python自帶的一些常用方法。
這次直接上程式碼,下面寫一下這次我新學一些方法和思路。
def check_response_hope_key(self,response={},hope_response={}):
temp_data={}
for n1 in hope_response:
print "n1:",n1
#如果值是字典型別
if isinstance(hope_response[n1],dict):
print "dict"
if not Check_Response_Hope().check_response_hope_key(response=response.get(n1), hope_response=hope_response[n1]):
MailFile().checkfail(response=response.get(n1), hope_response=hope_response[n1])
return False
raise '{},{}'.format(hope_response[n1],response[n1])
#如果值是列表型別
elif isinstance(hope_response[n1],list):
print "list"
for hope_index,hope_listValue in enumerate(hope_response[n1]):
#print "hope_index:",hope_index
#print "hope_listValue:",hope_listValue
for response_index,response_listValue in enumerate(response[n1]):
#print "response_index:",response_index
#print "response_listValue:",response_listValue
if isinstance(hope_listValue,dict):
Check_Response_Hope().check_response_hope_key(response=response[n1][response_index],
hope_response=hope_response[n1][response_index])
elif isinstance(hope_listValue,list):
if hope_response[n1][hope_index]==response[n1][hope_index]:
break
else:
MailFile().checkfail(response=response_listValue,hope=hope_listValue)
raise Exception ("hope_response="+str(hope_response[n1][hope_index])+"\n"+
"response="+str(response[n1][response_index]))
else:
if hope_response[n1][hope_index]==response[n1][hope_index]:
break
else:
MailFile().checkfail(response=response[n1][hope_index],hope=hope_response[n1][hope_index])
raise Exception ("hope_response="+str(hope_listValue)+"\n"+"response="+str(response_listValue))
else:
print "string"
if response.has_key(n1):
continue
else:
temp_data['error_data']='{}:{},{}:{}'.format(n1,hope_response[n1],n1,response[n1])
#傳送郵件
MailFile().checkfail(response=response[n1],hope=hope_response[n1])
raise Exception ("hope_response="+str(hope_response[n1])+"\n"+"response="+str(response.get(n1)))
return True
內建函式enumerate():
傳入list的資料時返回該列表的索引和值,例如:
>>> list1=[1,2,3,4]
>>> for list_index,list_value in enumerate(list1):
... print list_index,list_value
...
0 1
1 2
2 3
3 4
還可以控制索引的起始值開始迭代,例如:>>> for list_index,list_value in enumerate(list1,1):
... print list_index,list_value
...
1 1
2 2
3 3
4 4
內建函式isinstance(object,type):
用於判斷傳入物件是什麼型別,返回布林型別true或false,例如:
>>> isinstance(list1,dict)
False
ps:這個方法真的挺好用的,很基礎可以根據返回的布林型別走不同的if分支。
內建函式format()這個函式作用就是格式化字串,這裡面不是非要用,我用完感覺還是挺方便的,結構也清晰,在下面舉個常用例子。
1.通過位置進行對映:
>>> '{},{}'.format('abc',123)
'abc,123'
>>> '{1}{0}{1}'.format('abc',123)
'123abc123'
2.通過下標>>> list1=['a','b']
>>> '{0[1]},{0[0]}'.format(list1)
'b,a'
當然還其他很多用法,我也沒用到,還是挺強大的,有興趣自己百度一下吧,很多寫的很詳細。思路:
介面返回response一定是字典格式的,因為我寫的介面測試框架用的orm連結資料庫動態從資料庫中傳引數,所以返回value可能會不同,但是返回response的key肯定是固定的,所以我這裡驗證所有的key。
首先遍歷hope_response(期望介面返回),hope_response[n]可能型別字典,列表或者string/int(我目前沒有見過key是int型的),所以使用isinsstance()去判斷value的型別。如果是string就表示是最簡單的一層{key:value}形式,這裡就使用has_key來判斷response中有沒有該key。hope_response[n]是dict型別,就遞迴,最後一定會落到string/int型別的分支。如果hope_response[n]是list型別,就用到enumerate()來拿到索引和值,根據值的型別去判斷。大體思路這樣的,我除錯1天多,看著簡單,自己寫坑還是挺多的。
我這面裡確實借鑑了別人的,我把地址附在下面,大家有興趣的可以去看看。
https://testerhome.com/topics/8840