Skip to main content

pybond

Build codecov

pybond is a spying and stubbing library inspired heavily by the clojure bond library.

Installation

pip

pip install pybond==0.2.2

requirements.txt

pybond==0.2.2

pyproject.toml

pybond = "0.2.2"

Example usage

Let's say you wanted to test the functions in this module:

# /sample_code/my_module.py
from typing import Any

import sample_code.other_package as other_package


def foo(x: Any) -> None:
    response = other_package.make_a_network_request(x)
    other_package.write_to_disk(response)
    return response


def bar(x: Any) -> None:
    return foo(x)

With pybond you can easily spy on any given function or stub out functions that perform IO:

# /tests/test_my_module.py
from pybond import calls, called_with_args, spy, stub, times_called

import sample_code.other_package as other_package
import sample_code.my_module as my_module
from sample_code.my_module import bar


def test_foo_is_called():
    with spy(my_module.foo):
        assert times_called(my_module.foo, 0)
        bar(42)
        assert times_called(my_module.foo, 1)
        bar(42)
        bar(42)
        assert times_called(my_module.foo, 3)


def test_bar_handles_response():
    with stub(
        (other_package.make_a_network_request, lambda x: {"result": x * 2}),
        (other_package.write_to_disk, lambda _: None),
    ), spy(
        my_module.foo,
    ):
        assert times_called(my_module.foo, 0)
        assert times_called(other_package.make_a_network_request, 0)
        assert bar(21) == {"result": 42}
        assert times_called(my_module.foo, 1)
        assert times_called(other_package.make_a_network_request, 1)
        assert called_with_args(my_module.foo, args=[21])
        assert bar(20) == {"result": 40}
        assert calls(my_module.foo) == [
            {
                "args": [21],
                "kwargs": None,
                "return": {"result": 42},
                "error": None,
            },
            {
                "args": [20],
                "kwargs": None,
                "return": {"result": 40},
                "error": None,
            },
        ]
        assert calls(other_package.write_to_disk) == [
            {
                "args": [{"result": 42}],
                "kwargs": None,
                "return": None,
                "error": None,
            },
            {
                "args": [{"result": 40}],
                "kwargs": None,
                "return": None,
                "error": None,
            },
        ]

License

Distributed under the Eclipse Public License.

Metadata

Release files for pybond 0.2.2

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

Source distribution (sdist)

Source distribution for pybond 0.2.2
File Size Uploaded
pybond-0.2.2.tar.gz 5.2 kB Details

Built distribution (wheel)

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

Total release size: 11.5 kB

Release files / pybond-0.2.2.tar.gz

Download URL pybond-0.2.2.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
bc9275dcdb0ccba2bb48995df39fd702cd7d057ba77ab7f411d6ca35ee256bde
BLAKE2b-256 checksum
How to use checksums
189f17dc6bd9450c161c9b9051eac4cb0f1b037d50100dd33e249b832108cc92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.8 Darwin/23.3.0

Release files / pybond-0.2.2-py3-none-any.whl

Download URL pybond-0.2.2-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2f75464596cff9b8ed7bb67f56624e9196039fe6d0bee39ce6b1b0d87b26cac
BLAKE2b-256 checksum
How to use checksums
adbb8b98ed990917e1bb9e82f4aa326a9d0d54477bebbbf39f486c73519fa726
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.8 Darwin/23.3.0

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

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