Using Python Faker Library to Generate Mock Data for Testing
This article introduces the Python Faker library, explains how to install and configure it with different locales, lists its extensive data‑generation methods, and demonstrates a practical example of creating a MySQL table and inserting realistic fake user records for testing purposes.
When developing a project, generating realistic fake data for testing can be time‑consuming; the Python Faker library provides a convenient way to create such data.
1. Installation
pip install Faker2. Simple usage
from faker import Faker fake = Faker(locale='zh_CN') fake.name() fake.address()The locale parameter selects the cultural locale, e.g., zh_CN for Mainland China, en_US for United States, allowing generation of region‑specific data.
3. Other methods
Geographic information generators:
city_suffix() country() country_code() district() geo_coordinate() latitude() longitude() postcode() province() address() street_address() street_name() street_suffix()Basic information generators:
ssn() bs() company() company_prefix() company_suffix() credit_card_expire() credit_card_full() credit_card_number() credit_card_provider() credit_card_security_code() job() first_name_female() first_name_male() name() name_female() name_male() phone_number() phonenumber_prefix()Email information generators:
ascii_company_email() ascii_email() company_email() email() safe_email()Network information generators:
domain_name() domain_word() ipv4() ipv6() mac_address() tld() uri() uri_extension() uri_page() uri_path() url() user_name() image_url()Browser user‑agent generators:
chrome() firefox() internet_explorer() opera() safari() linux_platform_token() user_agent()Numeric generators:
numerify() random_digit() random_digit_not_null() random_int() random_number() pyfloat() pyint() pydecimal()Text and cryptographic generators:
pystr() random_element() random_letter() paragraph() paragraphs() sentence() sentences() text() word() words() binary() boolean() language_code() locale() md5() null_boolean() password() sha1() sha256() uuid4()4. Practical example
Creating a MySQL table and inserting 20 rows of fake user data:
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('Name:'+fake.name() + '|Password:'+fake.password(special_chars=False) + '|Address:'+fake.address()) cursor.execute(sql) conn.commit() cursor.close() conn.close()The script prints each generated record and stores it in the database, demonstrating how Faker can automate realistic test data creation.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.