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()遇到断言失败则不再继续断言
posted @ 2020-04-09 16:52  my_testing_road  阅读(2051)  评论(0编辑  收藏  举报