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 details)

Uploaded Source

Built Distribution

spyfi-1.0.0a0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file spyfi-1.0.0a0.tar.gz.

File metadata

  • Download URL: spyfi-1.0.0a0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for spyfi-1.0.0a0.tar.gz
Algorithm Hash digest
SHA256 d441da44d9079e78162ce269c6489bf1937f758ccf3869d9142b358e3090ea80
MD5 46887192450f4439192c07274b53bccb
BLAKE2b-256 8de8e71b00d362dde65fbb0ce3f85a2bf1c4b68ec8c036830eac8ba37235d3ee

See more details on using hashes here.

File details

Details for the file spyfi-1.0.0a0-py3-none-any.whl.

File metadata

  • Download URL: spyfi-1.0.0a0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for spyfi-1.0.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 de6c2d0167602ab62bd6de6b66fa77ccb7ba43dd3d0fee7dfea6d73b285321c2
MD5 9def869d9417565702c17cfb8056506d
BLAKE2b-256 4aaf18b40d35339e2c0e17f13997f22e253befcd2317e51959d0a2d942a519db

See more details on using hashes here.

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