Help you mock HTTP call and generate mock code
Project description
# Installation
```
pip install pytest-mock-helper
```
# Usage
In develop you need those enviroments:
```
export PYTEST_NETWORK_MOCK=develop
export PYTEST_NETWORK_FILEPATH=/tmp
```
For example you have a test file called test_request.py:
```
# -*- coding: utf-8 -*-
import requests
def test_requests_send(capsys):
requests.get("https://httpbin.org")
```
Run with `--blockhtp`
```
$ pytest tests/test_requests.py --blockhttp
======================================================== test session starts =========================================================
platform darwin -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /Users/laixintao/Program/pytest-network-mock-helper, inifile:
plugins: mock-helper-0.0.2
collected 1 item
tests/test_requests.py . [100%]
====================================================== 1 passed in 1.38 seconds ======================================================
Copy the following code to mock http call
@httmock.urlmatch(netloc="httpbin.org", path="/")
def mock_item(url, request):
return test_utils.fake_response(
rel_path="/tmp/httpbin_org0",
url="https://httpbin.org/",
status_code=200
)
```
Also a downloaded file will be saved to your $PYTEST_NETOWRK_FILEPATH:
```
$ ls /tmp/httpbin_org0
ls /tmp/httpbin_org0
```
## In Production
The test server can't send any HTTP request by default:
```
pytest tests/test_requests.py --blockhttp
======================================================== test session starts =========================================================
platform darwin -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /Users/laixintao/Program/pytest-network-mock-helper, inifile:
plugins: mock-helper-0.0.2
collected 1 item
tests/test_requests.py F [100%]
============================================================== FAILURES ==============================================================
_________________________________________________________ test_requests_send _________________________________________________________
capsys = <_pytest.capture.CaptureFixture object at 0x10a3c3898>
def test_requests_send(capsys):
> requests.get("https://httpbin.org")
tests/test_requests.py:7:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../.virtualenvs/pytest-plugin/lib/python3.6/site-packages/requests/api.py:72: in get
return request('get', url, params=params, **kwargs)
../../.virtualenvs/pytest-plugin/lib/python3.6/site-packages/requests/api.py:58: in request
return session.request(method=method, url=url, **kwargs)
../../.virtualenvs/pytest-plugin/lib/python3.6/site-packages/requests/sessions.py:508: in request
resp = self.send(prep, **send_kwargs)
../../.virtualenvs/pytest-plugin/lib/python3.6/site-packages/requests/sessions.py:618: in send
r = adapter.send(request, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <requests.adapters.HTTPAdapter object at 0x10a3c3e80>, request = <PreparedRequest [GET]>, args = ()
kwargs = {'cert': None, 'proxies': OrderedDict(), 'stream': False, 'timeout': None, ...}
parsed = ParseResult(scheme='https', netloc='httpbin.org', path='/', params='', query='', fragment=''), netloc = 'httpbin.org'
path = '/'
def whitelisted(self, request, *args, **kwargs):
global summaries
parsed = urlparse(request.url)
netloc = parsed.netloc
path = parsed.path
if isinstance(netloc, str) and netloc in whitelist:
return self.old_send(request, *args, **kwargs)
if not is_develop:
logger.warning('Denied HTTP connection to: %s' % netloc)
> raise MockHttpCall(netloc)
E pytest_mock_helper.MockHttpCall: httpbin.org
pytest_mock_helper.py:53: MockHttpCall
-------------------------------------------------------- Captured stderr call --------------------------------------------------------
pytest_mock_helper.py
```
```
pip install pytest-mock-helper
```
# Usage
In develop you need those enviroments:
```
export PYTEST_NETWORK_MOCK=develop
export PYTEST_NETWORK_FILEPATH=/tmp
```
For example you have a test file called test_request.py:
```
# -*- coding: utf-8 -*-
import requests
def test_requests_send(capsys):
requests.get("https://httpbin.org")
```
Run with `--blockhtp`
```
$ pytest tests/test_requests.py --blockhttp
======================================================== test session starts =========================================================
platform darwin -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /Users/laixintao/Program/pytest-network-mock-helper, inifile:
plugins: mock-helper-0.0.2
collected 1 item
tests/test_requests.py . [100%]
====================================================== 1 passed in 1.38 seconds ======================================================
Copy the following code to mock http call
@httmock.urlmatch(netloc="httpbin.org", path="/")
def mock_item(url, request):
return test_utils.fake_response(
rel_path="/tmp/httpbin_org0",
url="https://httpbin.org/",
status_code=200
)
```
Also a downloaded file will be saved to your $PYTEST_NETOWRK_FILEPATH:
```
$ ls /tmp/httpbin_org0
ls /tmp/httpbin_org0
```
## In Production
The test server can't send any HTTP request by default:
```
pytest tests/test_requests.py --blockhttp
======================================================== test session starts =========================================================
platform darwin -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /Users/laixintao/Program/pytest-network-mock-helper, inifile:
plugins: mock-helper-0.0.2
collected 1 item
tests/test_requests.py F [100%]
============================================================== FAILURES ==============================================================
_________________________________________________________ test_requests_send _________________________________________________________
capsys = <_pytest.capture.CaptureFixture object at 0x10a3c3898>
def test_requests_send(capsys):
> requests.get("https://httpbin.org")
tests/test_requests.py:7:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../.virtualenvs/pytest-plugin/lib/python3.6/site-packages/requests/api.py:72: in get
return request('get', url, params=params, **kwargs)
../../.virtualenvs/pytest-plugin/lib/python3.6/site-packages/requests/api.py:58: in request
return session.request(method=method, url=url, **kwargs)
../../.virtualenvs/pytest-plugin/lib/python3.6/site-packages/requests/sessions.py:508: in request
resp = self.send(prep, **send_kwargs)
../../.virtualenvs/pytest-plugin/lib/python3.6/site-packages/requests/sessions.py:618: in send
r = adapter.send(request, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <requests.adapters.HTTPAdapter object at 0x10a3c3e80>, request = <PreparedRequest [GET]>, args = ()
kwargs = {'cert': None, 'proxies': OrderedDict(), 'stream': False, 'timeout': None, ...}
parsed = ParseResult(scheme='https', netloc='httpbin.org', path='/', params='', query='', fragment=''), netloc = 'httpbin.org'
path = '/'
def whitelisted(self, request, *args, **kwargs):
global summaries
parsed = urlparse(request.url)
netloc = parsed.netloc
path = parsed.path
if isinstance(netloc, str) and netloc in whitelist:
return self.old_send(request, *args, **kwargs)
if not is_develop:
logger.warning('Denied HTTP connection to: %s' % netloc)
> raise MockHttpCall(netloc)
E pytest_mock_helper.MockHttpCall: httpbin.org
pytest_mock_helper.py:53: MockHttpCall
-------------------------------------------------------- Captured stderr call --------------------------------------------------------
pytest_mock_helper.py
```
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file pytest_mock_helper-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: pytest_mock_helper-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5adffffaee0f5134286da3050251b3677bc65da3ee829a9bba6754437bae615c |
|
MD5 | b1f7da602176b770988f19e50c6ae1e9 |
|
BLAKE2b-256 | 66742c1ec8b028df6f3a1b98458e9961ef547f702a92be9d98b4f9dace1d2158 |