zoukankan      html  css  js  c++  java
  • 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"}}
  • 相关阅读:
    Java面试题(3)Java new一个对象的过程中发生了什么
    spring boot(九):Spring Boot中Redis的使用
    intellij idea 2018
    springboot(八)自定义Filter、自定义Property
    springboot(六)SpringBoot问题汇总
    Java Web之路(五)JSP
    Java
    instrument(2)
    Instrumentation(1)
    Dubbo中订阅和通知解析
  • 原文地址:https://www.cnblogs.com/hito/p/11050170.html
Copyright © 2011-2022 走看看