天天看点

E02_HttpRunner断言:断言类型列表

E02_HttpRunner断言:断言类型列表

E02_HttpRunner断言:断言类型列表

在 comparators.py 文件中,定义了可以使用的断言类型:

  1. equals: 是否相等
  2. less_than: 小于
  3. less_than_or_equals: 小于等于
  4. greater_than: 大于
  5. greater_than_or_equals: 大于等于
  6. not_equals: 不等于
  7. string_equals: 字符串相等
  8. length_equals: 长度相等
  9. length_greater_than: 长度大于
  10. length_greater_than_or_equals: 长度大于等于
  11. length_less_than: 长度小于
  12. length_less_than_or_equals: 长度小于等于
  13. contains: 预期结果是否被包含在实际结果中
  14. contained_by: 实际结果是否被包含在预期结果中
  15. type_match: 类型是否匹配
  16. regex_match: 正则表达式是否匹配
  17. startswith: 字符串是否以什么开头
  18. endswith: 字符串是否以什么结尾

以访问百度首页举例(其中断言全部通过):

- config:
    name: TestCase

- test:
    name: TestStep -1
    request: 
      url: https://www.baidu.com/
      method: GET
      headers:
        User-Agent: 'ozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'

    validate:
        # 断言: 是否相等, 返回响应状态码是否为200
        - equals: [status_code, 200]

        # 断言: 是否包含, 响应实体中是否包含 “title” 字符串
        - contains: [content, title]

        # 断言: 是否以指定字符串开头, 响应实体是否以“<!DOCTYPE”字符串开头
        - startswith: [body, <!DOCTYPE]

        # 断言: 是否大于, 响应头域数量是否大于 10 个
        - length_greater_than: [headers, 10]

        # 断言: 是否类型匹配, 响应实体类型是否为“str”字符串类型
        - type_match: [content, str]

           

源码:\httprunner-2.5.5\httprunner\builtin\comparators.py

def equals(check_value, expect_value):
    assert check_value == expect_value


def less_than(check_value, expect_value):
    assert check_value < expect_value


def less_than_or_equals(check_value, expect_value):
    assert check_value <= expect_value


def greater_than(check_value, expect_value):
    assert check_value > expect_value


def greater_than_or_equals(check_value, expect_value):
    assert check_value >= expect_value


def not_equals(check_value, expect_value):
    assert check_value != expect_value


def string_equals(check_value, expect_value):
    assert builtin_str(check_value) == builtin_str(expect_value)


def length_equals(check_value, expect_value):
    assert isinstance(expect_value, integer_types)
    expect_len = _cast_to_int(expect_value)
    assert len(check_value) == expect_len


def length_greater_than(check_value, expect_value):
    assert isinstance(expect_value, integer_types)
    expect_len = _cast_to_int(expect_value)
    assert len(check_value) > expect_len


def length_greater_than_or_equals(check_value, expect_value):
    assert isinstance(expect_value, integer_types)
    expect_len = _cast_to_int(expect_value)
    assert len(check_value) >= expect_len


def length_less_than(check_value, expect_value):
    assert isinstance(expect_value, integer_types)
    expect_len = _cast_to_int(expect_value)
    assert len(check_value) < expect_len


def length_less_than_or_equals(check_value, expect_value):
    assert isinstance(expect_value, integer_types)
    expect_len = _cast_to_int(expect_value)
    assert len(check_value) <= expect_len


def contains(check_value, expect_value):
    assert isinstance(check_value, (list, tuple, dict, basestring))
    assert expect_value in check_value


def contained_by(check_value, expect_value):
    assert isinstance(expect_value, (list, tuple, dict, basestring))
    assert check_value in expect_value


def type_match(check_value, expect_value):
    def get_type(name):
        if isinstance(name, type):
            return name
        elif isinstance(name, basestring):
            try:
                return __builtins__[name]
            except KeyError:
                raise ValueError(name)
        else:
            raise ValueError(name)

    assert isinstance(check_value, get_type(expect_value))


def regex_match(check_value, expect_value):
    assert isinstance(expect_value, basestring)
    assert isinstance(check_value, basestring)
    assert re.match(expect_value, check_value)


def startswith(check_value, expect_value):
    assert builtin_str(check_value).startswith(builtin_str(expect_value))


def endswith(check_value, expect_value):
    assert builtin_str(check_value).endswith(builtin_str(expect_value))