pytest-req


Namepytest-req JSON
Version 0.3.0 PyPI version JSON
download
home_pageNone
Summarypytest requests plugin
upload_time2024-08-31 13:22:53
maintainerNone
docs_urlNone
authorbugmaster
requires_python>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pytest-req

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

```

> pytest requests plugin

pytest 使用 requests 库的插件。

## 特点

* 完全兼容Requests库的使用。
* 提供详细的`请求/响应`日志,并支持可配置。
* 轻量级,非侵入。

## 安装

```shell
pip install pytest-req
```

## 使用

pytest-req 完全兼容 [Requests](https://docs.python-requests.org/en/master/) API 如下:

| pytest-req(fixture) | requests           |
|---------------------|--------------------|
| 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(response).to_be_ok()                                             | 状态码 200                                    |
| expect(response).to_have_status_code(404)                               | 状态码等于 404                                  |
| expect(response).to_have_json_matching(json_data, exclude=[])           | 断言JSON数据,exclude=[排查的字段列表]                 |
| expect(response).to_have_path_value(path="headers.Host", value="value") | 断言提取的数据,是否等于value ,参考:https://jmespath.org |

👉︎ [查看测试](./tests)

__⭐ 支持简单的请求__

```python
# test_req.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__

```python
# 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__

```python
# 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 文档。

__✅ 运行测试__

```shell
> pytest -s  # 运行当前所有用例
> pytest -s test_req.py # 运行指定文件
> pytest -s --base-url=https://httpbin.org  # 指定base-url
```

- `-s` 查看详细日志。
- `--base-url` 设置接口基础URL,用例当中进需要配置路径。

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

__🗒 运行日志__

```shell
> 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
```


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pytest-req",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "bugmaster",
    "author_email": "defnngj@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/00/54/25829a154c2a702f40f47675d1b8601fb6b03e97a28bcbd2c6f8673c7763/pytest_req-0.3.0.tar.gz",
    "platform": null,
    "description": "# pytest-req\n\n```shell\n                 __            __                             \n    ____  __  __/ /____  _____/ /_            ________  ____ _\n   / __ \\/ / / / __/ _ \\/ ___/ __/  ______   / ___/ _ \\/ __ `/\n  / /_/ / /_/ / /_/  __(__  ) /_   /_____/  / /  /  __/ /_/ / \n / .___/\\__, /\\__/\\___/____/\\__/           /_/   \\___/\\__, /  \n/_/    /____/                                           /_/   \n\n```\n\n> pytest requests plugin\n\npytest \u4f7f\u7528 requests \u5e93\u7684\u63d2\u4ef6\u3002\n\n## \u7279\u70b9\n\n* \u5b8c\u5168\u517c\u5bb9Requests\u5e93\u7684\u4f7f\u7528\u3002\n* \u63d0\u4f9b\u8be6\u7ec6\u7684`\u8bf7\u6c42/\u54cd\u5e94`\u65e5\u5fd7\uff0c\u5e76\u652f\u6301\u53ef\u914d\u7f6e\u3002\n* \u8f7b\u91cf\u7ea7\uff0c\u975e\u4fb5\u5165\u3002\n\n## \u5b89\u88c5\n\n```shell\npip install pytest-req\n```\n\n## \u4f7f\u7528\n\npytest-req \u5b8c\u5168\u517c\u5bb9 [Requests](https://docs.python-requests.org/en/master/) API \u5982\u4e0b:\n\n| pytest-req(fixture) | requests           |\n|---------------------|--------------------|\n| get()               | requests.get()     |\n| post()              | requests.post()    |\n| put()               | requests.put()     |\n| delete()            | requests.delete()  |\n| patch()             | requests.patch()   |\n| options()           | requests.options() |\n| head()              | requests.head()    |\n| session()  \u26a0        | requests.session() |\n\n> session IDE\u65e0\u6cd5\u81ea\u52a8\u8865\u5168\u3002\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528session\u4e0b\u9762\u7684`get()/post()/put()...`\n\npytest-req \u63d0\u4f9b `expect` \u9488\u5bf9\u63a5\u53e3\u8fd4\u56de\u6570\u636e\u8fdb\u884c\u65ad\u8a00\u3002\n\n| pytest-req(assert)                                                      | \u8bf4\u660e                                         |\n|-------------------------------------------------------------------------|--------------------------------------------|\n| expect(response).to_be_ok()                                             | \u72b6\u6001\u7801 200                                    |\n| expect(response).to_have_status_code(404)                               | \u72b6\u6001\u7801\u7b49\u4e8e 404                                  |\n| expect(response).to_have_json_matching(json_data, exclude=[])           | \u65ad\u8a00JSON\u6570\u636e\uff0cexclude=[\u6392\u67e5\u7684\u5b57\u6bb5\u5217\u8868]                 |\n| expect(response).to_have_path_value(path=\"headers.Host\", value=\"value\") | \u65ad\u8a00\u63d0\u53d6\u7684\u6570\u636e\uff0c\u662f\u5426\u7b49\u4e8evalue ,\u53c2\u8003\uff1ahttps://jmespath.org |\n\n\ud83d\udc49\ufe0e [\u67e5\u770b\u6d4b\u8bd5](./tests)\n\n__\u2b50 \u652f\u6301\u7b80\u5355\u7684\u8bf7\u6c42__\n\n```python\n# test_req.py\nfrom pytest_req.assertions import expect\n\n\ndef test_post_method(post):\n    \"\"\"\n    test post request\n    \"\"\"\n    s = post('https://httpbin.org/post', data={'key': 'value'})\n    expect(s).to_be_ok()\n\n\ndef test_get_method(get):\n    \"\"\"\n    test get request\n    \"\"\"\n    payload = {'key1': 'value1', 'key2': 'value2'}\n    s = get(\"https://httpbin.org/get\", params=payload)\n    expect(s).to_be_ok()\n```\n\n__\u2b50 \u652f\u6301Session__\n\n```python\n# test_session.py\n\ndef test_session(session):\n    \"\"\"\n    test session, keep requests cookie\n    \"\"\"\n    s = session\n    s.get('https://httpbin.org/cookies/set/sessioncookie/123456789')\n    s.get('https://httpbin.org/cookies')\n```\n\n__\u2b50 \u652f\u6301base-url__\n\n```python\n# test_base_url.py\n\ndef test_req_base_url(get):\n    \"\"\"\n    test base url\n    pytest --base-url=https://httpbin.org\n    \"\"\"\n    payload = {'key1': 'value1', 'key2': 'value2'}\n    s = get(\"/get\", params=payload)\n    assert s.status_code == 200\n```\n\n\u66f4\u591a\u7684\u4f7f\u7528\u65b9\u5f0f\u53c2\u8003 requests \u6587\u6863\u3002\n\n__\u2705 \u8fd0\u884c\u6d4b\u8bd5__\n\n```shell\n> pytest -s  # \u8fd0\u884c\u5f53\u524d\u6240\u6709\u7528\u4f8b\n> pytest -s test_req.py # \u8fd0\u884c\u6307\u5b9a\u6587\u4ef6\n> pytest -s --base-url=https://httpbin.org  # \u6307\u5b9abase-url\n```\n\n- `-s` \u67e5\u770b\u8be6\u7ec6\u65e5\u5fd7\u3002\n- `--base-url` \u8bbe\u7f6e\u63a5\u53e3\u57fa\u7840URL\uff0c\u7528\u4f8b\u5f53\u4e2d\u8fdb\u9700\u8981\u914d\u7f6e\u8def\u5f84\u3002\n\n\u66f4\u591a\u7684\u8fd0\u884c\u65b9\u5f0f\u8bf7\u53c2\u8003 pytest \u6587\u6863\u3002\n\n__\ud83d\uddd2 \u8fd0\u884c\u65e5\u5fd7__\n\n```shell\n> pytest -qs --base-url=https://httpbin.org test_base_url.py\n\n2024-07-24 12:18:39 | INFO     | plugin.py | -------------- Request -----------------[\ud83d\ude80]\n2024-07-24 12:18:39 | INFO     | plugin.py | [method]: GET      [url]: /get \n2024-07-24 12:18:39 | DEBUG    | plugin.py | [params]:\n{\n  \"key1\": \"value1\",\n  \"key2\": \"value2\"\n}\n2024-07-24 12:18:40 | INFO     | plugin.py | -------------- Response ----------------[\ud83d\udeec\ufe0f]\n2024-07-24 12:18:40 | INFO     | plugin.py | successful with status 200\n2024-07-24 12:18:40 | DEBUG    | plugin.py | [type]: json      [time]: 1.655213\n2024-07-24 12:18:40 | DEBUG    | plugin.py | [response]:\n {\n  \"args\": {\n    \"key1\": \"value1\",\n    \"key2\": \"value2\"\n  },\n  \"headers\": {\n    \"Accept\": \"*/*\",\n    \"Accept-Encoding\": \"gzip, deflate\",\n    \"Host\": \"httpbin.org\",\n    \"User-Agent\": \"python-requests/2.32.3\",\n    \"X-Amzn-Trace-Id\": \"Root=1-66a080a0-2cb150485a260ae75b34b32f\"\n  },\n  \"origin\": \"171.10.176.209\",\n  \"url\": \"https://httpbin.org/get?key1=value1&key2=value2\"\n}\n.2024-07-24 12:18:40 | INFO     | plugin.py | -------------- Request -----------------[\ud83d\ude80]\n2024-07-24 12:18:40 | INFO     | plugin.py | [method]: GET      [url]: /cookies/set/sessioncookie/123456789 \n2024-07-24 12:18:43 | INFO     | plugin.py | -------------- Response ----------------[\ud83d\udeec\ufe0f]\n2024-07-24 12:18:43 | INFO     | plugin.py | successful with status 200\n2024-07-24 12:18:43 | DEBUG    | plugin.py | [type]: json      [time]: 0.807398\n2024-07-24 12:18:43 | DEBUG    | plugin.py | [response]:\n {\n  \"cookies\": {\n    \"sessioncookie\": \"123456789\"\n  }\n}\n2024-07-24 12:18:43 | INFO     | plugin.py | -------------- Request -----------------[\ud83d\ude80]\n2024-07-24 12:18:43 | INFO     | plugin.py | [method]: GET      [url]: /cookies \n2024-07-24 12:18:44 | INFO     | plugin.py | -------------- Response ----------------[\ud83d\udeec\ufe0f]\n2024-07-24 12:18:44 | INFO     | plugin.py | successful with status 200\n2024-07-24 12:18:44 | DEBUG    | plugin.py | [type]: json      [time]: 1.226137\n2024-07-24 12:18:44 | DEBUG    | plugin.py | [response]:\n {\n  \"cookies\": {\n    \"sessioncookie\": \"123456789\"\n  }\n}\n.\n2 passed in 5.36s\n```\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "pytest requests plugin",
    "version": "0.3.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7039a26732405529d478997c3c4b2facf0dcc7733c320d998333ed27650f04dc",
                "md5": "b4c42347ba0315ecdadbbfc9ed93c909",
                "sha256": "1f85d3e3d8a0da715f84e137eed5a1c29ff1f66b14b4ca74e6f57b0ac5822315"
            },
            "downloads": -1,
            "filename": "pytest_req-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b4c42347ba0315ecdadbbfc9ed93c909",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 12741,
            "upload_time": "2024-08-31T13:22:52",
            "upload_time_iso_8601": "2024-08-31T13:22:52.131608Z",
            "url": "https://files.pythonhosted.org/packages/70/39/a26732405529d478997c3c4b2facf0dcc7733c320d998333ed27650f04dc/pytest_req-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "005425829a154c2a702f40f47675d1b8601fb6b03e97a28bcbd2c6f8673c7763",
                "md5": "b39ddd8b56ad0170a213cce82e686129",
                "sha256": "e327c811a8b2d41841a4da03cf037f3413636c62fcacc293c0f6d0ab8cfab559"
            },
            "downloads": -1,
            "filename": "pytest_req-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "b39ddd8b56ad0170a213cce82e686129",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 12764,
            "upload_time": "2024-08-31T13:22:53",
            "upload_time_iso_8601": "2024-08-31T13:22:53.571060Z",
            "url": "https://files.pythonhosted.org/packages/00/54/25829a154c2a702f40f47675d1b8601fb6b03e97a28bcbd2c6f8673c7763/pytest_req-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-31 13:22:53",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "pytest-req"
}
        
Elapsed time: 0.48228s