{"id":7513,"library":"pretend","title":"Pretend: A Python Stubbing Library","description":"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.","status":"active","version":"1.0.9","language":"en","source_language":"en","source_url":"https://github.com/alex/pretend","tags":["testing","stubbing","test doubles","mocking"],"install":[{"cmd":"pip install pretend","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"Used to create a basic stub object with defined attributes and methods.","symbol":"stub","correct":"from pretend import stub"},{"note":"A helper function to create a callable that raises a specified exception.","symbol":"raiser","correct":"from pretend import raiser"},{"note":"Used for recording calls made to a stubbed function, if behavior verification is needed.","symbol":"call_recorder, call","correct":"from pretend import call_recorder, call"}],"quickstart":{"code":"from pretend import stub, raiser\n\n# Create a stub with a simple attribute\nuser_stub = stub(country_code=\"US\")\nprint(f\"User country code: {user_stub.country_code}\")\n\n# Create a stub with a method (note: functions on stubs don't take 'self')\ndata_service = stub(fetch_data=lambda: {'id': 1, 'value': 'test'})\nprint(f\"Fetched data: {data_service.fetch_data()}\")\n\n# Create a stub method that raises an exception\nerror_prone_api = stub(fail_action=raiser(ValueError('API error')))\ntry:\n    error_prone_api.fail_action()\nexcept ValueError as e:\n    print(f\"Caught expected error: {e}\")","lang":"python","description":"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."},"warnings":[{"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')`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"`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`).","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"When initializing the stub, ensure all expected methods and attributes are provided. Example: `my_stub = stub(method_name=lambda: 'value', attribute_name='data')`.","cause":"You attempted to access a method or attribute on a `pretend.stub` object that was not explicitly defined when the stub was created.","error":"AttributeError: 'Stub' object has no attribute 'some_method'"},{"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')`.","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.","error":"TypeError: <lambda>() missing 1 required positional argument: 'self'"},{"fix":"Wrap the call to the stub method (that uses `raiser`) in a `try...except` block to handle the expected exception in your test.","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.","error":"ValueError: API error (or similar exception message)"},{"fix":"Install the library using pip: `pip install pretend`.","cause":"The 'pretend' library is not installed in your current Python environment.","error":"ModuleNotFoundError: No module named 'pretend'"}]}