Placebo
Placebo is a Python library designed to mock `boto3` calls, allowing developers to record actual AWS service responses and replay them later without interacting with real AWS endpoints. This is particularly useful for unit testing, offline development, and creating consistent test environments. The current version is 0.10.0. Releases have been sporadic, with a significant update to 0.10.0 after a nearly three-year gap, indicating active maintenance but not a fixed cadence.
Common errors
-
ModuleNotFoundError: No module named 'boto3'
cause Placebo is designed to mock `boto3` interactions but does not include `boto3` as a direct installation dependency. This error occurs if `boto3` is not installed in your environment.fixInstall `boto3` alongside Placebo: `pip install boto3 placebo`. -
TypeError: a bytes-like object is required, not 'str' (or similar errors when processing binary data)
cause This typically indicates an attempt to process binary response data (e.g., from S3 `get_object` calls) as a string. Older Placebo versions had issues with binary responses, and even with 0.10.0+, incorrect handling of the `StreamingBody` can lead to this.fixEnsure you are reading the `botocore.response.StreamingBody` as bytes or handling compression correctly. For example, `response['Body'].read()` will return bytes. If you need to decompress, use `gzip.GzipFile` with `io.BytesIO`. -
AWS calls are still being made to real endpoints even when Placebo is supposedly active.
cause This can happen if `pill.record()` or `pill.playback()` was not called on the `Pill` object, if `boto3` clients were created before these methods were invoked, or if Placebo was attached to a different `boto3.Session` than the one being used.fixVerify that `pill.record()` or `pill.playback()` is explicitly called on the correct `Pill` instance, and that all `boto3` clients intended for mocking are instantiated *after* Placebo has been activated for that session.
Warnings
- breaking Placebo version 0.10.0, released in September 2021, officially dropped support for Python 2.x. Codebases still on Python 2.x will need to upgrade to Python 3.x to use this and future versions.
- gotcha Older versions of Placebo (prior to 0.10.0) had a bug handling binary response bodies from AWS services, which could lead to incorrect data or errors during playback.
- gotcha When using `boto3.DEFAULT_SESSION`, Placebo may not correctly attach unless `boto3.setup_default_session()` is explicitly called beforehand. If you create `boto3` clients from a default session that hasn't been set up, Placebo might not intercept calls.
- gotcha Clients created from a `boto3.Session` *before* `pill.record()` or `pill.playback()` is called might not be 'placebo-aware' and will still make live AWS calls. This was a bug fixed in 0.7.2, but misunderstanding the order of operations can still cause issues.
Install
-
pip install placebo
Imports
- attach
from placebo import attach
- Session
import boto3; session = boto3.DEFAULT_SESSION
import boto3; boto3.Session()
Quickstart
import boto3
import placebo
import os
data_path = os.path.join(os.path.dirname(__file__), 'placebo_data')
os.makedirs(data_path, exist_ok=True)
session = boto3.Session(region_name='us-east-1')
pill = placebo.attach(session, data_path=data_path)
# --- Recording mode ---
# Set PLACEBO_MODE=record in environment variables to enable recording
# e.g., PLACEBO_MODE=record python your_script.py
if os.environ.get('PLACEBO_MODE') == 'record':
print("Recording AWS calls...")
pill.record()
s3 = session.client('s3')
try:
s3.list_buckets()
print("Buckets listed and recorded.")
except Exception as e:
print(f"Error during recording: {e}")
# --- Playback mode ---
# Default behavior, or set PLACEBO_MODE=playback
# e.g., PLACEBO_MODE=playback python your_script.py
else:
print("Playing back recorded AWS calls...")
pill.playback()
s3 = session.client('s3')
try:
response = s3.list_buckets()
print("Buckets listed from recording:")
for bucket in response.get('Buckets', []):
print(f" - {bucket['Name']}")
except Exception as e:
print(f"Error during playback: {e}")
# Clean up (optional, for demonstration purposes)
# import shutil
# if os.path.exists(data_path):
# shutil.rmtree(data_path)