httprunner 3.x學習18 - validate 斷言總結


前言

httprunner 3.x使用 validate 方法斷言

comparator 校驗方式

validate 可以支持的校驗方式

comparator 縮寫 功能
equal "eq", "equals", "equal" 相等
less_than "lt", "less_than" 小於
less_or_equals "le", "less_or_equals" 小於或等於
greater_than "gt", "greater_than" 大於
greater_or_equals "ge", "greater_or_equals" 大於或等於
not_equal "ne", "not_equal" 不等於
string_equals "str_eq", "string_equals" 轉字符串相等
length_equal "len_eq", "length_equal" 長度相等
length_greater_than "len_gt","length_greater_than" 長度大於
length_greater_or_equals "len_ge","length_greater_or_equals" 長度大於或等於
length_less_than "len_lt", "length_less_than" 長度小於
length_less_or_equals ""len_le", "length_less_or_equals" 長度小於或等於
contains check_value 包含 expect_value
contained_by expect_value 包含check_value
type_match type類型匹配
regex_match 正則匹配re.match(expect_value, check_value)
startswith 字符串以xx開頭
endswith 字符串以xx結尾

yaml 中可以寫2種格式校驗

httprunner 校驗返回結果有2種格式

  • {"comparator_name": [check_value, expect_value]}
  • {"check": check_value, "comparator": comparator_name, "expect": expect_value}

我們平常用的比較多是是上面這種,直接拿到返回結果,校驗與期望結果是否一致

# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

validate:
        - eq: [status_code, 200]
        - less_than: [status_code, 400]
        - equals: [body.code, 0]
        - equals: [body.msg, success!]
        - length_equals: [body.datas, 2]
        - greater_than_or_equals: [body.datas.0.age, 18]

pytest腳本中的斷言

先調用.validate()后,可以看到全部assert方法,在StepRequestValidation 類中定義

方法 對應校驗方法 功能
assert_equal equal 相等
assert_not_equal not_equal 不等於
assert_less_than less_than 小於
assert_less_or_equals less_or_equals 小於或等於
assert_greater_than greater_than 大於
assert_greater_or_equals greater_or_equals 大於或等於
assert_assert_string_equals string_equals 轉字符串相等
assert_length_equal length_equal 長度相等
assert_length_greater_than length_greater_than 長度大於
assert_length_greater_or_equals length_greater_or_equals 長度大於或等於
assert_length_less_than length_less_than 長度小於
assert_length_less_or_equals length_less_or_equals 長度小於或等於
assert_contains contains check_value 包含 expect_value
assert_contained_by contained_by expect_value 包含check_value
assert_type_match type_match type類型匹配
assert_regex_match regex_match 正則匹配re.match(expect_value, check_value)
assert_startswith startswith 字符串以xx開頭
assert_endswith endswith 字符串以xx結尾

使用方式先調用 .validate() 方法,才能調用 assert 斷言方法


免責聲明!

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



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