Skip to content

#263: Generate Realistic Test Data With Faker

If you have ever worked on a project that needed realistic test data, you know how tedious it can be to create it manually. Hardcoding names, addresses, or even phone numbers into your test suite not only clutters your code but also makes it inflexible. Thankfully, the Python Faker library comes to the rescue.

What Is Faker?

Faker is a Python package that generates fake data for you. It is highly configurable, simple to use, and perfect for creating a variety of placeholder data for testing or development environments. It can generate names, addresses, dates, phone numbers, emails, and even paragraphs of lorem ipsum text. Best of all, it supports multiple locales, making it easy to simulate data from different countries.

Let us get practical and take our first steps with Faker.

Installing Faker

We can install Faker with this command:

pip install Faker

Once installed, we are ready to generate as much fake data as we need.

Creating Fake Data

We can create a Faker object and call its various methods to explore its data generator:

1
2
3
4
5
6
7
8
9
from faker import Faker

# Create a Faker instance
fake = Faker()

# Generate random fake data
print(fake.name())
print(fake.address())
print(fake.email())

This will output something like this:

1
2
3
4
Lisa Vasquez
769 Wright Center
Frankfort, LA 41683
[email protected]

Each time we run this code it will generate new random data.

Localisation

We can pass a locale to Faker to get a localised flavour of our generated data:

1
2
3
4
fake = Faker('de_DE')
print(fake.name())

# may print something like: Univ.Prof. Karlheinz Jungfer B.A.

Supported locales are listed in the Faker documentation. Should you use a locale that has no matching provider, Faker will fall back to en_US.

Seeding for consistency

Sometimes, especially in testing, we want our fake data to be consistent. We can achieve this by seeding the random generator:

1
2
3
fake.seed_instance(42)
print(fake.name())  
# This will always generate the same name for your locale

Advanced Use Cases

Faker can generate more than just names and addresses. Here are some advanced use cases:

  • random text:

    print(fake.text())
    # Organization list act matter. Recently though act.
    # Middle matter across stay. Record other or program seat 
    # maintain consumer.
    
    print(fake.paragraph(nb_sentences=5))
    # Key same themselves visit will trade. Despite whose morning 
    # detail involve suddenly again choice. Rest race story. Media 
    # per industry method car.
    
    print(fake.sentence(nb_words=10, variable_nb_words=False))
    # Sell between financial lead free most rather process teacher wrong.
    

  • credit card numbers:

    print(fake.credit_card_provider())
    # VISA 16 digit
    
    print(fake.credit_card_number())
    # 4642050533671765
    
    print(fake.credit_card_expire())
    # 06/33
    
    print(fake.credit_card_security_code())
    # 785
    
    print(fake.credit_card_full())
    # VISA 16 digit
    # David King
    # 4582638463858121 09/26
    # CVC: 380
    

  • Email:

    1
    2
    3
    4
    5
    6
    7
    8
    print(fake.ascii_company_email())
    # [email protected]
    
    print(fake.ascii_free_email())
    # [email protected]
    
    print(fake.ascii_safe_email())
    # [email protected]
    

  • ISBN:

    1
    2
    3
    4
    5
    print(fake.isbn10())
    # 0-909884-02-1
    
    print(fake.isbn13())
    # 978-1-9804-5634-6
    

  • Coordinates:

    print(fake.coordinate())
    # 151.534342
    
    print(fake.latitude())
    # 25.617686
    
    print(fake.longitude())
    # 155.400946
    
    print(fake.latlng())
    # (Decimal('-31.249222'), Decimal('104.436016'))
    
    print(fake.local_latlng())
    # ('34.63915', '-120.45794', 'Lompoc', 'US', 'America/Los_Angeles')
    
    print(fake.location_on_land())
    # ('-3.14306', '-58.44417', 'Itacoatiara', 'BR', 'America/Manaus')
    

We can generate these values with the standard providers. Read the documentation to see what else comes with Faker.

Next

The Faker library is a powerful tool for generating dummy data fast and without much effort. Whether we need fake user profiles for testing our web app or placeholders for populating our database during development, Faker has us covered. But what happens if we need more specific values? Next week we create our own custom provider to get the exact values we need.