Placebo

0.10.0 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up Placebo for both recording and playing back AWS API calls using `boto3`. It creates a `boto3` session, attaches Placebo to it with a specified data directory, and then, based on an environment variable, either records S3 `list_buckets` calls or plays them back from previously recorded JSON files.

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)

view raw JSON →