Skip to main content

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.

Metadata

Release files for python-spymock 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-spymock 0.3.0
File Size Uploaded Provenance Actions
python-spymock-0.3.0.tar.gz 4.1 kB Without provenance Details for python-spymock-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-spymock 0.3.0
File Interpreter ABI Platform Actions
python_spymock-0.3.0-py3-none-any.whl Python 3 none any Details for python_spymock-0.3.0-py3-none-any.whl

Total release size: 8.2 kB

Release files / python-spymock-0.3.0.tar.gz

Download URL python-spymock-0.3.0.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
adad41ff7d67ca7256bc9d07137e3669846b95c0d0abe052dde9fbfa332406f5
BLAKE2b-256 checksum
How to use checksums
4a70404a0606d834b002f2e6f4fdacb968e9a17ab70535c64e341c524b3ecfcb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1032-azure

Release files / python_spymock-0.3.0-py3-none-any.whl

Download URL python_spymock-0.3.0-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17e316354dd7be54edc69ebcfd2384be0764792f507f8591ccfb6c1c2d3b4305
BLAKE2b-256 checksum
How to use checksums
cc1a58860a03801bebf0e45b95148e9472d30aad6cb1a74e3b1a954ff3135fd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1032-azure

Security for python-spymock 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page