Code Monkey home page Code Monkey logo

python-mocket's Introduction

mocket /mɔˈkɛt/

image

image

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

image

image

image

image

image

image

image

image

python-mocket's People

Contributors

mindflayer avatar z4r avatar foobared avatar bootstraponline avatar

Watchers

James Cloos avatar songz avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.