溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python接口測(cè)試response返回?cái)?shù)據(jù)對(duì)比的示例分析

發(fā)布時(shí)間:2021-08-04 09:07:14 來(lái)源:億速云 閱讀:318 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹python接口測(cè)試response返回?cái)?shù)據(jù)對(duì)比的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

代碼如下

def check_response_hope_key(self,response={},hope_response={}):
  temp_data={}
  for n1 in hope_response:
   print "n1:",n1
   #如果值是字典類(lèi)型
   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])
   
   #如果值是列表類(lèi)型
   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])
     #發(fā)送郵件
     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

內(nèi)置函數(shù)enumerate():

傳入list的數(shù)據(jù)時(shí)返回該列表的索引和值,例如:

>>> 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

還可以控制索引的起始值開(kāi)始迭代,例如:

>>> for list_index,list_value in enumerate(list1,1):
...  print list_index,list_value
...

1 1
2 2
3 3
4 4

內(nèi)置函數(shù)isinstance(object,type):

用于判斷傳入對(duì)象是什么類(lèi)型,返回布爾類(lèi)型true或false,例如:

>>> isinstance(list1,dict)
False

ps:這個(gè)方法真的挺好用的,很基礎(chǔ)可以根據(jù)返回的布爾類(lèi)型走不同的if分支。

內(nèi)置函數(shù)format()

這個(gè)函數(shù)作用就是格式化字符串,這里面不是非要用,我用完感覺(jué)還是挺方便的,結(jié)構(gòu)也清晰,在下面舉個(gè)常用例子。
1.通過(guò)位置進(jìn)行映射:

>>> '{},{}'.format('abc',123)
'abc,123'
>>> '{1}{0}{1}'.format('abc',123)
'123abc123'

2.通過(guò)下標(biāo)

>>> list1=['a','b']
>>> '{0[1]},{0[0]}'.format(list1)
'b,a'

當(dāng)然還其他很多用法,我也沒(méi)用到,還是挺強(qiáng)大的,有興趣自己百度一下吧,很多寫(xiě)的很詳細(xì)。

思路:

接口返回response一定是字典格式的,因?yàn)槲覍?xiě)的接口測(cè)試框架用的orm鏈接數(shù)據(jù)庫(kù)動(dòng)態(tài)從數(shù)據(jù)庫(kù)中傳參數(shù),所以返回value可能會(huì)不同,但是返回response的key肯定是固定的,所以我這里驗(yàn)證所有的key。

首先遍歷hope_response(期望接口返回),hope_response[n]可能類(lèi)型字典,列表或者string/int(我目前沒(méi)有見(jiàn)過(guò)key是int型的),所以使用isinsstance()去判斷value的類(lèi)型。如果是string就表示是最簡(jiǎn)單的一層{key:value}形式,這里就使用has_key來(lái)判斷response中有沒(méi)有該key。hope_response[n]是dict類(lèi)型,就遞歸,最后一定會(huì)落到string/int類(lèi)型的分支。如果hope_response[n]是list類(lèi)型,就用到enumerate()來(lái)拿到索引和值,根據(jù)值的類(lèi)型去判斷。大體思路這樣的,我調(diào)試1天多,看著簡(jiǎn)單,自己寫(xiě)坑還是挺多的。

以上是“python接口測(cè)試response返回?cái)?shù)據(jù)對(duì)比的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI