Claude Code for Faker: Realistic Test Data Generation — Claude Skills 360 Blog
Blog / AI / Claude Code for Faker: Realistic Test Data Generation
AI

Claude Code for Faker: Realistic Test Data Generation

Published: December 10, 2027
Read time: 5 min read
By: Claude Skills 360

Faker generates realistic fake data for testing and development. pip install faker. from faker import Faker. fake = Faker(). Name: fake.name(), fake.first_name(), fake.last_name(). Email: fake.email(), fake.safe_email(). Phone: fake.phone_number(). Address: fake.address(), fake.city(), fake.state(), fake.country(), fake.postcode(). Internet: fake.url(), fake.ipv4(), fake.ipv6(), fake.user_agent(), fake.domain_name(). IDs: fake.uuid4(), fake.md5(), fake.sha256(). Text: fake.text(), fake.sentence(), fake.paragraph(), fake.words(5). Dates: fake.date_of_birth(minimum_age=18, maximum_age=80), fake.date_this_decade(), fake.date_time_between("-5y", "now"). Numbers: fake.random_int(1, 1000), fake.pyfloat(left_digits=3, right_digits=2, positive=True). Boolean: fake.boolean(chance_of_getting_true=70). Credit card: fake.credit_card_number(), fake.credit_card_expiry(), fake.credit_card_provider(). Company: fake.company(), fake.job(), fake.catch_phrase(). Color: fake.color_name(), fake.hex_color(). Locale: Faker("de_DE"), Faker(["en_US","fr_FR"]) — multi-locale. Seed: Faker.seed(42) — reproducible data. Custom provider: subclass BaseProvider. Profile: fake.simple_profile(), fake.profile(fields=["name","mail","address"]). fake.unique.email() — no repeats. Claude Code generates Faker test fixtures, database seed scripts, and localized demo data.

CLAUDE.md for Faker

## Faker Stack
- Version: faker >= 25.0
- Import: from faker import Faker; fake = Faker()
- Locale: Faker("de_DE") | Faker(["en_US", "es_MX"]) for multi-locale
- Seed: Faker.seed(42) (class-level) | fake.seed_instance(42) (instance)
- Unique: fake.unique.email() — raises UniquenessException after exhaustion
- Custom: subclass BaseProvider and fake.add_provider(MyProvider)
- Profile: fake.simple_profile() for a quick person dict

Faker Test Data Pipeline

# tests/faker_pipeline.py — realistic test data generation with Faker
from __future__ import annotations
import datetime
import decimal
import random
import uuid
from typing import Any

from faker import Faker
from faker.providers import BaseProvider

# Reproducible seed for deterministic tests
Faker.seed(42)
fake = Faker(["en_US"])          # or Faker(["en_US", "de_DE", "ja_JP"])


# ── 0. Custom providers ───────────────────────────────────────────────────────

class EcommerceProvider(BaseProvider):
    """Custom Faker provider for e-commerce domain data."""

    CATEGORIES   = ["Electronics", "Clothing", "Home & Garden", "Sports",
                    "Books", "Toys", "Health", "Food & Beverages"]
    STATUSES     = ["pending", "processing", "shipped", "delivered", "cancelled"]
    PAYMENT_METHODS = ["credit_card", "paypal", "stripe", "bank_transfer", "crypto"]
    UNITS        = ["piece", "kg", "liter", "pack", "box"]

    def product_category(self) -> str:
        return self.random_element(self.CATEGORIES)

    def order_status(self) -> str:
        return self.random_element(self.STATUSES)

    def payment_method(self) -> str:
        return self.random_element(self.PAYMENT_METHODS)

    def product_sku(self) -> str:
        prefix = self.random_element(["PROD", "SKU", "ITEM"])
        return f"{prefix}-{self.numerify('####-####')}"

    def price(self, min_price: float = 0.99, max_price: float = 9999.99) -> str:
        return f"{random.uniform(min_price, max_price):.2f}"


fake.add_provider(EcommerceProvider)


# ── 1. User data factories ─────────────────────────────────────────────────────

def make_user(
    locale:    str = "en_US",
    confirmed: bool = True,
) -> dict:
    """Generate a realistic user record."""
    f = Faker(locale)
    dob = f.date_of_birth(minimum_age=18, maximum_age=80)
    return {
        "id":             str(f.uuid4()),
        "username":       f.user_name(),
        "email":          f.unique.safe_email(),
        "first_name":     f.first_name(),
        "last_name":      f.last_name(),
        "phone":          f.phone_number(),
        "date_of_birth":  dob.isoformat(),
        "age":            (datetime.date.today() - dob).days // 365,
        "gender":         random.choice(["male", "female", "non_binary", "prefer_not_to_say"]),
        "locale":         locale,
        "is_active":      True,
        "is_confirmed":   confirmed,
        "created_at":     f.date_time_between("-3y", "now").isoformat(),
    }


def make_address(locale: str = "en_US") -> dict:
    """Generate a postal address for a given locale."""
    f = Faker(locale)
    return {
        "id":          str(f.uuid4()),
        "street":      f.street_address(),
        "city":        f.city(),
        "state":       f.state() if hasattr(f, "state") else f.province(),
        "postcode":    f.postcode(),
        "country":     f.current_country_code(),
        "is_default":  True,
    }


def make_users_batch(
    n:        int = 100,
    locales:  list[str] = None,
    seed:     int = None,
) -> list[dict]:
    """
    Generate a batch of user records with optional locale mixing.
    seed makes output reproducible.
    """
    if seed is not None:
        Faker.seed(seed)
    locales = locales or ["en_US"]
    return [make_user(locale=random.choice(locales)) for _ in range(n)]


# ── 2. E-commerce data factories ──────────────────────────────────────────────

def make_product() -> dict:
    """Generate a product listing."""
    category = fake.product_category()
    return {
        "id":          str(fake.uuid4()),
        "sku":         fake.product_sku(),
        "name":        fake.catch_phrase().title()[:80],
        "description": fake.paragraph(nb_sentences=3),
        "category":    category,
        "price":       fake.price(min_price=1.99, max_price=299.99),
        "stock":       fake.random_int(0, 500),
        "weight_kg":   round(random.uniform(0.1, 20.0), 2),
        "is_active":   fake.boolean(chance_of_getting_true=85),
        "created_at":  fake.date_time_between("-2y", "now").isoformat(),
        "tags":        fake.words(nb=random.randint(1, 5), unique=True),
    }


def make_order(
    user_id:     str = None,
    n_items:     int = None,
) -> dict:
    """Generate a realistic order with line items."""
    user_id  = user_id or str(fake.uuid4())
    n_items  = n_items or random.randint(1, 6)
    items    = []
    subtotal = decimal.Decimal("0.00")

    for _ in range(n_items):
        price = decimal.Decimal(fake.price(min_price=0.99, max_price=199.99))
        qty   = random.randint(1, 5)
        items.append({
            "product_id": str(fake.uuid4()),
            "sku":        fake.product_sku(),
            "name":       fake.catch_phrase().title()[:60],
            "qty":        qty,
            "unit_price": str(price),
            "line_total": str(price * qty),
        })
        subtotal += price * qty

    tax     = (subtotal * decimal.Decimal("0.08")).quantize(decimal.Decimal("0.01"))
    shipping = decimal.Decimal("9.99") if subtotal < 50 else decimal.Decimal("0.00")

    return {
        "id":               str(fake.uuid4()),
        "order_number":     f"ORD-{fake.numerify('########')}",
        "user_id":          user_id,
        "status":           fake.order_status(),
        "items":            items,
        "subtotal":         str(subtotal),
        "tax":              str(tax),
        "shipping":         str(shipping),
        "total":            str(subtotal + tax + shipping),
        "payment_method":   fake.payment_method(),
        "shipping_address": make_address(),
        "created_at":       fake.date_time_between("-1y", "now").isoformat(),
        "updated_at":       fake.date_time_between("-30d", "now").isoformat(),
    }


# ── 3. API / network data ─────────────────────────────────────────────────────

def make_api_event(
    event_type: str = None,
) -> dict:
    """Generate a web API access log event."""
    methods  = ["GET", "POST", "PUT", "PATCH", "DELETE"]
    paths    = ["/api/users", "/api/products", "/api/orders", "/health", "/metrics"]
    statuses = [200, 200, 200, 201, 204, 400, 401, 403, 404, 429, 500]

    return {
        "timestamp":    fake.date_time_between("-7d", "now").isoformat() + "Z",
        "request_id":   str(fake.uuid4()),
        "method":       random.choice(methods),
        "path":         random.choice(paths),
        "status_code":  random.choices(statuses, weights=[10,10,10,3,2,2,2,1,3,1,1])[0],
        "duration_ms":  round(random.lognormvariate(4.0, 0.8), 1),
        "user_agent":   fake.user_agent(),
        "ip_address":   fake.ipv4_public(),
        "user_id":      str(fake.uuid4()) if fake.boolean(70) else None,
    }


# ── 4. Financial test data ────────────────────────────────────────────────────

def make_transaction() -> dict:
    """Generate a financial transaction for testing payment processing."""
    return {
        "id":              str(fake.uuid4()),
        "amount":          fake.price(min_price=0.01, max_price=50000.00),
        "currency":        fake.currency_code(),
        "card_number":     fake.credit_card_number(card_type="visa"),
        "card_expiry":     fake.credit_card_expire(),
        "card_provider":   fake.credit_card_provider(),
        "iban":            fake.iban(),
        "bic":             fake.swift(),
        "description":     fake.sentence(nb_words=6),
        "reference":       fake.bothify("TXN-????-####"),
        "created_at":      fake.date_time_this_month().isoformat(),
    }


# ── 5. Localized data batches ─────────────────────────────────────────────────

def make_localized_dataset(
    locales:    dict[str, int],   # {"en_US": 70, "de_DE": 20, "ja_JP": 10}
    seed:       int = 0,
) -> list[dict]:
    """
    Generate a mixed-locale dataset proportional to the given distribution.
    Use for testing locale-specific validation and formatting.
    """
    Faker.seed(seed)
    users = []
    for locale, count in locales.items():
        users.extend(make_user(locale=locale) for _ in range(count))
    random.shuffle(users)
    return users


# ── 6. Database seed helper ───────────────────────────────────────────────────

def seed_database(session, n_users: int = 50, n_products: int = 200):
    """
    Seed a SQLAlchemy session with fake data.
    Replace UserModel / ProductModel with your actual model classes.
    """
    Faker.seed(0)

    print(f"Seeding {n_users} users...")
    for user_data in make_users_batch(n_users):
        # Example: session.add(UserModel(**user_data))
        pass

    print(f"Seeding {n_products} products...")
    for _ in range(n_products):
        # Example: session.add(ProductModel(**make_product()))
        pass

    # session.commit()
    print("Seed complete.")


# ── Demo ──────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    print("Faker Test Data Demo")
    print("=" * 50)

    # Single user
    Faker.seed(42)
    user = make_user("en_US")
    print(f"\nUser: {user['first_name']} {user['last_name']}")
    print(f"  email: {user['email']}")
    print(f"  dob:   {user['date_of_birth']} (age {user['age']})")

    # Product
    product = make_product()
    print(f"\nProduct: {product['name']}")
    print(f"  SKU:   {product['sku']}, Price: ${product['price']}, Stock: {product['stock']}")

    # Order
    order = make_order(user_id=user["id"])
    print(f"\nOrder: {order['order_number']} ({order['status']})")
    print(f"  Items: {len(order['items'])}, Total: ${order['total']}")
    print(f"  Ship to: {order['shipping_address']['city']}, {order['shipping_address']['country']}")

    # Batch
    batch = make_users_batch(5, locales=["en_US", "de_DE", "ja_JP"], seed=99)
    print(f"\nMixed-locale batch (5 users):")
    for u in batch:
        print(f"  [{u['locale']}] {u['first_name']} {u['last_name']}{u['email']}")

    # API logs
    events = [make_api_event() for _ in range(3)]
    print(f"\nAPI events:")
    for e in events:
        print(f"  {e['method']:6} {e['path']:<20} {e['status_code']} ({e['duration_ms']}ms)")

For the random + string.ascii_letters alternative — random.choice(string.ascii_letters) generates structurally invalid data (email “abcd”, phone “xyzq”) that passes field-presence checks but breaks real validators while Faker’s fake.email() generates RFC 5321-valid strings and fake.phone_number() follows locale-specific E.164 patterns, making test data that exercises the same validation code paths as production. For the hand-crafted fixtures alternative — manually maintained test_users.json fixtures go stale when schema changes while make_users_batch(100, seed=42) regenerates fresh conformant data on every test run, fake.unique.email() guarantees no duplicate emails in a single batch (raising UniquenessException cleanly if the pool is exhausted), and Faker("de_DE") generates Austrian postal codes and German phone formats for locale-aware validation tests without a separate DE fixture file. The Claude Skills 360 bundle includes Faker skill sets covering user/address factories, custom BaseProvider subclasses, make_order with line items and totals, make_api_event for log testing, financial transaction data, mixed-locale batch generation, Faker.seed for reproducibility, fake.unique for deduplication, and database seed helpers. Start with the free tier to try test data generation code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free