Mocket
raw JSON → 3.14.1 verified Fri May 01 auth: no python
Mocket is a socket mock framework for Python that allows you to mock socket connections and HTTP calls for testing purposes. It supports gevent, asyncio, and SSL, and provides a high-level API similar to httpretty. Current version is 3.14.1, with regular releases.
pip install mocket Common errors
error ImportError: cannot import name 'httpretty' from 'mocket' ↓
cause People try 'from mocket import httpretty' instead of using the plugin module.
fix
Use 'from mocket.plugins.httpretty import httpretty'.
error AttributeError: module 'mocket' has no attribute 'MocketEntry' ↓
cause Old versions or import path changed; MocketEntry is directly in mocket module.
fix
Ensure mocket version >= 3.0 and import as 'from mocket import MocketEntry'.
error RuntimeError: Mocket not initialized. Use @mocketize decorator. ↓
cause Attempting to use Mocket.register outside of a mocketized session.
fix
Wrap test function with @mocketize or use 'with mocketize():' block.
Warnings
gotcha Mocket replaces global socket functions; ensure tests are isolated and mocketize is scoped properly to avoid leakage. ↓
fix Use @mocketize decorator or 'with mocketize()' context manager to automatically enable/disable mocks.
gotcha Mocket's httpretty plugin is not compatible with the standalone httpretty library; import from mocket.plugins.httpretty not httpretty directly. ↓
fix Use 'from mocket.plugins.httpretty import httpretty' instead of 'import httpretty'.
deprecated The 'strict' mode and custom request-matching via callables are new in 3.13.x; old patterns may not work. ↓
fix Review changelog for 3.13.9 and 3.13.11 for strict mode and custom can_handle logic.
breaking Fixture 'event_loop' removed in pytest-asyncio; Mocket 3.13.8 addresses this but tests relying on old event_loop may break. ↓
fix Upgrade to latest mocket and ensure pytest-asyncio >=0.23.
Imports
- Mocket
from mocket import Mocket - MocketEntry
from mocket import MocketEntry - mocketize
from mocket import mocketize - http.mock wrong
import httprettycorrectfrom mocket.plugins.httpretty import httpretty
Quickstart
from mocket import mocketize, MocketEntry, Mocket
@mocketize
def test_http_get():
url = "http://example.com/"
Mocket.register(MocketEntry(url, body="mock response"))
import urllib.request
response = urllib.request.urlopen(url)
assert response.read() == b"mock response"