httprunner 3.x学习20 - jmespath取值特殊字符问题解决

前言

jmespath在取值的时候,遇到一些特殊的字符处理起来会比较麻烦。
httprunner3.x提取和校验结果都是用jmespath取值,本篇总结几个常见的问题

使用案例

访问/api/test/demo接口,接口返回如下

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

{
    "code":0,
    "msg":"成功success!",
    "data":[
        {
            "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"
        }
    ]
}

validate 校验

需求:
1.提取data数据中name的值为yoyo的邮箱,并校验结果是:283340479@qq.com
2.提取data数据组中,年龄大于20的结果,并校验结果的数量是:1

先看第一个问题,有[]字符的时候

config:
    name: logincase
    base_url: http://127.0.0.1:8000
    variables: {}
teststeps:
-
    name: step1 login
    request:
        url: /api/test/demo
        method: GET
    validate:
        - eq: [status_code, 200]
        - eq: [body.data[?name=='yoyo'].mail, ["283340479@qq.com"]]

运行后结果 :expected ',' or ']', but got '['

D:\demo>hrun demo.yml
2021-08-24 18:56:11.154 | INFO     | httprunner.make:__make:512 - make path: D:\demo\demo.yml
2021-08-24 18:56:11.158 | ERROR    | httprunner.loader:_load_yaml_file:36 - YAMLError:
file: D:\202108kecheng\demo\demo.yml
error: while parsing a flow sequence
  in "D:\202108kecheng\demo\demo.yml", line 13, column 15
expected ',' or ']', but got '['
  in "D:\202108kecheng\demo\demo.yml", line 13, column 25
2021-08-24 18:56:11.159 | WARNING  | httprunner.make:__make:530 - Invalid test file: D:\demo\demo.yml
FileFormatError:
2021-08-24 18:56:11.160 | INFO     | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ...
No Path provided. Nothing to do 
上一篇:【httprunner】环境配置


下一篇:MVC验证13-2个属性至少输入一项