httprunner3源码解读(5) response.py

源码目录结构


 

get_uniform_comparator

作用:将比较器别名转换为统一名称

    if comparator in ["eq", "equals", "equal"]:
        return "equal"
    elif comparator in ["lt", "less_than"]:
        return "less_than"
    elif comparator in ["le", "less_or_equals"]:
        return "less_or_equals"
    elif comparator in ["gt", "greater_than"]:
        return "greater_than"
    elif comparator in ["ge", "greater_or_equals"]:
        return "greater_or_equals"
    elif comparator in ["ne", "not_equal"]:
        return "not_equal"
    elif comparator in ["str_eq", "string_equals"]:
        return "string_equals"
    elif comparator in ["len_eq", "length_equal"]:
        return "length_equal"
    elif comparator in [
        "len_gt",
        "length_greater_than",
    ]:
        return "length_greater_than"
    elif comparator in [
        "len_ge",
        "length_greater_or_equals",
    ]:
        return "length_greater_or_equals"
    elif comparator in ["len_lt", "length_less_than"]:
        return "length_less_than"
    elif comparator in [
        "len_le",
        "length_less_or_equals",
    ]:
        return "length_less_or_equals"
    else:
        return comparator

 

uniform_validator

作用:统一验证器

由于3和2的验证格式不一致,所以写了这个函数来兼容旧版本的格式

  • 旧版本格式
{"check": "status_code", "comparator": "eq", "expect": 201}
{"check": "$resp_body_success", "comparator": "eq", "expect": True}
  • 新版本格式
{'eq': ['status_code', 201]}
{'eq': ['$resp_body_success', True]}

最后返回的格式为

{
        "check": check_item,  # 想断言的字段,e.g. stats_code
        "expect": expect_value,  # 期望值
        "assert": assert_method,  # 断言的方法,e.g. eq
        "message": message,  # 默认是空字符串
}

 

ResponseObject

作用:初始化response.Response对象,提取报文字段,校验断言

posted @ 2021-11-15 17:14  Silent丿丶黑羽  阅读(118)  评论(0编辑  收藏  举报