Python對接口測試結果斷言


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:
                    errorList.append('▇▇ expect中的key %s[%s]未在B中找到\n' % (xpath, i))
        if isinstance(expect, dict) and isinstance(actual, dict):
            for i in expect:
                if i in skip:
                    continue
                try:
                    actual[i]
                except:
                    errorList.append('▇▇ [expect]中的key:%s             %s/%s 未在[actual]中找到\n' % (i,xpath, i))
                    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)):
                        errorList.append('▇▇ 類型不同參數在[expect]中的絕對路徑:  %s/%s  ►►► expect is %s, actual is %s \n' % (xpath, i, type(expect.get(i)), type(actual.get(i))))
                    elif expect.get(i) != actual.get(i):
                        errorList.append('▇▇ 僅內容不同參數在[expect]中的絕對路徑:  %s/%s  ►►► expect is %s, actual is %s \n' % (xpath, i, 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):
            errorList.append('▇▇ 類型不同參數在[expect]中的絕對路徑:  %s  ►►► expect is %s, actual is %s \n' % (xpath, type(expect), type(actual)))
        elif expect != actual and type(expect) is not list:
            errorList.append('▇▇ 僅內容不同參數在[expect]中的絕對路徑:  %s  ►►► expect is %s, actual is %s \n' % (xpath, expect, actual))
        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()遇到斷言失敗則不再繼續斷言


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM