Skip to content

prefer-library-fake

Prefer a maintained fake, emulator, recorder, or test service for substantial third-party protocols.

Why

A substantial hand-written protocol model can drift from supported request, state, and error behavior.

Fix

When the test depends on protocol fidelity, use a maintained fake, emulator, recorder, or test service while keeping the production client. Keep narrow application-port and failure-scripting doubles local.

Examples

Before — flagged Test defines its own S3 client
tests/fakes/s3.py
class FakeS3Client:
def __init__(self):
self.objects = {}
def put_object(self, Bucket, Key, Body):
self.objects[(Bucket, Key)] = Body
return {"ResponseMetadata": {"HTTPStatusCode": 200}}
def get_object(self, Bucket, Key):
return {"Body": self.objects[(Bucket, Key)]}
def delete_object(self, Bucket, Key):
self.objects.pop((Bucket, Key), None)
After — preferred Test uses the maintained AWS fake
tests/test_upload.py
import boto3
from moto import mock_aws
@mock_aws
def test_upload():
client = boto3.client("s3", region_name="us-east-1")
client.create_bucket(Bucket="uploads")
upload(client, bucket="uploads", key="avatar", body=b"image")
result = client.get_object(Bucket="uploads", Key="avatar")
assert result["Body"].read() == b"image"