httprunner2.x--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: 字符串是否以什么結尾

源碼:\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))

 

例如

- 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 中,將斷言使用的比較器進行了名稱的統一,每個比較器的名稱可以有多種別名,如 equals 可以簡寫成 eq 、 == 或者 is 等名稱。 象 eq、ge、gt 等寫法,和 Linux 中的 shell 腳本比較符名稱相同,可以進行類比記憶。

各比較器的名稱整理如下:  

 

 

源碼\httprunner-2.5.5\httprunner\parser.py

def get_uniform_comparator(comparator):
    """ convert comparator alias to uniform name
    """
    if comparator in ["eq", "equals", "==", "is"]:
        return "equals"
    elif comparator in ["lt", "less_than"]:
        return "less_than"
    elif comparator in ["le", "less_than_or_equals"]:
        return "less_than_or_equals"
    elif comparator in ["gt", "greater_than"]:
        return "greater_than"
    elif comparator in ["ge", "greater_than_or_equals"]:
        return "greater_than_or_equals"
    elif comparator in ["ne", "not_equals"]:
        return "not_equals"
    elif comparator in ["str_eq", "string_equals"]:
        return "string_equals"
    elif comparator in ["len_eq", "length_equals", "count_eq"]:
        return "length_equals"
    elif comparator in ["len_gt", "count_gt", "length_greater_than", "count_greater_than"]:
        return "length_greater_than"
    elif comparator in ["len_ge", "count_ge", "length_greater_than_or_equals",
                        "count_greater_than_or_equals"]:
        return "length_greater_than_or_equals"
    elif comparator in ["len_lt", "count_lt", "length_less_than", "count_less_than"]:
        return "length_less_than"
    elif comparator in ["len_le", "count_le", "length_less_than_or_equals",
                        "count_less_than_or_equals"]:
        return "length_less_than_or_equals"
    else:
        return comparator

  

例如

name: 斷言的2種寫法


request:
    url: https://www.baidu.com/
    method: GET
    headers:
      User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36


validate:
    # 斷言一
    - eq: [status_code, 200]
    # 斷言二
    - check: status_code
      comparator: eq
      expect: 200
    # 等價斷言一
    - is: [status_code, 200]
    - ==: [status_code, 200]
    - equals: [status_code, 200]

  

執行后查看報告

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM