Package: pytest-mock-helper
# Installation```pip install pytest-mock-helper```# UsageIn develop you need those enviroments:```export PYTEST_NETWORK_MOCK=developexport PYTEST_NETWORK_FILEPATH=/tmp```For example you have a test file called test_request.py:```# -*- coding: utf-8 -*-import requestsdef 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.0rootdir: /Users/laixintao/Program/pytest-network-mock-helper, inifile:plugins: mock-helper-0.0.2collected 1 itemtests/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_org0ls /tmp/httpbin_org0```## In ProductionThe 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.0rootdir: /Users/laixintao/Program/pytest-network-mock-helper, inifile:plugins: mock-helper-0.0.2collected 1 itemtests/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 getreturn request('get', url, params=params, **kwargs)../../.virtualenvs/pytest-plugin/lib/python3.6/site-packages/requests/api.py:58: in requestreturn session.request(method=method, url=url, **kwargs)../../.virtualenvs/pytest-plugin/lib/python3.6/site-packages/requests/sessions.py:508: in requestresp = self.send(prep, **send_kwargs)../../.virtualenvs/pytest-plugin/lib/python3.6/site-packages/requests/sessions.py:618: in sendr = 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 summariesparsed = urlparse(request.url)netloc = parsed.netlocpath = parsed.pathif 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.orgpytest_mock_helper.py:53: MockHttpCall-------------------------------------------------------- Captured stderr call --------------------------------------------------------pytest_mock_helper.py```