Location>code7788 >text

pytest-req plugin: easier to do interface testing

Popularity:993 ℃/2024-07-26 12:26:33

pytest-req plugin: easier to do interface testing

contexts

We often use pytest and requests for interface automation testing. pytest provides a very convenient plug-in development capabilities, in pytest to use the requests library will first think of whether there has been packaged plug-ins, like thepytest-playwrightpytest-seleniumSame. Unfortunately looking for it didn't.

So, took it upon myself to implement one, originally namedpytest-requestspypi Warehouse searched was taken.pytest-requestsis a library for writing interface use cases in YAML, similar to thehttprunner. The final name ispytest-req

The design idea of the whole plugin is relatively simple, designing the request methods commonly used by requests into theHook function; addrequestingcap (a poem)responsivelogging, code wrapped from the seldom framework, using thepytest-base-url This is to enable the global setting of the base URL. The end use is much simpler than writing requests directly in pytest.

synopsis

pytest requests plugin

pytest uses plugins from the requests library.

specificities

  • Fully compatible with the use of the Requests library.
  • Provides detailed request/response logs and supports configurability.
  • Lightweight and non-intrusive.

mounting

be in favor ofpipmountingpytest-reqPlug-ins.

pip install pytest-req

utilization

pytest-req is fully compatible withRequests The API is as follows.

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

session IDE can't auto-complete. You can use get()/post()/put() under session normally...

👉︎ [view test]/SeldomQA/pytest-req/tree/main/tests

⭐ Support for simple requests

# test_req.py

def test_post_method(post):
    """
    test post request
    """
    s = post('/post', data={'key': 'value'})
    assert s.status_code == 200


def test_get_method(get):
    """
    test get request
    """
    payload = {'key1': 'value1', 'key2': 'value2'}
    s = get("/get", params=payload)
    assert s.status_code == 200

⭐ Support for Session

# test_session.py

def test_session(session):
    """
    test session, keep requests cookie
    """
    s = session
    ('/cookies/set/sessioncookie/123456789')
    ('/cookies')

⭐ Support for base-url

# test_base_url.py

def test_req_base_url(get):
    """
    test base url
    pytest --base-url=
    """
    payload = {'key1': 'value1', 'key2': 'value2'}
    s = get("/get", params=payload)
    assert s.status_code == 200

See the requests documentation for more information on how to use it.

✅ Operational testing

> pytest -s # run all current use cases
> pytest -s test_req.py # run the specified file
> pytest -s --base-url= # Specify base-url

-s View Detailed Log

--base-url Specifies the request base URL, which can be left unset in the use case.

See the pytest documentation for more information on how to run it.

🗒 Running Logs

> pytest -qs --base-url= test_base_url.py

2024-07-24 12:18:39 | INFO     |  | -------------- Request -----------------[🚀]
2024-07-24 12:18:39 | INFO     |  | [method]: GET      [url]: /get 
2024-07-24 12:18:39 | DEBUG    |  | [params]:
{
  "key1": "value1",
  "key2": "value2"
}
2024-07-24 12:18:40 | INFO     |  | -------------- Response ----------------[🛬️]
2024-07-24 12:18:40 | INFO     |  | successful with status 200
2024-07-24 12:18:40 | DEBUG    |  | [type]: json      [time]: 1.655213
2024-07-24 12:18:40 | DEBUG    |  | [response]:
 {
  "args": {
    "key1": "value1",
    "key2": "value2"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "",
    "User-Agent": "python-requests/2.32.3",
    "X-Amzn-Trace-Id": "Root=1-66a080a0-2cb150485a260ae75b34b32f"
  },
  "origin": "171.10.176.209",
  "url": "/get?key1=value1&key2=value2"
}
.2024-07-24 12:18:40 | INFO     |  | -------------- Request -----------------[🚀]
2024-07-24 12:18:40 | INFO     |  | [method]: GET      [url]: /cookies/set/sessioncookie/123456789 
2024-07-24 12:18:43 | INFO     |  | -------------- Response ----------------[🛬️]
2024-07-24 12:18:43 | INFO     |  | successful with status 200
2024-07-24 12:18:43 | DEBUG    |  | [type]: json      [time]: 0.807398
2024-07-24 12:18:43 | DEBUG    |  | [response]:
 {
  "cookies": {
    "sessioncookie": "123456789"
  }
}
2024-07-24 12:18:43 | INFO     |  | -------------- Request -----------------[🚀]
2024-07-24 12:18:43 | INFO     |  | [method]: GET      [url]: /cookies 
2024-07-24 12:18:44 | INFO     |  | -------------- Response ----------------[🛬️]
2024-07-24 12:18:44 | INFO     |  | successful with status 200
2024-07-24 12:18:44 | DEBUG    |  | [type]: json      [time]: 1.226137
2024-07-24 12:18:44 | DEBUG    |  | [response]:
 {
  "cookies": {
    "sessioncookie": "123456789"
  }
}
.
2 passed in 5.36s