Skip to main content

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

Project description

https://api.travis-ci.org/mindflayer/python-mocket.svg?branch=master https://coveralls.io/repos/github/mindflayer/python-mocket/badge.svg?branch=master Code Climate

A socket mock framework

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

How to use it

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:

Please also have a look at the huge test suite:

Installation

Using pip:

$ pip install mocket

Issues

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 example.py file as the following one:

import json

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


@pytest.fixture
def response():
    return {
        "integer": 1,
        "string": "asd",
        "boolean": False,
    }


@mocketize
def test_json(response):
    url_to_mock = 'https://testme.org/json'

    Entry.single_register(
        Entry.GET,
        url_to_mock,
        body=json.dumps(response),
        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 example.py

Works well with others

Using Mocket as pook engine:

$ pip install mocket[pook]

Example:

import pook
from mocket.plugins.pook_mock_engine import MocketEngine

pook.activate()

pook.set_mock_engine(MocketEngine)

url = 'http://twitter.com/api/1/foobar'
status = 404
response_json = {'error': 'foo'}

mock = pook.get(
    url,
    headers={'content-type': 'application/json'},
    reply=status,
    response_json=response_json,
)
mock.persist()

requests.get(url)
assert mock.calls == 1

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

pook.disable()

First appearance

EuroPython 2013, Florence

http://badge.kloud51.com/pypi/v/mocket.svg http://badge.kloud51.com/pypi/d/mocket.svg http://badge.kloud51.com/pypi/w/mocket.svg http://badge.kloud51.com/pypi/e/mocket.svg http://badge.kloud51.com/pypi/l/mocket.svg http://badge.kloud51.com/pypi/f/mocket.svg http://badge.kloud51.com/pypi/py_versions/mocket.svg http://badge.kloud51.com/pypi/s/mocket.svg

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mocket-1.7.6.tar.gz (17.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page