Pretend: A Python Stubbing Library

1.0.9 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

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}")

view raw JSON →