前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >httprunner 3.x学习19 - 从返回头部headers取值

httprunner 3.x学习19 - 从返回头部headers取值

作者头像
上海-悠悠
发布2021-08-20 16:56:49
9650
发布2021-08-20 16:56:49
举报
文章被收录于专栏:从零开始学自动化测试

前言

httprunner 3.x 取值是用 jmespath 表达式,当从头部取值Content-Type时,有特殊字符 -,会报错 jmespath.exceptions.LexerError: Bad jmespath expression: Unknown token ‘-‘: headers.Content-Type

使用示例

从返回的headers提取Content-Type内容

代码语言:javascript
复制
from httprunner import HttpRunner, Config, Step, RunRequest
# 作者-上海悠悠 QQ交流群:717225969 
# blog地址 https://www.cnblogs.com/yoyoketang/

class TestLoginV4Case(HttpRunner):
    config = Config("登录v4用例").base_url("http://127.0.0.1:80")
    teststeps = [
        Step(RunRequest("step-login")
             .post("/api/v1/login")
             .with_json({"username": "test", "password": "123456"})
             .validate()
             .assert_equal("status_code", 200)
             .assert_equal('headers.Content-Type', 'application/json')
             )
    ]

返回的 response 内容

代码语言:javascript
复制
================== response details ==================
status_code : 200
headers  : {
    "Date": "Tue, 17 Aug 2021 10:54:10 GMT",
    "Server": "WSGIServer/0.2 CPython/3.6.8",
    "Content-Type": "application/json",
    "Vary": "Accept, Cookie",
    "Allow": "POST, OPTIONS",
    "X-Frame-Options": "SAMEORIGIN",
    "Content-Length": "109"
}
cookies  : {}
encoding : utf-8
content_type : application/json
body     : {
    "code": 0,
    "msg": "login success!",
    "username": "test",
    "token": "607d2bea6a652b05f3e3d201e7328e2bb4026173"
}

运行的时候会报错

代码语言:javascript
复制
expression: headers.Content-Type
data: {'status_code': 200, 
'headers': {'Date': 'Tue, 17 Aug 2021 10:54:10 GMT', 'Server': 'WSGIServer/0.2 CPython/3.6.8', 'Content-Type': 'application/json', 'Vary': 'Accept, Cookie', 'Allow': 'POST, OPTIONS', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Length': '109'}, 
'cookies': {}, 
'body': {'code': 0, 'msg': 'login success!', 'username': 'test', 'token': '607d2bea6a652b05f3e3d201e7328e2bb4026173'}}
exception: Bad jmespath expression: Unknown token '-':
headers.Content-Type

解决办法

因为headers.Content-Type有特殊字符-,jmespath处理特殊字符可以用引号包起来headers.\"Content-Type\"

代码语言:javascript
复制
             .validate()
             .assert_equal("status_code", 200)
             .assert_equal('headers.\"Content-Type\"', 'application/json')

参考文档:http://frayedmind.com/

2021年第八期《python接口web自动化+测试开发》7.17号开学(课程全面升级!) 加量不加价(新增postman, 赠送selenium和python基础2个课)

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-08-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 使用示例
  • 解决办法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档