Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Spyfi

PyPI Status Python Version License

Read the documentation at https://spyfi.readthedocs.io/ Tests Codecov

pre-commit Black

A quick and dirty way to turn your existing classes into spies.

Why though?

I very often create spies for my tests by wrapping an interface around a list, eg.

class FakeEmailSender(list):

    def send(self, address: str, message: str) -> None:
        self.append((address, message))


def test_when_a_customer_signs_up():

    sender = FakeEmailSender()
    handler = SignupHandler(sender)

    handler("user@domain.com", "password")

    assert (("user@domain.com", "welcome to the website")) in sender

Sometimes this is a little fiddly, particularly if you need to spy on a hierarchy of objects. Spyfi, pronounced "spiffy", is a quick way to instrument a python object graph and capture calls made to it.

Installation

You can install Spyfi via pip from PyPI:

$ pip install spyfi

Usage

from spyfi import Spy


class Thing:

    def __init__(self, colour):
        self.colour = colour

    def say_hello(self, message):
        print(f"Hello, I am a {self.colour} thing: {message})


class ThingFactory:

    def make_thing(self, colour:str) -> Thing:
        return Thing(colour)


def test_thing_messages():

    # Spiffy takes any old object and wraps its methods
    # so that an arbitrary callback receives args and kwargs.
    # In this case, we're appending all calls to a list.
    spy = Spy(ThingFactory())

    # The returned object is otherwise unchanged. `factory` is a real
    # ThingFactory and behaves as normal.
    factory = spy.target
    factory.make_thing("blue").say_hello("I like python")

    # Since we have access to the calls list, we can assert that
    # particular methods were called with the right data.
    assert len(spy.calls) == 2
    assert calls[0].method == "make_thing"
    assert calls[0].args == ("blue",)

    # Spyfi includes a helper method to make assertions easier
    assert spy.has("say_hello")
    assert spy.has("say_hello", "I like python")

Contributing

Contributions are very welcome. To learn more, see the Contributor Guide.

License

Distributed under the terms of the MIT license, Spyfi is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This project was generated from @cjolowicz's Hypermodern Python Cookiecutter template.

Release files for spyfi 1.0.0a0

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

Source distribution (sdist)

Source distribution for spyfi 1.0.0a0
File Size Uploaded
spyfi-1.0.0a0.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spyfi 1.0.0a0
File Interpreter ABI Platform
spyfi-1.0.0a0-py3-none-any.whl Python 3 none any Details

Total release size: 10.0 kB

Release files / spyfi-1.0.0a0.tar.gz

Download URL spyfi-1.0.0a0.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d441da44d9079e78162ce269c6489bf1937f758ccf3869d9142b358e3090ea80
BLAKE2b-256 checksum
How to use checksums
8de8e71b00d362dde65fbb0ce3f85a2bf1c4b68ec8c036830eac8ba37235d3ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / spyfi-1.0.0a0-py3-none-any.whl

Download URL spyfi-1.0.0a0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de6c2d0167602ab62bd6de6b66fa77ccb7ba43dd3d0fee7dfea6d73b285321c2
BLAKE2b-256 checksum
How to use checksums
4aaf18b40d35339e2c0e17f13997f22e253befcd2317e51959d0a2d942a519db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release history Release notifications | RSS feed

This release

1.0.0a0 This release

2 release files

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