Last modified: Oct 24, 2026

Generate Test Data with Faker in Python

Writing tests needs data. Real data is messy and private. So we need fake data that looks real. That is where Faker helps.

Faker is a Python library that generates fake data for you. It creates names, emails, addresses, phone numbers, dates, and much more. This article shows you how to use it step by step.

Why Use Faker?

Hard-coded test data gets boring fast. You end up with the same "John Doe" in every test.

Faker solves this. It gives you thousands of realistic values. Your tests become more varied and more likely to catch bugs.

Faker is also great for demos, database seeding, and building sample apps. It supports many locales, so you can generate data in different languages.

Installing Faker

Faker installs with pip like most Python packages. Open your terminal and run this command.


pip install Faker

That is all you need. The library has no heavy dependencies. It works on Python 3.6 and newer.

Creating Your First Fake Data

Start by importing the Faker class. Then create an instance and call its methods.


from faker import Faker

# Create a Faker instance with default (English) locale
fake = Faker()

# Generate a few fake values
print(fake.name())        # A full name
print(fake.email())       # An email address
print(fake.address())     # A multi-line address
print(fake.phone_number())# A phone number

The output changes every time you run the script. Here is one possible result.


Jennifer Davis
[email protected]
1234 Smith Lane
Lake Sarah, OH 45678
(555) 123-4567

Each call returns a new random value. You can call any method as many times as you like.

Common Faker Methods

Faker groups methods by data type. Here are the ones you will use most.

  • Person:name, first_name, last_name, job
  • Contact:email, phone_number, company
  • Address:address, city, country, postcode
  • Numbers:random_int, random_number, pyfloat
  • Dates:date, date_time, past_date, future_date
  • Text:text, sentence, paragraph, word

Let's see a few of them in action.


from faker import Faker

fake = Faker()

print(fake.job())
print(fake.company())
print(fake.city())
print(fake.random_int(min=1, max=100))
print(fake.date())
print(fake.sentence())

Software engineer
Walker and Sons
Port Michael
42
2023-08-14
Central clear maybe hope three.

These methods cover most everyday needs. For the full list, check the official Faker docs.

Generating Structured Data

Often you need a whole record, not single values. A simple dictionary works well.


from faker import Faker

fake = Faker()

# Build a list of fake user records
users = []
for _ in range(3):
    users.append({
        "name": fake.name(),
        "email": fake.email(),
        "age": fake.random_int(min=18, max=80),
        "city": fake.city(),
    })

for user in users:
    print(user)

{'name': 'David Brown', 'email': '[email protected]', 'age': 34, 'city': 'West Lisa'}
{'name': 'Maria Garcia', 'email': '[email protected]', 'age': 61, 'city': 'South Anthony'}
{'name': 'James Lee', 'email': '[email protected]', 'age': 27, 'city': 'New Amanda'}

This pattern is perfect for seeding a database. It also works for building JSON fixtures for API tests.

If you are testing an API that calls an LLM, you can pair fake records with the techniques in OpenAI Function Calling in Python to build realistic end-to-end test flows.

Controlling Randomness with a Seed

Random data is great, but tests need to be repeatable. Faker lets you set a seed.

The seed method makes the output deterministic. Run the script twice with the same seed and you get the same values.


from faker import Faker

fake = Faker()
Faker.seed(42)  # Lock the random generator

print(fake.name())
print(fake.email())

Norman Miller
[email protected]

Run it again with seed 42 and you get the exact same output. This is a must for unit tests.

Using Multiple Locales

Faker supports many languages and regions. Pass a locale code when you create the instance.


from faker import Faker

# French and Japanese data
fr_fake = Faker("fr_FR")
ja_fake = Faker("ja_JP")

print(fr_fake.name())
print(ja_fake.address())

Élodie Leclerc
〒123-4567 東京都渋谷区...

You can also mix locales. Use Faker(["en_US", "de_DE"]) to draw from several at once.

Generating Text and Tokens

Faker can create sentences and paragraphs. This is handy for testing text pipelines.

For example, you might feed fake text into an NLP tokenizer. Learn how in Tokenization with NLTK in Python.


from faker import Faker

fake = Faker()

# A short paragraph of fake text
print(fake.paragraph(nb_sentences=2))

Play guess better challenge. Window ago good expert its. Entire care those.

Faker with Data Serialization

Fake records often need to travel between systems. You may serialize them with protobuf.

Build fake messages, then encode them. The guide on how to compile and use .proto files in Python walks through the schema side.

Best Practices

Follow these tips to get the most from Faker.

  • Seed your tests. Always call seed in test setup for repeatable runs.
  • Keep it out of production. Faker is a test tool, not a data source for real users.
  • Use unique values. Call unique when you need no duplicates, like for emails in a database column.
  • Match your locale. Pick a locale that fits your app's users.

The unique proxy is very useful. It guarantees each generated value is different.


from faker import Faker

fake = Faker()

# Emails will never repeat
emails = [fake.unique.email() for _ in range(5)]
print(emails)

['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']

Conclusion

Faker makes test data easy. It is simple to install, simple to use, and very flexible.

You can generate names, emails, dates, and text in seconds. You can seed it for repeatable tests. You can even use many locales.

Start small. Create a Faker instance and print a few values. Then build records and seed your database.

Your tests will be cleaner, more realistic, and easier to maintain. Give Faker a try in your next project.