接口测试困难

一、断言

1.数据结构判断(将值的内容忽略)通过字典键值、列表长度

2.判断预期与响应完全一致(列表按顺序或没有列表的情况)

def assert_equal(self,expect_value,response,*args):
    # 响应结果中的列表顺序必须与预期结果的顺序完全一致,否则会断言失败
    global remove_args
    remove_args = [arg for arg in args]
    if isinstance(expect_value,(list,tuple)):
        assert isinstance(response,(list,tuple)),"%s 不是列表,请核查!" %response
        assert len(expect_value) == len(response)
        if len(expect_value) != 0:
            for exp,res in zip(expect_value,response):
                self.assert_equal(exp,res,*args)
    elif isinstance(expect_value,dict):
        assert isinstance(response,dict),"%s 不是字典,请核查!" %response
        for k in expect_value.keys():
            assert k in response.keys()
            if k in remove_args :
                continue
            else:
                # assert k in response.keys()
                self.assert_equal(expect_value[k],response[k],*args)
    elif isinstance(expect_value,(int,bool,str)):
        assert expect_value == response,"%s 不等于 %s" %(expect_value,response)

3.判断响应数据是否包含关键字

def assert_contain(self,expect_value,response,*args):
        ‘‘‘ bool型无法转换并判断是否包含‘‘‘
        global remove_args
        remove_args = [arg for arg in args]  # 取消断言的key
        if isinstance(expect_value,(list,tuple)):
            for i in expect_value :
                self.assert_contain(i,response,*args)
        elif isinstance(expect_value,dict) :
            for k,v in expect_value.items():
                if k in remove_args :
                    continue
                else:
                    self.assert_contain(v,response,*args)
        elif isinstance(expect_value,(int,bool)) :
            self.assert_contain(str(expect_value),response,*args)
        elif isinstance(expect_value,str) :
            print(expect_value)
            assert expect_value in json.dumps(response,ensure_ascii=False),"%s 不在响应结果中,请核查!" %expect_value

4.判断结果不包含关键字

def assert_not_contain(self,expect,reponse):
        assert expect not in json.dumps(reponse,ensure_ascii=False),"%s 在响应结果中,请核查!" %expect

5.下载文件与预期结果对比

二、遇到的困难

1.接口文档在用与废弃未及时更新

2.代码复制,未修改注释,导致接口说明不清晰

相关推荐