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.
pip install pretendVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to create basic stubs with attributes, methods, and methods that raise exceptions using `pretend.stub` and `pretend.raiser`. Note that functions provided to stubs should not expect a `self` argument.
Define stubbed functions as `lambda *args, **kwargs: ...` or regular functions that explicitly don't take `self`. For example, `stub(method=lambda: 'result')` instead of `stub(method=lambda self: 'result')`.
Avoid searching for a `pretend.patch` function. Instead, refactor your code to accept dependencies as arguments, or integrate `pretend` stubs with your test runner's mocking/patching capabilities.
Ensure all required attributes and methods are explicitly defined when creating your `pretend.stub`. If you need to record calls or assert interactions, use `pretend.call_recorder` for specific methods.
When initializing the stub, ensure all expected methods and attributes are provided. Example: `my_stub = stub(method_name=lambda: 'value', attribute_name='data')`.
Remove 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')`.
Wrap the call to the stub method (that uses `raiser`) in a `try...except` block to handle the expected exception in your test.
Install the library using pip: `pip install pretend`.
No dependency data recorded yet.