Getting started
This guide gets you from zero to generating fake data with Capper in a few minutes.
Install
You need Python 3.9+. Install Capper and its required backends:
pip install capper
For Pydantic models (used in most examples), also install the optional extra:
pip install capper[pydantic]
Capper depends on Faker and Polyfactory; they are installed automatically.
Your first model and factory
- Define a Pydantic model (or dataclass) and use Capper types for fields you want to fake.
- Create a Polyfactory factory for that model — no extra configuration needed.
- Call
Factory.build()to get one instance, orFactory.batch(n)for many.
Example: a simple user
from pydantic import BaseModel
from capper import Name, Email
from polyfactory.factories.pydantic_factory import ModelFactory
class User(BaseModel):
name: Name
email: Email
class UserFactory(ModelFactory[User]):
pass
if __name__ == "__main__":
user = UserFactory.build()
print("Name:", user.name)
print("Email:", user.email)
Run this (from the repo root, with Capper installed):
python docs/examples/getting_started.py
Example output (values will vary):
Name: Lisa Roberts
Email: fwilliams@example.com
You’ll see different name/email each run. Capper types are subclasses of str, so you can use them anywhere you’d use a string.
What just happened?
NameandEmailare Capper types. Each is tied to a Faker provider (name,email).- Polyfactory’s
ModelFactorydiscovers Capper types and uses Capper’s shared Faker instance to generate values — no manual registration. UserFactory.build()creates oneUserwith random but valid-looking name and email.
Next steps
- Models and factories — Pydantic vs dataclasses, batches, and more types.
- Reproducible data — seeding for tests and demos.
- Custom types — your own Faker-backed types and
faker_kwargs.