Skip to content

bunpy.mock

See the Mocking guide for a full walkthrough.

from bunpy.mock import fn, spyOn

Quick reference

FunctionDescription
fn(impl=None)Create a mock function
spyOn(obj, method)Wrap an existing method
module(name)Context manager to stub a module

Mock function API

m = fn(lambda x: x * 2)
m(5)           # 10
m.mock.calls   # [[5]]
m.mock.results # [10]
m.mock.call_count  # 1

m.mockReturnValue(42)
m.mockImplementation(lambda x: x + 1)
m.mockClear()
m.mockReset()

spyOn

spy = spyOn(os.path, "exists")
os.path.exists("/tmp")
spy.mock.call_count  # 1
spy.mockRestore()