Skip to main content

pytest requests plugin

Project description

pytest-req

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

pytest requests plugin

pytest 使用 requests 库的插件。

特点

  • 完全兼容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()

👉︎ 查看测试

⭐ 支持简单的请求

# 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

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.2.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

pytest_req-0.2.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_req-0.2.0.tar.gz
  • Upload date:
  • Size: 8.8 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.2.0.tar.gz
Algorithm Hash digest
SHA256 79857dac76e5cb5c7a0971c318523b9dc2f2520d80a7527a3f9eddf82138c292
MD5 323178cf255b02d996d95c68d2daf49e
BLAKE2b-256 bdd548f748f22660706ca2a4de1959d214126abc8692ef2ba812fbfad4e98845

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_req-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0b41aa7ac2a357124d1bc27df88568fc0dffd754752e2ec1af9755b41a37536
MD5 9f70c7e05d61f0f604095bd10d5204ed
BLAKE2b-256 3ceab6e9953e95cb1ec32370d25ce05e281b422657d5fbdf8a9252de54250648

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