skip to navigation
skip to content

mocket 2.1.0

Socket Mock Framework - for all kinds of socket animals, web-clients included - with gevent/asyncio/SSL support

mocket /mɔˈkɛt/

.. image::

.. image::

.. image::
:alt: Code Climate

.. image::
:alt: Requirements Status

A socket mock framework
for all kinds of socket *animals*, web-clients included - with gevent/asyncio/SSL support

How to use it
Read these two blog posts if you want to have a big picture of what *Mocket* is capable of:


The starting point to understand how to use *Mocket* to write a custom mock is the following example:


Next step, you are invited to have a look at both the implementation of the two mocks we provide:

- HTTP mock (similar to HTTPretty) -
- Redis mock (basic implementation) -

Please also have a look at the huge test suite:

- Tests module at

Using pip::

$ pip install mocket

When opening an **Issue**, please add few lines of code as failing test, or -better- open its relative **Pull request** adding this test to our test suite.

Quick example of its HTTP mock
Let's create a new virtualenv with all we need::

$ virtualenv example
$ source example/bin/activate
$ pip install pytest requests mocket

As second step, we create an `` file as the following one:

.. code-block:: python

import json

from mocket import mocketize
from mocket.mockhttp import Entry
import requests
import pytest

def response():
return {
"integer": 1,
"string": "asd",
"boolean": False,

def test_json(response):
url_to_mock = ''

headers={'content-type': 'application/json'}

mocked_response = requests.get(url_to_mock).json()

assert response == mocked_response

Let's fire our example test::

$ py.test

What about the other socket animals?
Using *Mocket* with asyncio based clients::

$ pip install aiohttp


.. code-block:: python

class AioHttpEntryTestCase(TestCase):
def test_http_session(self):
url = ''
body = "asd" * 100
Entry.single_register(Entry.GET, url, body=body, status=404)
Entry.single_register(Entry.POST, url, body=body*2, status=201)

async def main(l):
async with aiohttp.ClientSession(loop=l) as session:
with async_timeout.timeout(3):
async with session.get(url) as get_response:
assert get_response.status == 404
assert await get_response.text() == body

with async_timeout.timeout(3):
async with, data=body * 6) as post_response:
assert post_response.status == 201
assert await post_response.text() == body * 2

loop = asyncio.get_event_loop()

Works well with others
Using *Mocket* as pook_ engine::

$ pip install mocket[pook]

.. _pook:


.. code-block:: python

import pook
from mocket.plugins.pook_mock_engine import MocketEngine



url = ''
status = 404
response_json = {'error': 'foo'}

mock = pook.get(
headers={'content-type': 'application/json'},

assert mock.calls == 1

resp = requests.get(url)
assert resp.status_code == status
assert resp.json() == response_json
assert mock.calls == 2

First appearance
EuroPython 2013, Florence

- Video:
- Slides:
- Slides as PDF:
File Type Py Version Uploaded on Size
mocket-2.1.0.tar.gz (md5) Source 2017-10-15 22KB