Skip to main content

No project description provided

Project description

spymock

PyPI PyPI - License PyPI - Python Version Test

This library provides SpyMock which is similar to MagicMock but recording function return values and exceptions on call_values_or_exceptions attribute.

Installation

pip install python-spymock

Usage

Use spymock.spy function as-like patch.object to spy and mock the target attribute like:

import urllib.request

from spymock import spy


def request():
    url = "http://httpbin.org/json"
    req = urllib.request.Request(url)
    with urllib.request.urlopen(req) as res:
        return json.loads(res.read())


def test_request_with_spy():
    with spy(urllib.request, "urlopen") as s:
        assert request() == {
            "slideshow": {
                "author": "Yours Truly",
                "date": "date of publication",
                "slides": [
                    {"title": "Wake up to WonderWidgets!", "type": "all"},
                    {
                        "items": [
                            "Why <em>WonderWidgets</em> are great",
                            "Who <em>buys</em> WonderWidgets",
                        ],
                        "title": "Overview",
                        "type": "all",
                    },
                ],
                "title": "Sample Slide Show",
            }
        }

        # 's' is like MagicMock but it has 'call_values_or_exceptions' attribute
        assert len(s.call_values_or_exceptions) == 1

        r = s.call_values_or_exceptions[0]
        assert isinstance(r, HTTPResponse)
        assert r.status == 200
        assert r.reason == "OK"

Or directly create spymock.SpyMock instance as-like MagicMock like:

import urllib.request

from spymock import SpyMock


def request():
    url = "http://httpbin.org/json"
    req = urllib.request.Request(url)
    with urllib.request.urlopen(req) as res:
        return json.loads(res.read())


def test_request_with_spymock():
    s = SpyMock(request)
    assert s() == {
        "slideshow": {
            "author": "Yours Truly",
            "date": "date of publication",
            "slides": [
                {"title": "Wake up to WonderWidgets!", "type": "all"},
                {
                    "items": [
                        "Why <em>WonderWidgets</em> are great",
                        "Who <em>buys</em> WonderWidgets",
                    ],
                    "title": "Overview",
                    "type": "all",
                },
            ],
            "title": "Sample Slide Show",
        }
    }

    # 's' is like MagicMock but it has 'call_values_or_exceptions' attribute
    assert len(s.call_values_or_exceptions) == 1

    r = s.call_values_or_exceptions[0]
    assert r == {
        "slideshow": {
            "author": "Yours Truly",
            "date": "date of publication",
            "slides": [
                {"title": "Wake up to WonderWidgets!", "type": "all"},
                {
                    "items": [
                        "Why <em>WonderWidgets</em> are great",
                        "Who <em>buys</em> WonderWidgets",
                    ],
                    "title": "Overview",
                    "type": "all",
                },
            ],
            "title": "Sample Slide Show",
        }
    }

License

Distributed under the terms of the MIT license.

Project details


Download files

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

Source Distribution

python-spymock-0.3.0.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

python_spymock-0.3.0-py3-none-any.whl (4.0 kB view hashes)

Uploaded Python 3

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