前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest + yaml 框架 -68.新增全局请求参数配置verify和headers

pytest + yaml 框架 -68.新增全局请求参数配置verify和headers

作者头像
上海-悠悠
发布2024-04-25 16:40:55
1010
发布2024-04-25 16:40:55
举报

前言

最近有小伙伴提到如何全局添加请求参数verify=False 和 全局请求添加头部参数如:{“x-token”: “xxx”} 之前的版本可以用fixture解决,v1.5.8版本可以支持在config中配置

fixture 更新全局请求

第一种解决方案,通过fixture来更新全局session会话

代码语言:javascript
复制
import pytest

@pytest.fixture(scope="session", autouse=True)
def auto_add_args(requests_session):
    # 全局更新verify = False
    requests_session.verify = False
    # 全局更新头部参数headers
    requests_session.headers.update({"x-token": "xxxxx"})

config中配置全局请求参数

config配置中,目前仅支持verify 和 headers 2个请求相关参数的配置

代码语言:javascript
复制
from pytest_yaml_yoyo.db import ConnectMysql

class Config:
    """每个环境都有一样的公共配置"""
    version = "v1.0"

class TestConfig(Config):
    """测试环境"""
    BASE_URL = 'http://124.70.221.221:8201'
    BLOG_URL = 'https://www.cnblogs.com'
    USERNAME = 'test9'
    PASSWORD = '123456'

    verify = False
    headers = {"xx-token": "xx111"}

class UatConfig(Config):
    """联调环境"""
    BASE_URL = 'http://124.70.221.221:8201'
    USERNAME = 'test_uat'
    PASSWORD = '123456'

# 环境关系映射,方便切换多环境配置
env = {
    "test": TestConfig,
    "uat": UatConfig
}

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

运行日志

代码语言:javascript
复制
>pytest test_a.yml
================================================== test session starts ==================================================
platform win32 -- Python 3.9.13, pytest-7.4.4, pluggy-1.0.0
rootdir: D:\demo\untitled3.9
configfile: pytest.ini
plugins: allure-pytest-2.13.1, Faker-18.4.0, repeat-0.9.1, rerunfailures-12.0, runtime-yoyo-1.0.1, yaml-yoyo-1.5.8
collected 1 item                                                                                                         

test_a.yml::test_demo
----------------------------------------------------- live log call -----------------------------------------------------
2024-04-22 12:14:17 [INFO]: 执行文件-> test_a.yml
2024-04-22 12:14:17 [INFO]: base_url-> http://124.70.221.221:8201
2024-04-22 12:14:17 [INFO]: config variables-> {}
2024-04-22 12:14:17 [INFO]: 运行用例-> test_demo
2024-04-22 12:14:17 [INFO]: 用例步骤name: post
2024-04-22 12:14:17 [INFO]: yml raw  -->: {'method': 'POST', 'url': 'http://httpbin.org/post', 'json': {'username': 'test'
, 'password': '123456'}}
2024-04-22 12:14:17 [INFO]: ------  request info   ------
POST http://httpbin.org/post
headers: {
    "User-Agent": "python-requests/2.31.0",
    "Accept-Encoding": "gzip, deflate",
    "Accept": "*/*",
    "Connection": "keep-alive",
    "x-token": "xxxxx"
}
json: {
    "username": "test",
    "password": "123456"
}
2024-04-22 12:14:18 [INFO]: ------  response info   ------
url: http://httpbin.org/post
status_code: 200 OK
headers: {
    "Date": "Mon, 22 Apr 2024 04:14:17 GMT",
    "Content-Type": "application/json",
    "Content-Length": "567",
    "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-6625e418-5610a93047c129fe3a7e1d84",
        "X-Token": "xxxxx"
    },
    "json": {
        "password": "123456",
        "username": "test"
    },
    "origin": "183.193.25.182",
    "url": "http://httpbin.org/post"
}

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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • fixture 更新全局请求
  • config中配置全局请求参数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com