Socket
Socket
Sign inDemoInstall

pybond

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pybond

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


Maintainers
1

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.

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc