Home » Generating test data using the Faker package

Generating test data using the Faker package

by maxguy71

If you need to generate fake data for any reason then faker is the python package for you.

 

Installation

You can install this easily using pip like this

pip install Faker

From there, we create a Faker ‘generator’ on which we will make the many calls for our generated data:

from faker import Faker
fake = Faker()

Faker has numerous built in providers, here are

faker.providers
faker.providers.address
faker.providers.automotive
faker.providers.bank
faker.providers.barcode
faker.providers.color
faker.providers.company
faker.providers.credit_card
faker.providers.currency
faker.providers.date_time
faker.providers.file
faker.providers.geo
faker.providers.internet
faker.providers.isbn
faker.providers.job
faker.providers.lorem
faker.providers.misc
faker.providers.person
faker.providers.phone_number
faker.providers.profile
faker.providers.python
faker.providers.ssn
faker.providers.user_agent

Here’s a list of Providers written by the community:

Description URL
Airport names, airport codes, and flights faker_airtravel
Fake credit score data for testing purpose faker_credit_score
Fake microservice names faker_microservice
Music genres, subgenres, and instruments. faker_music
Fake posts in markdown format mdgen
Fake vehicle information includes Year Make Model faker_vehicle
Web-related data such as mime-type and web server versions. faker_web
Fake Wi-Fi ESSIDs faker_wifi_essid
Wrap over other providers to return their value or None. faker_optional

You can also change the default locale from english to another language

 

Code examples

This example would generate 10 fake email accounts

#!/usr/bin/python

from faker import Faker
fake = Faker()

print (fake.name())

for _ in range(10):
  print(fake.name(),fake.ascii_free_email())

This example expands on generating fake email data

#!/usr/bin/python

from faker import Faker

fake = Faker()

print(f'Email: {fake.ascii_company_email()}')
print(f'Email: {fake.ascii_email()}')
print(f'Email: {fake.ascii_free_email()}')
print(f'Email: {fake.ascii_safe_email()}')
print(f'Email: {fake.company_email()}')

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More