use python Faker to generate test data for any situation

  1. pip install Faker
  2. test Faker functions:
>>> from faker import Faker
>>> fake = Faker(locale='it_IT')
>>> fake.name()
'Romeo Lanfranchi'
>>> fake.address()
'Piazza Antonio 599 Appartamento 12\nSan Ludovica, 42403 Reggio Emilia (TR)'
>>> fake.ssn()
'YQGCOO12N17H487H'
>>> fake.company()
'Acerbi, Rossini e Viola s.r.l.'
>>> fake.ascii_email()
'campanellaguido@hotmail.it'

3. generate test data with name, password and address

import pymysql
from faker import Faker

conn = pymysql.connect(host="ubuntu2020", port=3306, user="monty", password="password", db="flask201",
                       charset="utf8")

cursor = conn.cursor()
sql1 = """drop table if exists faker_user"""
sql2 = """
create table faker_user(
pid int primary key auto_increment,
username varchar(40),
password varchar(20),
address varchar(100) 
)
"""
cursor.execute(sql1)
cursor.execute(sql2)
fake = Faker("en-US")
for i in range(20):
    sql = """insert into faker_user(username,password,address) 
    values('%s','%s','%s')""" % (fake.name(), fake.password(special_chars=False), fake.address())
    print('NAME:'+fake.name() + '|PASSWORD:'+fake.password(special_chars=False) + '|ADDR:'+fake.address())
    cursor.execute(sql)

conn.commit()
cursor.close()
conn.close()

4. generate a json of 10 students with name students.json that contains student name, address, location coordinates and student roll number

from faker import Faker 
import json            # To create a json file                 
from random import randint      # For student id 
fake = Faker() 
def input_data(x): 
  
    # dictionary 
    student_data ={} 
    for i in range(0, x): 
        student_data[i]={} 
        student_data[i]['id']= randint(1, 100) 
        student_data[i]['name']= fake.name() 
        student_data[i]['address']= fake.address() 
        student_data[i]['latitude']= str(fake.latitude()) 
        student_data[i]['longitude']= str(fake.longitude()) 
    print(student_data) 
  
    # dictionary dumped as json in a json file 
    with open('students.json', 'w') as fp: 
        json.dump(student_data, fp) 
      
def main():
      # Enter number of students 
    number_of_students = 10  
    input_data(number_of_students) 
main() 

C:\Users\zhuby>test_faker.py
{0: {'id': 57, 'name': 'Joshua Russell', 'address': '27726 Reed Bypass Apt. 649\nNelsonburgh, PA 87356', 'latitude': '-86.0037635', 'longitude': '-88.371746'}, 1: {'id': 88, 'name': 'Alexander Carpenter', 'address': '5124 Gonzales Center Apt. 087\nAngelamouth, RI 36493', 'latitude': '87.4800465', 'longitude': '-16.477034'}, 2: {'id': 40, 'name': 'Rachel Miller', 'address': '2878 Curtis Parkways Suite 338\nCarolburgh, OK 27880', 'latitude': '-87.890325', 'longitude': '-98.986404'}, 3: {'id': 72, 'name': 'Jon Graves', 'address': '087 Kelley Ports\nSusanmouth, MN 32402', 'latitude': '-50.9987975', 'longitude': '-145.366978'}, 4: {'id': 1, 'name': 'Joseph Rogers', 'address': '630 Sheryl Underpass\nEast Cheryl, IL 95386', 'latitude': '-62.0369605', 'longitude': '-167.059577'}, 5: {'id': 28, 'name': 'Cheryl Hill', 'address': '6863 Christopher Flats\nNew Angela, OK 10664', 'latitude': '59.4719505', 'longitude': '-73.419133'}, 6: {'id': 93, 'name': 'Sean Robinson', 'address': '911 Gardner Center\nEduardoshire, DC 78317', 'latitude': '7.7988015', 'longitude': '156.459505'}, 7: {'id': 6, 'name': 'Jason Myers', 'address': '9577 Shane Valleys Apt. 973\nNew Peggychester, MI 19213', 'latitude': '27.410656', 'longitude': '71.306510'}, 8: {'id': 100, 'name': 'Lindsay Johnson', 'address': '8592 Kathleen Forge Apt. 490\nBerryhaven, TX 84031', 'latitude': '58.841526', 'longitude': '79.355569'}, 9: {'id': 23, 'name': 'Sandra White', 'address': '93126 Schneider Center Suite 054\nAmandaberg, CA 31652', 'latitude': '25.893303', 'longitude': '113.012125'}}

11 Replies to “use python Faker to generate test data for any situation”

  1. Awesome write-up. I am a regular visitor of your web site and appreciate you taking the time to maintain the nice site. I will be a regular visitor for a long time. Ariana Andras Bremen

  2. hello there and thank you for your information ?
    I’ve definitely picked up something new from right here.
    I did however expertise a few technical points using this website,
    as I experienced to reload the website a lot of times previous to I could get it
    to load properly. I had been wondering if your web hosting is OK?
    Not that I’m complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your high quality score if ads and marketing
    with Adwords. Well I am adding this RSS to my e-mail and can look
    out for a lot more of your respective exciting content.
    Ensure that you update this again very soon.

    my site … Frost Portable AC Reviews

Leave a Reply

Your email address will not be published. Required fields are marked *