Pretend: A Python Stubbing Library
Pretend is a lightweight Python library designed to simplify the creation of stubs for testing purposes. Stubs are objects that provide pre-canned responses, rather than performing actual computations, making tests more isolated and predictable. The library is currently at version 1.0.9 and, despite less frequent updates, is considered stable and actively maintained for its specific utility within testing frameworks.
Common errors
-
AttributeError: 'Stub' object has no attribute 'some_method'
cause You attempted to access a method or attribute on a `pretend.stub` object that was not explicitly defined when the stub was created.fixWhen initializing the stub, ensure all expected methods and attributes are provided. Example: `my_stub = stub(method_name=lambda: 'value', attribute_name='data')`. -
TypeError: <lambda>() missing 1 required positional argument: 'self'
cause A function defined as a method on a `pretend.stub` was written to expect a `self` argument, but `pretend` does not bind stub functions to the instance in this way.fixRemove the `self` argument from functions assigned to stub methods. Example: `my_stub = stub(do_something=lambda: 'result')` instead of `my_stub = stub(do_something=lambda self: 'result')`. -
ValueError: API error (or similar exception message)
cause You used `pretend.raiser` to make a stub method raise an exception, but the calling code did not include a `try...except` block to catch this expected exception.fixWrap the call to the stub method (that uses `raiser`) in a `try...except` block to handle the expected exception in your test. -
ModuleNotFoundError: No module named 'pretend'
cause The 'pretend' library is not installed in your current Python environment.fixInstall the library using pip: `pip install pretend`.
Warnings
- gotcha Functions defined for `pretend.stub` objects do not automatically receive a `self` argument. They should be defined as regular functions or lambdas that do not expect a first positional argument for the instance.
- gotcha `pretend` is a stubbing library and explicitly does not include a `patch` method for monkey-patching. It promotes passing stubs as arguments or relying on your testing framework's patching utilities (e.g., `pytest` fixtures, `unittest.mock.patch`).
- gotcha Stubs created with `pretend.stub` are strict; they only respond to attributes and methods explicitly defined during their creation. Attempting to access an undefined attribute will result in an `AttributeError`, unlike some mocking libraries that might dynamically create attributes.
Install
-
pip install pretend
Imports
- stub
from pretend import stub
- raiser
from pretend import raiser
- call_recorder, call
from pretend import call_recorder, call
Quickstart
from pretend import stub, raiser
# Create a stub with a simple attribute
user_stub = stub(country_code="US")
print(f"User country code: {user_stub.country_code}")
# Create a stub with a method (note: functions on stubs don't take 'self')
data_service = stub(fetch_data=lambda: {'id': 1, 'value': 'test'})
print(f"Fetched data: {data_service.fetch_data()}")
# Create a stub method that raises an exception
error_prone_api = stub(fail_action=raiser(ValueError('API error')))
try:
error_prone_api.fail_action()
except ValueError as e:
print(f"Caught expected error: {e}")