This release is a pre-release and may not be stable for production use.
Spyfi
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)
| File | Size | Uploaded | |
|---|---|---|---|
| spyfi-1.0.0a0.tar.gz | 5.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|