rf接口自动化之结果校验

接口自动化的结果校验分两个层次:协议层,业务层

(本篇仅对协议层校验做扩展,仅考虑返回结果为json格式的接口)

协议层校验即对返回结果的key是否存在,value类型是否与协议一致进行判断

class CheckResp():
    def check_keys(self,target,template,except_key=""):
        if isinstance(target,(str,unicode)):
            target = json.loads(target)
        if isinstance(template,(str,unicode)):
            template = json.loads(template)
        for key,value in template.items():
            if not target.has_key(key) and key not in except_key:   # 校验断言json的key在返回结果中的是否存在
                raise Exception("key:%s not in the response" % key)
            elif target.has_key(key) and type(value) != type(target[key]):  # 校验返回结果的value类型与断言json的value类型一致
                raise Exception("the value's type of key:%s not match the assert json" % key)
            if isinstance(value,dict):  # 多重嵌套的字典类型递归校验判断
                self.check_keys(target[key],value)

在robot framework中的应用:

  1. 引用代码库
  2. 在接口调用请求后,获取返回结果,赋值给${resp}
  3. 接口返回结果协议层校验关键字使用:Check Keys    ${resp}    {"a":1,"b":{"c":"d"}}

 

posted on 2019-06-19 11:08  涛哥爱吃面  阅读(1599)  评论(0编辑  收藏  举报

导航