httprunner学习 -validate校验器

前言

在一个完整的测试用例中,断言是必不可少的,断言是拿实际结果和期望结果去比较,在httprunner中用validate来对比测试结果。

validate校验器

在httprunner的源码里,utils.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

平常用eq比较多,eq就是equals的缩写,这里总结下相关的校验方法

  • eq equals,判断实际结果和期望结果是否相等,可以用"eq", "equals", "==", "is"
  • lt less_than, 判断实际结果小于期望结果 ,可以用 "lt", "less_than"
  • le less_than_or_equals,判断实际结果小于等于期望结果 ,可以用 "le", "less_than_or_equals"
  • gt greater_than,判断实际结果大于期望结果,可以用"gt", "greater_than"
  • ge greater_than_or_equals,判断实际结果大于等于期望结果,可以用"ge", "greater_than_or_equals"
  • ne not_equals, 判断实际结果和期望结果不相等,可以用"ne", "not_equals"
  • str_eq string_equals 判断转字符串后对比 实际结果和期望结果是否相等,可以用"str_eq", "string_equals"
  • len_eq length_equals 判断字符串或list长度,可以用"len_eq", "length_equals", "count_eq"
  • len_gt length_greater_than 判断实际结果的长度大于和期望结果,可以用"len_gt", "count_gt", "length_greater_than", "count_greater_than"
  • len_ge length_greater_than_or_equals 实际结果的长度大于等于期望结果,可以用"len_ge", "count_ge", "length_greater_than_or_equals", "count_greater_than_or_equals"
  • len_lt length_less_than 实际结果的长度小于期望结果,可以用"len_lt", "count_lt", "length_less_than", "count_less_than"
  • len_le length_less_than_or_equals 实际结果的长度小于等于期望结果,可以用"len_le", "count_le", "length_less_than_or_equals", "count_less_than_or_equals"

案例

接着上一篇的demo案例,返回内容如下

C:\Users\dell>http http://127.0.0.1:8000/api/test/demo
HTTP/1.1 200 OK
Content-Length: 255
Content-Type: application/json
Date: Sun, 22 Sep 2019 10:11:07 GMT
Server: WSGIServer/0.2 CPython/3.6.0
X-Frame-Options: SAMEORIGIN

{
    "code": 0,
    "datas": [
        {
            "age": 20,
            "create_time": "2019-09-15",
            "id": 1,
            "mail": "283340479@qq.com",
            "name": "yoyo",
            "sex": "M"
        },
        {
            "age": 21,
            "create_time": "2019-09-16",
            "id": 2,
            "mail": "123445@qq.com",
            "name": "yoyo111",
            "sex": "M"
        }
    ],
    "msg": "success!"
}

针对上面的返回结果写几种不同的validate,校验返回结果,报错问test_validate_demo.yml

- config:
    name: test_demo
    variables: {}
- test:
    name: test_demo case1
    request:
        url: http://127.0.0.1:8000/api/test/demo
        method: GET
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
        json:
            username: test
            password: 123456
    extract:
        - mail: content.datas.0.mail         # 提取mail
    validate:
        - eq: [status_code, 200]
        - less_than: [status_code, 400]
        - equals: [content.code, 0]
        - equals: [content.msg, success!]
        - length_equals: [content.datas, 2]
        - greater_than_or_equals: [content.datas.0.age, 18]

运行用例

运行结果

D:\soft\untitled>hrun test_validate_demo.yml
test_demo case1
INFO     GET http://127.0.0.1:8000/api/test/demo
INFO     status_code: 200, response_time(ms): 5.99 ms, response_length: 255 bytes
INFO     start to extract from response object.
INFO     start to validate.
.

----------------------------------------------------------------------
Ran 1 test in 0.011s

OK
INFO     Start to render Html report ...
INFO     Generated Html report: D:\soft\untitled\reports\1569165498.html

查看报告

httprunner学习 -validate校验器

validate两种格式

validate支持两种格式:

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

那么上面的validate校验也可以用下面这种格式

    validate:
        - check: status_code
          comparator: eq
          expect: 200

        - check: status_code
          comparator: less_than
          expect: 400

        - check: content.code
          comparator: equals
          expect: 0

        - check: content.msg
          comparator: equals
          expect: success!
  转:https://www.cnblogs.com/yoyoketang/p/11569942.html
上一篇:开源后端数据校验插件Validate.Net,类似Validate.js


下一篇:nodejs Joi报错:Joi.validate is not a function及解决方式