Skip to main content

pytest requests plugin

Project description

pytest-req

                 __            __                             
    ____  __  __/ /____  _____/ /_            ________  ____ _
   / __ \/ / / / __/ _ \/ ___/ __/  ______   / ___/ _ \/ __ `/
  / /_/ / /_/ / /_/  __(__  ) /_   /_____/  / /  /  __/ /_/ / 
 / .___/\__, /\__/\___/____/\__/           /_/   \___/\__, /  
/_/    /____/                                           /_/   

pytest requests plugin

pytest 使用 Requests 库的插件。完全兼容 Requests 库的使用。轻量级,非侵入。

特点

  • 支持详细的请求/响应日志,并支持可配置。
  • 支持断言response 断言。

安装

pip install pytest-req

使用

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

pytest-req(fixture) requests
req() requests.request()
get() requests.get()
post() requests.post()
put() requests.put()
delete() requests.delete()
patch() requests.patch()
options() requests.options()
head() requests.head()
session() ⚠ requests.session()

session IDE无法自动补全。可以正常使用session下面的get()/post()/put()...

pytest-req 提供 expect 针对接口返回数据进行断言。

pytest-req(assert) 说明
expect(s).to_be_ok() 状态码 200
expect(s).to_have_status_code(404) 状态码等于 404
expect(s).to_have_json_matching(json_data, exclude=[]) 断言JSON数据,exclude=[排查的字段列表]
expect(s).to_have_path_value("headers.Host", "httpbin.org") 提取的数据,断言是否等于, 参考:https://jmespath.org
expect(s).to_have_path_contains("headers.Host", "httpbin") 提取的数据,断言是否包含, 参考:https://jmespath.org

👉︎ 查看测试

⭐ 支持简单的请求

# test_assert.py
from pytest_req.assertions import expect


def test_post_method(post):
    """
    test post request
    """
    s = post('https://httpbin.org/post', data={'key': 'value'})
    expect(s).to_be_ok()


def test_get_method(get):
    """
    test get request
    """
    payload = {'key1': 'value1', 'key2': 'value2'}
    s = get("https://httpbin.org/get", params=payload)
    expect(s).to_be_ok()

⭐ 支持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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_req-0.5.2.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_req-0.5.2-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest_req-0.5.2.tar.gz.

File metadata

  • Download URL: pytest_req-0.5.2.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pytest_req-0.5.2.tar.gz
Algorithm Hash digest
SHA256 876ba8db2388f0a5daa1dd83ec9aea4981d2eab9a49138ad8e0e3fd111c598b9
MD5 66dfe770f45bfed0eef35c4acdc101dd
BLAKE2b-256 d6fa69c961d2899f45e73641db3d12f93e589abf94feb3bce2f192b3505005f2

See more details on using hashes here.

File details

Details for the file pytest_req-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: pytest_req-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pytest_req-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1b8c77fdf3be01326dca5be97efdb9e39dc904f473e917834a71895743adbd0a
MD5 84686ffcc7ecf2e496343f162804f842
BLAKE2b-256 137cdece16d4bc55237953fe6a639865541a83bb8e0f88bf6b41a801e3430ae1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page