Faker library in Python: a powerful tool for generating Mock data (there are scene examples at the end of the article)

Faker library is a powerful Mock data generation library in Python that can help development The user can quickly generate various Mock data to simplify the development and testing process. In development projects, we often need to generate some virtual data, such as virtual users, orders, addresses, etc. To simplify this process, Python provides the Faker library, a powerful Mock data generator. This article will introduce in detail the usage, function and code examples of the Faker library to help you better use Faker in your projects.

1. Introduction to Faker Library

Faker is a third-party library for Python used to generate virtual data. It supports the generation of place names, occupations, gender and other data around the world. The core function of the Faker library is to randomly generate similar data in the real world through a powerful generation algorithm. The Faker library is widely used in fields such as data testing, data cleaning and data filling.

2. Installation of Faker library

Installation of the Faker library is very simple, just use the pip command:

pip install Faker

3. Usage of Faker’s built-in test data

from faker import Faker
from collections import OrderedDict
# fake = Faker(["en_US", "zh_CN", "ja_JP"])
fake = Faker(["zh_CN"])
# print(fake.name())
# print(fake['en-US'].name())
# print(fake.company())

# 1. Use? #Customize rules to randomly generate strings
print(fake.bothify()) #Default generated string format: 05 RW
print(fake.bothify(text="666####", letters='Our Home')) # The string of letters is randomly given to text? Use,##default number instead, the format is: My My My 4777

# 2. Use ^ custom rules to randomly generate hexadecimal strings
print(fake.hexify(text='MAC Address: ^^:^^:^^:^^:^^:^^', upper=True)) # MAC Address: CD:18:FC:9F: B6:49

# 3. Randomly generate i18n language code
print(fake.language_code()) #yo

# 4. Use? Custom rules to randomly generate ASCII strings
print(fake.lexify(text='Random Identifier: ', letters='iABCDE')) # Random Identifier: CBCiDDiABE

# 5. Randomly generate i18n locale settings
print(fake.locale()) # zh_CH

# 6. Use #! @%Custom rules, randomly generate strings
print(fake.numerify(text='# @!! @ %')) # 1 98 7 (#=[0,9] %=[1,9] !=random number or null character @=non-0 numbers or null characters)

# 7. Randomly select object elements and randomly generate a list
print(fake.random_choices(elements=('a', 'b', 'c', 'd'), length=10)) # ['a', 'c ', 'c', 'd', 'a', 'd', 'd', 'b', 'b', 'a' ]
print(fake.random_choices(elements=OrderedDict([("a", 0.45), ("b", 0.35), ("c", 0.15), ("d", 0.05) , ]))) # ['b', 'c', 'a', 'a']

# 8. Randomly generate integers 0-9
print(fake.random_digit()) # 0

# 9. Randomly generate integers 1-9
print(fake.random_digit_not_null()) # 1

# 10. Randomly generate 0-9 integers or null values
print(fake.random_digit_or_empty()) # ""

# 11. Randomly select elements. The default is repeatable and the length is 1.
print(fake.random_element(elements=('a', 'b', 'c', 'd'))) # a
print(fake.random_element(elements=OrderedDict([("a", 0.45), ("b", 0.35), ("c", 0.15), ("d", 0.05) , ]))) # a

# 12. Randomly select elements, repeatable by default, variable length
print(fake.random_elements(elements=('a', 'b', 'c', 'd'), unique=False))
print(fake.random_elements(elements=OrderedDict([("a", 0.45), ("b", 0.35), ("c", 0.15), ("d", 0.05) , ]), length=20, unique=False))

# 13. Randomly generate integers within the specified range
print(fake.random_int(min=0, max=15, step=3))

# 14. Randomly generate ASCII string [a-zA-Z]
print(fake.random_letter()) # 'y'

# 15. Randomly generate ASCII string list [a-zA-Z]
print(fake.random_letters(length=10)) # ['R', 'N', 'v', 'n', 'A', 'v', \ 'O', 'p', 'y', 'E']

# 16. Randomly generate ASCII lowercase strings
print(fake.random_lowercase_letter()) # c

# 17. Randomly generate integers
'''
If digits is None (default value), the value range is a random integer between 1 and 9.
If fix_len is False (the default), all integers up to and including the number of digits can be generated.
If fix_len is True, only integers with an exact number of digits can be generated.
'''
print(fake.random_number(fix_len=True)) # 297371
print(fake.random_number(digits=3, fix_len=False)) # 577

# 18. Randomly generate a list with non-repeating elements that does not exceed the number of elements.
print(fake.random_sample(elements=('a', 'b', 'c', 'd', 'f', 'f'), length=6) ) # The elements can be the same, but the length cannot be greater than 6

# 19. Generate ASCII string with uppercase letters
print(fake.random_uppercase_letter())

# 20. Randomly generate an integer close to a certain number
'''
If le is False (the default), 140% of the generated quantity is allowed. If True, the cap generated is 100%.
If ge is False (the default), the allowed spawn count is reduced to 60%. If True, the lower bound generates an upper bound of 100%.
If a numeric value for the minimum value is provided, the resulting values less than the minimum value will be fixed at the minimum value.
If a numeric value is provided for max, the resulting values greater than max will be fixed at max.
If le and ge are both True, the value of number is automatically returned regardless of the supplied values of min and max.
'''
print(fake.randomize_nb_elements(number=100)) # 83
print(fake.randomize_nb_elements(number=100, le=True, ge=True, min=80)) # 100

# 21. Randomly generate address and postal number
print(fake.address())

# 22. Randomly generate house numbers
print(fake.building_number())

# 23. Randomly generate cities
print(fake.city())

# 24. Randomly generate special cities
print(fake.city_suffix())

# 25. Randomly generate countries
print(fake.country())

# 26. Randomly generate country numbers
print(fake.country_code())

# 27. Generate the current country
print(fake.current_country())

# 28. Generate the current country number
print(fake.current_country_code())

# 29. Randomly generate zip code
print(fake.postcode())

# 30. Randomly generate street addresses
print(fake.street_address())

# 31. Randomly generate street names
print(fake.street_name())

# 32. Randomly generate street name suffixes
print(fake.street_suffix())

# 33. Randomly generate automobile supplier license plates
print(fake.license_plate()) # 974-XXRA

# 34. Generate ABA routing transmission number
print(fake.aba())

# 35. Generate the ISO 3166-1 alpha-2 country code of the bank provider
print(fake.bank_country()) # GB

# 36. Generate basic bank account number (BBAN)
print(fake.bban()) #MAAN00447407504564

# 37. Generate international bank account number (IBAN)
print(fake.iban())

# 38. Generate SWIFT code
print(fake.swift(length=11, primary=True, use_dataset=True)) # SVWBGBNKXXX

# 39. Generate 11-digit SWIFT code
print(fake.swift11(use_dataset=True)) # SVWBGBNKXXX

# 40. Generate 8-digit SWIFT code
print(fake.swift8(use_dataset=True))

# 41. Generate EAN code
print(fake.ean(prefixes=('45', '49'), length=13)) # 4532804944052

# 42. Generate EAN13 code
print(fake.ean13(prefixes=('45', '49'))) # 4518561138095

# 43. Generate EAN8 code
print(fake.ean8(prefixes=('45', '49'))) # 45877841

# 44. Generate localized EAN barcodes of specified length
print(fake.localized_ean(length=8))

# 45. Generate localized EAN13 barcode of specified length
print(fake.localized_ean13())

# 46. Generate localized EAN8 barcode of specified length
print(fake.localized_ean8())

# 47. Generate random color values
print(fake.color(hue='red'))
print(fake.color(luminosity='light'))
print(fake.color(hue=(100, 200), color_format='rgb'))
print(fake.color(hue='orange', luminosity='bright'))
print(fake.color(hue=135, luminosity='dark', color_format='hsv'))
print(fake.color(hue=(300, 20), luminosity='random', color_format='hsl'))

# 48. Randomly generate color names
print(fake.color_name())

# 49. Generate a color in hexadecimal triplet format
print(fake.hex_color())

# 50. Generate a color in comma-separated RGB value format
print(fake.rgb_color())

# 51. Use CSS rgb() function to generate color format
print(fake.rgb_css_color())

# 52. Generate a network-safe color name
print(fake.safe_color_name())

# 53. Generate a network-safe color format as hexadecimal triple
print(fake.safe_hex_color())

# 54. Company related (technology\ideas\\
ame...)
print(fake.bs()) # leverage plug-and-play networks
print(fake.catch_phrase())
print(fake.company())
print(fake.company_suffix())

#55, credit card related
print(fake.credit_card_expire()) # 09/28
print(fake.credit_card_full()) # 'Discover\\
Katherine Fisher\\
6587647593824218 05/26\\
CVC: 892\\
'
print(fake.credit_card_number()) # 6504876475938248
print(fake.credit_card_provider()) # VISA 19 digit
print(fake.credit_card_security_code()) #604

# 56. Currency related
print(fake.cryptocurrency())
print(fake.cryptocurrency_code())
print(fake.cryptocurrency_name())
print(fake.currency())
print(fake.currency_code())
print(fake.currency_name())
print(fake.currency_symbol())
print(fake.pricetag())

# 57, time related
print(fake.am_pm())
print(fake.century())
print(fake.date())
print(fake.date_between())
print(fake.date_between_dates())
print(fake.date_object())
print(fake.date_of_birth())
print(fake.date_this_century())
print(fake.date_this_decade())
print(fake.date_this_month())
print(fake.date_this_year())
print(fake.date_time())
print(fake.date_time_ad())
print(fake.date_time_between())
print(fake.date_time_between_dates())
print(fake.date_time_this_century())
print(fake.date_time_this_decade())
print(fake.date_time_this_month())
print(fake.date_time_this_year())
print(fake.day_of_month())
print(fake.day_of_week())
print(fake.future_date())
print(fake.future_datetime())
print(fake.iso8601())
print(fake.month())
print(fake.month_name())
print(fake.past_date())
print(fake.past_datetime())
print(fake.pytimezone())
print(fake.time())
print(fake.time_delta())
print(fake.time_object())
print(fake.time_series())
print(fake.timezone())
print(fake.unix_time())
print(fake.year())

# 58. File related
print(fake.file_extension())
print(fake.file_extension(category='image'))
print(fake.file_name(category='audio'))
print(fake.file_name(extension='abcdef'))
print(fake.file_name(category='audio', extension='abcdef'))
print(fake.file_path(depth=3))
print(fake.file_path(depth=5, category='video'))
print(fake.file_path(depth=5, category='video', extension='abcdef'))
print(fake.mime_type())
print(fake.mime_type(category='application'))
print(fake.unix_device())
print(fake.unix_device(prefix='mmcblk'))
print(fake.unix_partition())
print(fake.unix_partition(prefix='mmcblk'))

# 59. Land coordinate data
print(fake.coordinate())
print(fake.latitude())
print(fake.latlng())
print(fake.local_latlng())
print(fake.location_on_land())
print(fake.longitude())

# 60. Internet related
print(fake.ascii_company_email()) # Email
print(fake.ascii_email()) # ascii email
print(fake.ascii_free_email())
print(fake.ascii_safe_email())
print(fake.company_email()) #Company email
print(fake.dga()) # URL
print(fake.domain_name()) # URL
print(fake.domain_word())
print(fake.email())
print(fake.free_email())
print(fake.free_email_domain())
print(fake.hostname())
print(fake.http_method()) # http request method
print(fake.iana_id()) # IANA registration ID
print(fake.ipv4()) #random ip
print(fake.ipv4_network_class()) # Network class
print(fake.ipv4_private())
print(fake.ipv4_public())
print(fake.ipv6())
print(fake.mac_address()) # mac address
print(fake.nic_handle()) # Network card processing ID
print(fake.nic_handles())
print(fake.port_number()) #Port number
print(fake.ripe_id()) #Organization ID
print(fake.safe_domain_name()) #Domain name
print(fake.safe_email()) # Email
print(fake.slug()) # Django algorithm
print(fake.tld()) #Domain name suffix
print(fake.uri()) # http request path
print(fake.uri_extension())
print(fake.uri_page()) # Request page name
print(fake.uri_path()) # Resource path
print(fake.url()) # url
print(fake.user_name()) # Username

# 61, isbn rules related
print(fake.isbn10())
print(fake.isbn13())

# 62. Job title
print(fake.job())

# 63. Article related
print(fake.paragraph(nb_sentences=5)) # Generate paragraphs
print(fake.paragraph(nb_sentences=5, variable_nb_sentences=False))
print(fake.paragraph(nb_sentences=5, ext_word_list=['abc', 'def', 'ghi', 'jkl']))
print(fake.paragraph(nb_sentences=5, variable_nb_sentences=False, ext_word_list=['abc', 'def', 'ghi', 'jkl']))
print(fake.paragraphs(nb=5)) # Generate paragraph list
print(fake.sentence(nb_words=10)) # Generate a sentence
print(fake.sentence(nb_words=10, variable_nb_words=False))
print(fake.sentences()) # Generate sentence list
print(fake.sentences(nb=5))
print(fake.text(max_nb_chars=20)) # Text string
print(fake.text(max_nb_chars=80))
print(fake.text(max_nb_chars=160))
print(fake.text(ext_word_list=['abc', 'def', 'ghi', 'jkl']))
print(fake.texts(nb_texts=5)) # Text string list
print(fake.texts(nb_texts=5, max_nb_chars=50))
print(fake.texts(nb_texts=5, max_nb_chars=50, ext_word_list=['abc', 'def', 'ghi', 'jkl']))
print(fake.word()) # word string
print(fake.word(ext_word_list=['abc', 'def', 'ghi', 'jkl']))
print(fake.words()) # word list
print(fake.words(nb=5, ext_word_list=['abc', 'def', 'ghi', 'jkl']))
print(fake.words(nb=4, ext_word_list=['abc', 'def', 'ghi', 'jkl'], unique=True))

# Data type related
print(fake.binary(length=64)) # Create bytes
print(fake.boolean(chance_of_getting_true=75)) #Boolean type
print(fake.csv(header=('Name', 'Address', 'Favorite Color'), data_columns=('{<!-- -->{name}}', \ '{<!-- -->{address}}', '{<!-- -->{safe_color_name}}'), num_rows=10, include_row_ids=True)) # Generate random comma separated values
print(fake.dsv(data_columns=('{<!-- -->{name}}', '{<!-- -->{address}}'), num_rows=5, delimiter= '$')) # Generate random delimiter separated values.
print(fake.fixed_width(data_columns=[(20, 'name'), (3, 'pyint', {'min_value':50, 'max_value':100})], align= 'right', num_rows=2)) # Generate random fixed width values
print(fake.image(size=(16, 16), hue=[90, 270], image_format='ico')) # Use the Python image library to generate an image and draw a random polygon on it. This provider will not run without it installed. Returns the bytes representing the image in the given format.
print(fake.json(data_columns=[('Name', 'name'), ('Points', 'pyint', {'min_value':50, 'max_value' :100})], num_rows=1)) # Generate random JSON structure values
print(fake.md5(raw_output=False)) # Generate MD5 data
print(fake.null_boolean()) # Generate null or boolean value
print(fake.password(length=12)) # Generate password
print(fake.password(length=40, special_chars=False, upper_case=False))
print(fake.psv(header=('Name', 'Address', 'Favorite Color'), data_columns=('{<!-- -->{name}}', \ '{<!-- -->{address}}', '{<!-- -->{safe_color_name}}'), num_rows=10, include_row_ids=True)) # Generate random pipe-separated values
print(fake.sha1(raw_output=False)) # Generate a random SHA1 hash
print(fake.sha256(raw_output=False)) # Generate a random SHA256 hash
print(fake.tar(uncompressed_size=256, num_files=32, min_file_size=4, compression='bz2')) # Generate a byte object containing a random valid tar file.
print(fake.tsv(header=('Name', 'Address', 'Favorite Color'), data_columns=('{<!-- -->{name}}', \ '{<!-- -->{address}}', '{<!-- -->{safe_color_name}}'), num_rows=10, include_row_ids=True)) # Generate random tab characters separated values
print(fake.uuid4()) # If specified using a callable object, generate a random UUID4 object and convert it to another type
print(fake.uuid4(cast_to=None))
print(fake.zip(uncompressed_size=256, num_files=32, min_file_size=4, compression='bz2')) # Generate a bytes object containing a random and valid zip archive file.

# People related
print(fake.first_name()) # Name of person
print(fake.first_name_female()) #Female name
print(fake.first_name_male()) # Male name
print(fake.first_name_nonbinary())
print(fake.language_name())
print(fake.last_name())
print(fake.last_name_female())
print(fake.last_name_male())
print(fake.last_name_nonbinary())
print(fake.name())
print(fake.name_female())
print(fake.name_male())
print(fake.name_nonbinary())
print(fake.prefix())
print(fake.prefix_female())
print(fake.prefix_male())
print(fake.prefix_nonbinary())
print(fake.suffix())
print(fake.suffix_female())
print(fake.suffix_male())
print(fake.suffix_nonbinary())

# Phone number related
print(fake.country_calling_code()) # Area code
print(fake.msisdn())
print(fake.phone_number())

# Personal information related
print(fake.profile())
print(fake.simple_profile())

# python related (python data type)
print(fake.pybool())
print(fake.pydecimal())
print(fake.pydict())
print(fake.pyfloat())
print(fake.pyint())
print(fake.pyiterable())
print(fake.pylist())
print(fake.pyset())
print(fake.pystr())
print(fake.pystr_format())
print(fake.pystruct())
print(fake.pytuple())

#ssn
print(fake.ssn()) # 865-50-6891

# Default user agent related, authentication information related, pass related
print(fake.android_platform_token())
print(fake.chrome())
print(fake.firefox())
print(fake.internet_explorer())
print(fake.ios_platform_token())
print(fake.linux_platform_token())
print(fake.linux_processor())
print(fake.mac_platform_token())
print(fake.mac_processor())
print(fake.opera())
print(fake.safari())
print(fake.user_agent())
print(fake.windows_platform_token())

4. Examples of usage scenarios of Faker library

The use of the Faker library is quite simple. There are mainly the following methods to generate virtual data:

1. Generate random strings

from faker import Faker

fake = Faker()
random_string = fake.pystr(length=10)
print(random_string)

2. Generate random place names

from faker import Faker


fake = Faker()
country = fake.country_code()
city = fake.city_name(country=country)
print(f"Country: {country}, City: {city}")

3. Generate random occupations

from faker import Faker


fake = Faker()
job = fake.job()
print(job)

4. Generate random gender

from faker import Faker


fake = Faker()
gender = fake.gender()
print(gender)

5. Generate random mobile phone number

When testing SMS verification, user registration and other scenarios, we need to generate a random mobile phone number. The Faker library can easily generate virtual mobile phone numbers. The sample code is as follows:

from faker import Faker


fake = Faker()
phone_number = fake.phone_number()
print(phone_number)

6. Generate random dates

When testing date-related functions or populating a database, we may need to generate random date data. The Faker library can generate random dates in various formats. The sample code is as follows:

from faker import Faker


fake = Faker()
random_date = fake.date_of_birth(minimum_age=18, maximum_age=65)
print(random_date)

7. Generate random colors

When designing and developing graphical interfaces or data visualizations, we may need to generate random color data. The Faker library can generate random colors in various formats. The sample code is as follows:

from faker import Faker


fake = Faker()
random_color = fake.hex_color()
print(random_color)

8. Generate random IP addresses

In scenarios such as network security testing and log analysis, we may need to generate random IP address data. The Faker library can generate legal random IP addresses. The sample code is as follows:

from faker import Faker


fake = Faker()
random_ip = fake.ipv4()
print(random_ip)

9. Generate random passwords

In scenarios such as user registration and account management, we need to generate random passwords. The Faker library can generate random passwords of various complexities. The sample code is as follows:

from faker import Faker


fake = Faker()
random_password = fake.password(length=8, special_chars=True, digits=True, upper_case=True, lower_case=True)
print(random_password)

10. Generate random company names

When testing enterprise-related functionality or populating a database, we may need to generate random company names. The Faker library can generate various types of random company names. The sample code is as follows:

from faker import Faker


fake = Faker()
company_name = fake.company()
print(company_name)

11. Generate virtual order data

When we need to test e-commerce, takeout and other order systems, we can use the Faker library to generate virtual order data. Here is an example of generating dummy order data::

from faker import Faker
import random

fake = Faker()

# Generate random product names
product_name = fake.product_name(length=10)

# Generate random product prices
product_price = random.randint(10, 100)

# Generate a random order number
order_id = fake.order_id(length=10)

# Generate random username
user_name = fake.username(length=10)

# Generate a random shipping address
address = fake.address()

# Generate random payment method
payment_method = fake.payment_method()

# Generate random order time
order_time = fake.between(start_date="-5y", end_date="today").isoformat()

# Output virtual order data
print(f"Order number: {order_id}\\
Product name: {product_name}\\
Product price: {product_price}\\
User name: {user_name}\\
Address: {address}\\
Payment Method: {payment_method}\\
Order time: {order_time}")

5. Summary

This article introduces the usage of the Faker library, more application scenarios, and provides rich code examples. The Faker library is very powerful in generating various virtual data and can be applied in various development and testing scenarios. By using the Faker library, developers can generate virtual data more efficiently and improve the efficiency of development and testing. I hope this article can help readers better understand and apply the Faker library.

Take action, it is better to be on the road than to wait and see all the time. In the future, you will definitely thank yourself for working hard now! If you want to learn and improve but can’t find the information and there is no one to answer your questions, please join the group in time: 786229024. There are various test and development materials and technologies in which you can communicate together.

Finally: The complete software testing video tutorial below has been compiled and uploaded. Friends who need it can get it by themselves [Guaranteed 100% Free]

Software testing interview document

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 385233 people are learning the system