前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest + yaml 框架 -62.支持yaml和json2种格式用例

pytest + yaml 框架 -62.支持yaml和json2种格式用例

作者头像
上海-悠悠
发布2024-04-19 11:03:05
940
发布2024-04-19 11:03:05
举报

前言

v1.5.7版本开始新增json格式用例支持,本次版本改动内容

  • 1.支持 .json 文件用例
  • 2.优化日志中文件后缀名称.yml .yaml .json
  • 3.ruamel.yaml 版本兼容0.18.6

yaml 格式用例

yaml 格式用例示例,test_a.yml

代码语言:javascript
复制
test_demo:
  name: post
  request:
    method: POST
    url: http://httpbin.org/post
    json:
      username: test
      password: "123456"
  extract:
      url:  body.url
  validate:
    - eq: [status_code, 200]
    - eq: [headers.Server, gunicorn/19.9.0]
    - eq: [$..username, test]
    - eq: [body.json.username, test]

执行用例

代码语言:javascript
复制
pytest test_a.yml

json 格式用例

前面的yaml 格式用例,等价于以下json格式用例,test_x.json

代码语言:javascript
复制
{
  "test_demo": {
    "name": "post",
    "request": {
      "method": "POST",
      "url": "http://httpbin.org/post",
      "json": {
        "username": "test",
        "password": "123456"
      }
    },
    "extract": {
      "url": "body.url"
    },
    "validate": [
      {"eq": ["status_code", 200]},
      {"eq": ["headers.Server", "gunicorn/19.9.0"]},
      {"eq": ["$..username", "test"]},
      {"eq": ["body.json.username", "test"]}
    ]
  }
}

执行用例

代码语言:javascript
复制
pytest test_x.json

运行报告日志

代码语言:javascript
复制
collected 1 item                                                                                                   

test_x.json::test_demo
-------------------------------------------------- live log call --------------------------------------------------
2024-04-18 16:41:20 [INFO]: 执行文件-> test_x.json
2024-04-18 16:41:20 [INFO]: base_url-> http://124.70.221.221:8201
2024-04-18 16:41:20 [INFO]: config variables-> {}
2024-04-18 16:41:20 [INFO]: 运行用例-> test_demo
2024-04-18 16:41:20 [INFO]: 用例步骤name: post
2024-04-18 16:41:20 [INFO]: yml raw  -->: {'method': 'POST', 'url': 'http://httpbin.org/post', 'json': {'username':
'test', 'password': '123456'}}
2024-04-18 16:41:20 [INFO]: ------  request info   ------
POST http://httpbin.org/post
headers: {
    "User-Agent": "python-requests/2.31.0",
    "Accept-Encoding": "gzip, deflate",
    "Accept": "*/*",
    "Connection": "keep-alive"
}
json: {
    "username": "test",
    "password": "123456"
}
2024-04-18 16:41:21 [INFO]: ------  response info   ------
url: http://httpbin.org/post
status_code: 200 OK
headers: {
    "Date": "Thu, 18 Apr 2024 08:41:21 GMT",
    "Content-Type": "application/json",
    "Content-Length": "542",
    "Connection": "keep-alive",
    "Server": "gunicorn/19.9.0",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Credentials": "true"
}
cookies: {}
body: {
    "args": {},
    "data": "{\"username\": \"test\", \"password\": \"123456\"}",
    "files": {},
    "form": {},
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Content-Length": "42",
        "Content-Type": "application/json",
        "Host": "httpbin.org",
        "User-Agent": "python-requests/2.31.0",
        "X-Amzn-Trace-Id": "Root=1-6620dcb1-4aeba72a487a44e00566071c"
    },
    "json": {
        "password": "123456",
        "username": "test"
    },
    "origin": "183.193.25.182",
    "url": "http://httpbin.org/post"
}

2024-04-18 16:41:21 [INFO]: extract  提取对象-> {'url': 'body.url'}
2024-04-18 16:41:21 [INFO]: extract  提取结果-> {'url': 'http://httpbin.org/post'}
2024-04-18 16:41:21 [INFO]: validate 校验内容-> [{'eq': ['status_code', 200]}, {'eq': ['headers.Server', 'gunicorn/1
9.9.0']}, {'eq': ['$..username', 'test']}, {'eq': ['body.json.username', 'test']}]
2024-04-18 16:41:21 [INFO]: validate 校验结果-> eq: [200, 200]
2024-04-18 16:41:21 [INFO]: validate 校验结果-> eq: [gunicorn/19.9.0, gunicorn/19.9.0]
2024-04-18 16:41:21 [INFO]: validate 校验结果-> eq: [test, test]
2024-04-18 16:41:21 [INFO]: validate 校验结果-> eq: [test, test]
PASSED                                                                                                       [100%]

================================================ 1 passed in 0.90s ==

之前教程全是yaml格式用例,有部分同学反馈不太习惯yaml格式,所以新增了json格式的用例。

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

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

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

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • yaml 格式用例
  • json 格式用例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com