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.
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.
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
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()
We can create the inputs mentioned above using Faker’s methods. The following are the syntaxes:
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()
To generate a dummy address using faker, we use the address()
method. The syntax of the address method is the following:
fakerObject.address()
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()
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()
The country()
method of the Faker
class will return a dummy country name. The following is the syntax of the method.
fakerObject.country()
To generate a dummy date using faker, we use the date()
method. The syntax of the method is the following:
fakerObject.date()
The following code will create a dummy profile record.
from faker import Fakerdef 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 functionif __name__ == "__main__":dummy=create_person_record()for i in dummy:print(i,':',dummy[i])
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