pytest - req插件:更简单的做接口测试!

背景

我们经常会用到 pytest 和 requests 进行接口自动化测试。pytest 提供了非常方便的插件开发能力,在pytest中使用requests库首先会想到是否有已经封装好的插件,就像pytest-playwrightpytest-selenium一样。可惜找了一下没有。

于是,自己动手实现了一个,本来命名为pytest-requestspypi 仓库搜索了一下被被占用了。pytest-requests是一个用YAML写接口用例的库,类似httprunner。最终命名为pytest-req

整个插件的设计思路比较简单,将requests常用的请求方法设计成pytest.fixture钩子函数;增加请求响应日志,从seldom框架封装的代码,使用pytest-base-url 实现基础URL的全局设置。最终使用起来比 直接在 pytest写requests请求简单了很多。

简介

pytest requests plugin

pytest 使用 requests 库的插件。

特点
  • • 完全兼容Requests的使用。

  • • 提供详细的请求日志。

  • • 轻量级,非侵入。

安装

支持pip安装pytest-req插件。

pip install pytest-req

使用

pytest-req 完全兼容 Requests API 如下:

pytest-req(fixture)requests
get()requests.get()
post()requests.post()
put()requests.put()
delete()requests.delete()
patch()requests.patch()
session()requests.session()

👉︎ [查看测试]https://github.com/SeldomQA/pytest-req/tree/main/tests

⭐ 支持简单的请求


# test_req.py


def test_post_method(post):

    """

    test post request

    """

    s = post('https://httpbin.org/post', data={'key': 'value'})

    assert s.status_code == 200



def test_get_method(get):

    """

    test get request

    """

    payload = {'key1': 'value1', 'key2': 'value2'}

    s = get("https://httpbin.org/get", params=payload)

    assert s.status_code == 200

⭐ 支持Session


# test_session.py


def test_session(session):

    """

    test session, keep requests cookie

    """

    s = session

    s.get('https://httpbin.org/cookies/set/sessioncookie/123456789')

    s.get('https://httpbin.org/cookies')

⭐ 支持base-url


# test_base_url.py


def test_req_base_url(get):

    """

    test base url

    pytest --base-url=https://httpbin.org

    """

    payload = {'key1': 'value1', 'key2': 'value2'}

    s = get("/get", params=payload)

    assert s.status_code == 200

更多的使用方式参考 requests 文档。

✅ 运行测试


> pytest -s  # 运行当前所有用例

> pytest -s test_req.py # 运行指定文件

> pytest -s --base-url=https://httpbin.org  # 指定base-url

-s 查看详细日志

--base-url 指定请求基础URL,用例中可以不设置。

更多的运行方式请参考 pytest 文档。

🗒 运行日志


> pytest -qs --base-url=https://httpbin.org test_base_url.py


2024-07-24 12:18:39 | INFO     | plugin.py | -------------- Request -----------------[🚀]

2024-07-24 12:18:39 | INFO     | plugin.py | [method]: GET      [url]: /get 

2024-07-24 12:18:39 | DEBUG    | plugin.py | [params]:

{

  "key1": "value1",

  "key2": "value2"

}

2024-07-24 12:18:40 | INFO     | plugin.py | -------------- Response ----------------[🛬️]

2024-07-24 12:18:40 | INFO     | plugin.py | successful with status 200

2024-07-24 12:18:40 | DEBUG    | plugin.py | [type]: json      [time]: 1.655213

2024-07-24 12:18:40 | DEBUG    | plugin.py | [response]:

 {

  "args": {

    "key1": "value1",

    "key2": "value2"

  },

  "headers": {

    "Accept": "*/*",

    "Accept-Encoding": "gzip, deflate",

    "Host": "httpbin.org",

    "User-Agent": "python-requests/2.32.3",

    "X-Amzn-Trace-Id": "Root=1-66a080a0-2cb150485a260ae75b34b32f"

  },

  "origin": "171.10.176.209",

  "url": "https://httpbin.org/get?key1=value1&key2=value2"

}

.2024-07-24 12:18:40 | INFO     | plugin.py | -------------- Request -----------------[🚀]

2024-07-24 12:18:40 | INFO     | plugin.py | [method]: GET      [url]: /cookies/set/sessioncookie/123456789 

2024-07-24 12:18:43 | INFO     | plugin.py | -------------- Response ----------------[🛬️]

2024-07-24 12:18:43 | INFO     | plugin.py | successful with status 200

2024-07-24 12:18:43 | DEBUG    | plugin.py | [type]: json      [time]: 0.807398

2024-07-24 12:18:43 | DEBUG    | plugin.py | [response]:

 {

  "cookies": {

    "sessioncookie": "123456789"

  }

}

2024-07-24 12:18:43 | INFO     | plugin.py | -------------- Request -----------------[🚀]

2024-07-24 12:18:43 | INFO     | plugin.py | [method]: GET      [url]: /cookies 

2024-07-24 12:18:44 | INFO     | plugin.py | -------------- Response ----------------[🛬️]

2024-07-24 12:18:44 | INFO     | plugin.py | successful with status 200

2024-07-24 12:18:44 | DEBUG    | plugin.py | [type]: json      [time]: 1.226137

2024-07-24 12:18:44 | DEBUG    | plugin.py | [response]:

 {

  "cookies": {

    "sessioncookie": "123456789"

  }

}

.

2 passed in 5.36s

最后作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些软件测试的学习资源,希望能给你前进的路上带来帮助。

视频文档获取方式:
这份文档和视频资料,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!以上均可以分享,点下方小卡片即可自行领取。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值