httmock alternatives and similar packages
Based on the "Mock" category.
Alternatively, view httmock alternatives based on common mentions on social networks and blogs.
-
Moto
A library that allows you to easily mock out tests based on AWS infrastructure. -
VCR.py
Automatically mock your HTTP interactions to simplify and speed up testing -
httpretty
Intercept HTTP requests at the Python socket level. Fakes the whole socket module -
Mocket
a socket mock framework - for all kinds of socket animals, web-clients included -
python-libfaketime
A fast time mocking alternative to freezegun that wraps libfaketime. -
Mock Generator
A tool to auto generate the basic mocks and asserts for faster unit testing -
Pytest Mock Generator
A pytest fixture wrapper for https://pypi.org/project/mock-generator
Collect and Analyze Billions of Data Points in Real Time
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of httmock or a related project?
Popular Comparisons
README
httmock
A mocking library for requests
for Python 2.7 and 3.4+.
Installation
pip install httmock
Or, if you are a Gentoo user:
emerge dev-python/httmock
Usage
You can use it to mock third-party APIs and test libraries that use requests
internally, conditionally using mocked replies with the urlmatch
decorator:
from httmock import urlmatch, HTTMock
import requests
@urlmatch(netloc=r'(.*\.)?google\.com$')
def google_mock(url, request):
return 'Feeling lucky, punk?'
with HTTMock(google_mock):
r = requests.get('http://google.com/')
print r.content # 'Feeling lucky, punk?'
The all_requests
decorator doesn't conditionally block real requests. If you return a dictionary, it will map to the requests.Response
object returned:
from httmock import all_requests, HTTMock
import requests
@all_requests
def response_content(url, request):
return {'status_code': 200,
'content': 'Oh hai'}
with HTTMock(response_content):
r = requests.get('https://foo_bar')
print r.status_code
print r.content
If you pass in Set-Cookie
headers, requests.Response.cookies
will contain the values. You can also use response
method directly instead of returning a dict:
from httmock import all_requests, response, HTTMock
import requests
@all_requests
def response_content(url, request):
headers = {'content-type': 'application/json',
'Set-Cookie': 'foo=bar;'}
content = {'message': 'API rate limit exceeded'}
return response(403, content, headers, None, 5, request)
with HTTMock(response_content):
r = requests.get('https://api.github.com/users/whatever')
print r.json().get('message')
print r.cookies['foo']