Python Faker Library


Faker is a Python library used for generating fake data, fake data is mainly used for Integration Testing by creating dummy data in databases. Faker can generate meaningful fake data like generating names, addresses, emails, JSON data, currency-related data also generating the data from a given data set as well. The Faker library can also be used while writing mock test cases as well. The article briefly explains how to work with the Faker library and covers multiple examples of it.

Python Faker Library


Just like all the other python packages, faker installation is exactly very similar, using pip for local installation we can use ‘pip install Faker’

Basic Usage

In this section, the article covers various examples of Faker lib. For generating one random name, we can use the following code

from faker import Faker
fake = Faker()
print( # Haley Ramirez

If we want to generate say 10 fake names, we can enhance the code by simply calling the function inside the loop

for i in range(10):

Similarly, we can use address(), company(), country(), email(), credit_card_number(), currency. Faker library contains almost all the attributes required for generating the fake data. For example

print("Phone:", fake.phone_number())
print("URL:", fake.uri())

Data in JSON format

With the help of ‘json’ library we can generate fake data in JSON format as well, say we are writing an Integration Test for a RESTful service for POST or PUT operation.

import json     
employee ={} 
employee[0]['address']= fake.address() 
employee[0]['state']= str(fake.state()) 
employee[0]['country']= str( 
employee[0]['ssn']= str(fake.ssn()) 
print(json.dumps(employee, sort_keys=True, indent=4))

Similarly, we can execute the same code in a loop for generating multiple JSON.

import json  
for i in range(3):
    employee ={} 
    employee[i]['address']= fake.address() 
    employee[i]['state']= str(fake.state()) 
    employee[i]['country']= str( 
    employee[i]['ssn']= str(fake.ssn()) 
    print(json.dumps(employee, sort_keys=True, indent=4))

Generating Locale Specific Data

Faker library is capable enough of generating locale-specific data, like generating fake Japanese names

fake_jp = Faker('ja_JP')
for i in range(10):

Multiple Locales

fake = Faker(['en_US', 'hi_IN', 'ja_JP'])
for i in range(10):


We have seen how to work with the Python Faker package for generating various types of data. We have seen generating profile-related data, locale-specific data, tweaking fake data to be in JSON format. But still, there is a lot more is left to explore like currency-related data, etc.