Skip to main content

pytest requests plugin

Project description

pytest-req

pytest requests plugin

pytest 使用 requests 库的插件。

安装

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()

👉︎ 查看测试

⭐ 支持简单的请求

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

def test_session(session):
    s = session
    s.get('https://httpbin.org/cookies/set/sessioncookie/123456789')
    s.get('https://httpbin.org/cookies')

⭐ 支持base-url

def test_get_method(get):
    """
    test get request
    --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

更多的运行方式请参考 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.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

pytest_req-0.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_req-0.1.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for pytest_req-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d1e0343d6af78284effb082496b4d9f1393ee8c92f5438d665d60e182797a04e
MD5 2f458caf21244a92b2c0ebc06a74b380
BLAKE2b-256 a9e794b15af575aba56c0827e470b8c8425b2f7a035a4013ed1b113dc8a83239

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_req-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for pytest_req-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6339dea39d4138204c526239520aea21775136b65e7f6b4bb721d675fc3173c7
MD5 1263e5f8cb13ed98cdf38ee71a8eeaaa
BLAKE2b-256 bee25439ecb8b865c6fd58fd3464cc4ea39786423c2c6c889f658a94f9e3b661

See more details on using hashes here.

Supported by

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