Registry / testing / pretend

pretend

JSON →
library1.0.9pypypi✓ verified 87d ago

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 pretend
INSTALL
IMPORT
SIG · PRETEND
P
pretend
testingpythonv1.0.9
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.9 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

stub
from pretend import stub
Used to create a basic stub object with defined attributes and methods.
raiser
from pretend import raiser
A helper function to create a callable that raises a specified exception.
call_recorder, call
from pretend import call_recorder, call
Used for recording calls made to a stubbed function, if behavior verification is needed.

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.

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}")
Debug
Known issues
gotchaFunctions 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.
fix
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')`.
affects: All versions
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`).
fix
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.
affects: All versions
gotchaStubs 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.
fix
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.
affects: All versions
Errors
Common errors & fixes
AttributeError: 'Stub' object has no attribute 'some_method'
You attempted to access a method or attribute on a `pretend.stub` object that was not explicitly defined when the stub was created.
fix
When 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'
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.
fix
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')`.
ValueError: API error (or similar exception message)
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.
fix
Wrap 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'
The 'pretend' library is not installed in your current Python environment.
fix
Install the library using pip: `pip install pretend`.
Upgrade
Version history
1.0.9latest on PyPI · released Apr 14, 2018
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
12
Resources
pretend — pip install pretend · libregistry