If you use custom queries, try httmock . It is wonderfully simple and elegant:
from httmock import urlmatch, HTTMock import requests # define matcher: @urlmatch(netloc=r'(.*\.)?google\.com$') def google_mock(url, request): return 'Feeling lucky, punk?' # open context to patch with HTTMock(google_mock): # call requests r = requests.get('http://google.com/') print r.content # 'Feeling lucky, punk?'
If you want something more general (like mocking a library using http calls, for example ), go to httpretty .
Almost as elegant:
import requests import httpretty @httpretty.activate def test_one(): # define your patch: httpretty.register_uri(httpretty.GET, "http://yipit.com/", body="Find the best daily deals") # use! response = requests.get('http://yipit.com') assert response.text == "Find the best daily deals"
HTTPretty is much more functional - it also offers a mocking status code, streaming responses, rotating responses, dynamic responses (with a callback).
Marek Brzóska Aug 28 '13 at 14:19 2013-08-28 14:19
source share