Pytest plugin for capturing and mocking connection requests.
Project description
This package provides a plugin for pytest framework for capturing and mocking connection requests during the test run.
Inspired by pook and pytest-responses.
Get started using the documentation and getting-started.
🔌Installation
$ pip install pytest-remote-response
or
$ git clone https://github.com/devanshshukla99/pytest-remote-response $ cd pytest-remote-response $ pip install .
The plugin will register automatically with pytest framework and will be ready to use.
💁🏻♀️Supported Clients
Currently, pytest-remote-response supports,
💨Usage
🐍🧪Pytest plugin
The plugin works by applying monkeypatches of interceptors for different libraries using a wrapper response.activate. The interceptors when applied can capture, prevent or mock the connection request.
The available interceptors are listed in response.available method.
Example of using the decorator:
import urllib3 from pytest_response import response response.configure(remote=True, capture=True, response=False) @response.activate("urllib3") def get_url(): http = urllib3.PoolManager() url = "https://www.python.org" # Since the interceptors are in response mode, the response data and headers # will be spoofed with saved data in the database; # if the query comes back empty, this request will # error out with :class:`pytest_response.exceptions.ResponseNotFound` res = http.request("GET", url) assert res.status == 200 assert res.data
Handling requests:
- Block remote requests:
- all requests are allowed by default; one can disable them using --remote-block flag
$ pytest --remote-block
- Capture remote requests:
- the requests can be captured in a sqlite3 database using --remote-capture arg
$ pytest --remote-capture
- Mock remote requests:
- the requests can be mocked using --remote-response
$ pytest --remote-response
🐱👤Standalone package
The tools implemented in this package can be easily ported to any other application, with mimial config required.
Configuration:
from pytest_response import response response.setup_database({DUMP FILE}) response.post({INTERCEPTOR}) ... response.unpost()
🧪 Testing
Use tox to make sure the plugin is working:
$ git clone https://github.com/devanshshukla99/pytest-remote-response $ cd pytest-remote-response $ tox -e py38
See tox for more info.
Licence
This plugin is licenced under a 3-clause BSD style licence - see the LICENCE file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pytest-remote-response-2.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cd8ef5a5117a682317cd4232ba654ba827989ac52da0c44ff0b29d7cfd853f2 |
|
MD5 | f4306a08a88dab6f948bc7907320b859 |
|
BLAKE2-256 | be6eb7d718a2a18a4c57a24f63801be8858305bb6ade1858167f1343b08a580c |
Hashes for pytest_remote_response-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b2f2806430e0174868383e8281e01d18c76e80ff0542a8e2e08b37072fa607f |
|
MD5 | 8b285f4464f4791747b791ac6ddb2eff |
|
BLAKE2-256 | 6bfd3137e10aff56827e083e42284bf9c229e4305642c4793d6b3d660c4e7cbf |