Python對介面測試結果斷言

2022-08-29 19:03:12 字數 3132 閱讀 5527

class comparejson:

def compare_json_data(self,expect, actual, errorlist,sort,xpath = '.',*args):

skip = (arg for arg in args)

if isinstance(expect, list) and isinstance(actual, list):

for i in range(len(expect)):

try:

expect = sorted(expect)

except typeerror:

expect = sorted(expect,key=lambda expect:expect[sort])

try:

actual = sorted(actual)

except typeerror:

actual = sorted(actual,key=lambda actual:actual[sort])

try:

self.compare_json_data(expect[i], actual[i], errorlist, sort,xpath + '[%s]' % str(i),*args)

except:

if isinstance(expect, dict) and isinstance(actual, dict):

for i in expect:

if i in skip:

continue

try:

actual[i]

except:

continue

if not (isinstance(expect.get(i), (list, dict)) or isinstance(actual.get(i), (list, dict))):

if type(expect.get(i)) != type(actual.get(i)):

elif expect.get(i) != actual.get(i):

continue

self.compare_json_data(expect.get(i), actual.get(i), errorlist,sort, xpath + '/' + str(i),*args)

return

if type(expect) != type(actual):

elif expect != actual and type(expect) is not list:

return errorlist

def assertequal(self,expect,actual,sort,xpath,*skip):

errorlist =

self.compare_json_data(expect, actual, errorlist,sort,xpath,*skip)

assert len(errorlist) == 0, "\n"+"".join(errorlist)

def assert_equal(self,expect_value,response,sort,*args):

# 響應結果中的列表順序必須與預期結果的順序完全一致,否則會斷言失敗

remove_args = (arg for arg in args)

if isinstance(expect_value,(list,tuple)):

assert isinstance(response,(list,tuple)),"響應結果中的:%s 不是list型別,與預期型別不符,請核查!" %response

assert len(expect_value) == len(response),"響應結果長度:%s 預期長度:%s" %(len(response),len(expect_value))

if len(expect_value) != 0:

try:

expect_value = sorted(expect_value)

except typeerror:

expect_value = sorted(expect_value,key= lambda expect_value:expect_value[sort])

try:

response = sorted(response)

except typeerror:

response = sorted(response,key=lambda response:response[sort])

for exp,res in zip(expect_value,response):

self.assert_equal(exp,res,sort,*args)

elif isinstance(expect_value,dict):

assert isinstance(response,dict),"響應結果中的:%s 不是dict型別,與預期型別不符,請核查!" %response

for k in expect_value.keys():

assert k in response.keys()

if k in remove_args :

continue

else:

self.assert_equal(expect_value[k],response[k],sort,*args)

elif isinstance(expect_value,(int,bool,str)):

assert expect_value == response,"預期結果:%s 不等於 實際結果:%s" %(expect_value,response)

def assert_not_contain(self,expect,reponse):

assert expect not in json.dumps(reponse,ensure_ascii=false),"值:%s 不應該在響應結果中,請核查!" %expect

呼叫:

compare = comparejson()

compare.assertequal(a,b,"id","path:")將斷言結果存於列表中,統一檢視比對結果。如果列表中同時包含(字串、字典)則無法進行排序,並向下比對

compare.assert_equal()遇到斷言失敗則不再繼續斷言

介面測試 apipost介面斷言詳解

在做介面測試的時候,會對介面進行斷言,乙個完整的介面測試,包括 請求 獲取響應正文 斷言。apipost的斷言設定實在後執行指令碼中進行編寫的。apipost本身提供了11中斷言 apt.assert response.raw.responsetext test 測試響應內容是否為test apt....

介面測試斷言詳解 Jmeter

介面測試是目前最主流的自動化測試手段,它向伺服器傳送請求,接收和解析響應結果,通過驗證響應報文是否滿足需求規約來驗證系統邏輯正確性。介面的響應型別通過content type指定,常見的響應型別有 text html html格式 text plain 純文字格式 text xml xml格式 響應...

介面測試 斷言設計思路

檢查響應碼是否符合預期,用來判斷測試用例是否執行成功 針對http介面 驗證關鍵字是否符合預期,用來判斷測試用例是否執行成功 當乙個介面返回的內容較多,並且有一定規律時,可通過正規表示式來校驗介面 返回的資訊來判定測試用例是否執行成功 比如對查詢乙個介面返回的資料進行驗證時,可通過編寫sql語句查詢...