Pretenders: Fake servers for testing

raw JSON →
1.4.5 verified Fri May 01 auth: no python

Pretenders provides fake implementations of external services (HTTP, SMTP, IMAP) for testing. Version 1.4.5 is the latest stable release; the project is maintained with irregular releases.

pip install pretenders
error ImportError: No module named 'pretenders'
cause The package is not installed.
fix
Run 'pip install pretenders'
error AttributeError: module 'pretenders' has no attribute 'http'
cause Using import pretenders and trying to access submodule incorrectly.
fix
Use from pretenders.http import HTTPMock instead of import pretenders then pretenders.http
deprecated Pretenders 1.4.x no longer supports Python 2.7; use Python 3.4+.
fix Upgrade your environment to Python 3.4 or higher.
gotcha For SMTP mock, the popular 'smtp' subpackage is not available as of version 1.4. SMTP mock is unsupported according to release notes.
fix Use the HTTP mock for testing, or consider other tools for SMTP testing.
gotcha The mock URL returned by HTTPMock may have a trailing path stripped incorrectly. Reported in issue #121 fixed in 1.4.
fix Upgrade to 1.4 or later.

Starts a mock HTTP server, defines a mock response, and tests an HTTP call.

from pretenders.http import HTTPMock, PretenderServer

# Start a mock HTTP server
server = PretenderServer(host='127.0.0.1', port=8888)
server.start()

# Create a mock
mock = HTTPMock('http://127.0.0.1:8888')
mock.when('GET /api').respond(200, body='{"status": "ok"}')

# Test your code
import requests
response = requests.get('http://127.0.0.1:8888/api')
print(response.json())

# Cleanup
server.stop()