欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 接口測試response返回數(shù)據(jù)對比的方法

 更新時間:2018年02月11日 14:26:38   作者:gogoboi_jin  
本篇文章主要介紹了python 接口測試response返回數(shù)據(jù)對比的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

背景:之前寫的接口測試一直沒有支持無限嵌套對比key,上次testerhome逛論壇,有人分享了他的框架,看了一下,有些地方不合適我這邊自己修改了一下,部署在jenkins上跑完效果還不錯,拿出來分享一下。ps:還是要多看看別人寫的,新學(xué)了不少python自帶的一些常用方法。

這次直接上代碼,下面寫一下這次我新學(xué)一些方法和思路。

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])
     #發(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ù)時返回該列表的索引和值,例如:

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

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

用于判斷傳入對象是什么類型,返回布爾類型true或false,例如:

>>> isinstance(list1,dict)
False

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

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

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

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

2.通過下標(biāo)

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

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

思路:

接口返回response一定是字典格式的,因為我寫的接口測試框架用的orm鏈接數(shù)據(jù)庫動態(tài)從數(shù)據(jù)庫中傳參數(shù),所以返回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()來拿到索引和值,根據(jù)值的類型去判斷。大體思路這樣的,我調(diào)試1天多,看著簡單,自己寫坑還是挺多的。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實現(xiàn)的最近最少使用算法

    Python實現(xiàn)的最近最少使用算法

    這篇文章主要介紹了Python實現(xiàn)的最近最少使用算法,涉及節(jié)點、時間、流程控制等相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • Scrapy框架基本命令與settings.py設(shè)置

    Scrapy框架基本命令與settings.py設(shè)置

    這篇文章主要介紹了Scrapy框架基本命令與settings.py設(shè)置,結(jié)合實例形式分析了創(chuàng)建爬蟲項目、創(chuàng)建爬蟲文件、存儲、打開網(wǎng)頁及settings.py設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下
    2020-02-02
  • cmd輸入python命令無反應(yīng)的解決方案

    cmd輸入python命令無反應(yīng)的解決方案

    這篇文章主要介紹了cmd輸入python命令無反應(yīng)的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python中的split、rsplit、splitlines用法說明

    python中的split、rsplit、splitlines用法說明

    這篇文章主要介紹了python中的split、rsplit、splitlines用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • jupyter 使用Pillow包顯示圖像時inline顯示方式

    jupyter 使用Pillow包顯示圖像時inline顯示方式

    這篇文章主要介紹了jupyter 使用Pillow包顯示圖像時inline顯示方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • matplotlib之pyplot模塊實現(xiàn)添加子圖subplot的使用

    matplotlib之pyplot模塊實現(xiàn)添加子圖subplot的使用

    這篇文章主要介紹了matplotlib之pyplot模塊實現(xiàn)添加子圖subplot的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • pytorch教程resnet.py的實現(xiàn)文件源碼分析

    pytorch教程resnet.py的實現(xiàn)文件源碼分析

    torchvision.models這個包中包含alexnet、densenet、inception、resnet、squeezenet、vgg等常用的網(wǎng)絡(luò)結(jié)構(gòu),并且提供了預(yù)訓(xùn)練模型,可以通過簡單調(diào)用來讀取網(wǎng)絡(luò)結(jié)構(gòu)和預(yù)訓(xùn)練模型
    2021-09-09
  • python讀取LMDB中圖像的方法

    python讀取LMDB中圖像的方法

    這篇文章主要為大家詳細(xì)介紹了python讀取LMDB中圖像的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • python中退出多層循環(huán)的方法

    python中退出多層循環(huán)的方法

    這篇文章主要介紹了python中退出多層循環(huán)的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • Python Django模型詳解

    Python Django模型詳解

    這篇文章主要介紹了Django 模型類(models.py)的定義詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-10-10

最新評論