Skip to main content

Spyfi

Project description

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.

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

spyfi-1.0.0a0.tar.gz (5.3 kB view hashes)

Uploaded Source

Built Distribution

spyfi-1.0.0a0-py3-none-any.whl (4.6 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