Skip to content
Using the Python Faker Library to Generate Mock Data for Testing

Using the Python Faker Library to Generate Mock Data for Testing

This article introduces the Python Faker library, explains how to install it, demonstrates basic usage with locale settings, lists various data generation methods (e.g., names, addresses, emails, IPs, credit cards), and provides a practical example of inserting fake records into a MySQL database.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle

When developing a project, testing often requires realistic mock data, and manually creating such data is time‑consuming and unrealistic. The Python Faker library provides a professional solution for generating a wide variety of fake data.

Installation: pip install Faker Simple usage example:

>> from faker import Faker
>>> fake = Faker(locale='zh_CN')
>>> fake.name()
'李洁'
>>> fake.address()
'上海市兴安盟县江北东莞路r座 803484'

The locale parameter selects the cultural locale for generated data; common options include zh_CN (Chinese Mainland), zh_TW (Chinese Taiwan), and en_US (English United States).

Faker also offers many specialized generators, such as geographic information ( city_suffix(), country(), latitude(), longitude()), basic personal data ( ssn(), name(), phone_number()), email utilities ( email(), safe_email()), network details ( ipv4(), url(), user_agent()), browser user‑agents ( chrome(), firefox()), numeric generators ( random_int(), pyfloat()), and text/encryption helpers ( paragraph(), uuid4(), md5()), among others.

Practical use case – inserting fake records into a MySQL table:

import pymysql
from faker import Faker

conn = pymysql.connect(host="114.215.129.166", port=3306, user="nice", 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(20),
    password varchar(20),
    address varchar(35)
)
"""
cursor.execute(sql1)
cursor.execute(sql2)
fake = Faker("zh-CN")
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('姓名:' + fake.name() + '|密码:' + fake.password(special_chars=False) + '|地址:' + fake.address())
    cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()

Sample output of the script shows generated Chinese names, passwords, and addresses, e.g.:

姓名:王平|密码:iZqPxLO947|地址:吉林省莉市房山杨路R座 491718
姓名:柏倩|密码:h853B0idne|地址:辽宁省玉华县蓟州永安街g座 205585
... (additional rows omitted)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Data GenerationtestingMySQLmock dataFaker
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.