Code Monkey home page Code Monkey logo

spyfi's Introduction

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("[email protected]", "password")

    assert (("[email protected]", "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.

spyfi's People

Contributors

bobthemighty avatar dependabot[bot] avatar

Stargazers

Ed Blackburn avatar Egidijus Ligeika avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.