How to generate dummy records using the faker library in Python

Testing an application is one of the most critical phases in the development process. However, testing a system that manipulates data requires having a large amount of testing data. Adding data entries manually for a profile or other inputs can be very difficult and redundant.

Faker library

We can automate profile creation and add dummy data for a person using the faker library in Python. We use the library to generate dummy data used for testing the application. It provides different methods to generate fake/dummy data of various types.

Installation

The Faker library needs to be installed before we can use its methods. Write the following command in the terminal to install the library:

pip install faker

Profile inputs can have the following attributes generated by the faker library.

  • Name

  • Address

  • Email

  • Phone number

  • Country

  • Date

To use the library, we need to import it to get the package's methods in the script and create an instance/object of the Faker() class as the following:

fakerObject=Faker()

Syntax

We can create the inputs mentioned above using Faker’s methods. The following are the syntaxes:

Name

The name() method of the Faker class will return a string that is the dummy person’s name. The following is the syntax of the method.

fakerObject.name()

Address

To generate a dummy address using faker, we use the address() method. The syntax of the address method is the following:

fakerObject.address()

Email

The email() method of the Faker class will return a string that is the dummy person's email. The following is the syntax of the aforementioned method.

fakerObject.email()

Phone number

To generate a dummy contact number using faker in Python, we use the phone_number() method. The syntax of the method is the following:

fakerObject.phone_number()

Country

The country() method of the Faker class will return a dummy country name. The following is the syntax of the method.

fakerObject.country()

Date

To generate a dummy date using faker, we use the date() method. The syntax of the method is the following:

fakerObject.date()

Example

The following code will create a dummy profile record.

from faker import Faker
def create_person_record():
fakeObject=Faker()
record=dict()
record['name']=fakeObject.name()
record['address']=fakeObject.address()
record['email']=fakeObject.email()
record['contact']=fakeObject.phone_number()
record['country']=fakeObject.country()
record['DOB']=fakeObject.date()
return record
#Main function
if __name__ == "__main__":
dummy=create_person_record()
for i in dummy:
print(i,':',dummy[i])

Explanation

  • Line 1: We import the Faker class from the faker library.

  • Lines 4–5: We declare the function create_person_record().

  • Line 5: We create an instance of the Faker type and a dictionary named record that stores the dummy data.

  • Lines 7–14: We call all the methods we need to create the record of a person and return the record.

  • Lines 17–20: We call the create_person_record function, whose output is stored in the dummy variable. We print the dictionary using a loop to get the output in each line.

Note: We can also use the fakerObject.profile() method to return a dictionary object of the standard profile inputs.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved