Python generates test data-Faker (non-LOL player-Lee Sang Hyuk)

Python generates test data-Faker (non-LOL player-Lee Sang Hyuk)

Faker introduction

Official document:

Faker is a Python package that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you .

Faker is a Python package that generates fake data for you. Whether you need to guide your database, create beautiful XML documents, populate your persistence for stress testing, or anonymize data obtained from production services, Faker is for you.

Need to know before using

These data are all forged, the authenticity is not guaranteed, and it needs to be used with caution.


pip install Faker

Data return language (Simplified Chinese, Traditional Chinese, English...)

Used in Faker

Keyword (you don't need to specify this, you can pass in positional parameters directly) to specify
A kind
Many kinds
The language of fake data

from faker import Faker # Instantiate the Faker object, do not specify the data display language, the default is en_US: English faker_defalut = Faker() # Randomly return a name print ( # Nathan Munoz # Specify Chinese faker_CN = Faker(locale = 'zh_CN' ) print ( # Wan Yi'an # Randomly use a certain language in the locale list to return data fake = Faker(locale=[ 'en_US' , 'zh_CN' , 'zh_TW' ]) print ( # 5 random return data for _ in the Range ( 5 ): # variables here _ do not want to be used, but can be used, as long as we process it cycles Print ( ()) Copy the code

other languages

PS: The methods available for each language may vary:

Local document

Execute in the local directory:

python -m faker> docs.txt
Will be generated in the current directory
'S documentation

Common method

from faker import Faker() fake_CN = Faker(locale = 'zh_CN' ) # name() Generate name print ( # name_female() Female name print (faker_CN.name_female()) # # name_male() Male name print (faker_CN.name_male()) # # phone_number() phone number print (faker_CN.phone_number()) # 13506788385 # ssn() Generate ID number print (faker_CN.ssn()) # 37160319971003166X # postcode() postcode print (faker_CN.postcode()) # 176393 # address() will generate a detailed address and zip code, but this address may not be correct print (faker_CN.address()) # 418615, Block F, Changshouyang Street, Harbin County, Liaoning Province # company() Company name print ( # Lingyun Information Co., Ltd. # credit_card_number() credit card number print (faker_CN.credit_card_number()) # 2530679186722860 # date() Get the time: the default format %Y-%m-%d # 2000-08-02 print ( # Modify the format print ( '%Y-%m-% d %H:%M:%S' )) # 2020-04-26 11:52:57 # free_email() Email print (faker_CN.free_email()) # # job() Occupation print (faker_CN.job()) # Intelligence information analyst # bothify(text, letters) method # In the text keyword parameter: replace the # sign with a number from 0-9, and in the letters keyword: replace the? sign with any subcharacter of the string in the string can be used to generate the order number, Product number similar to data print (faker_CN.bothify(text = 'Product number: ??-########' , letters = 'JD' )) # Product number: DD-28748630 # ean(length,prefixes) barcode, length The total length of the barcode-can only be 8 or 13 (default), prefixes need to pass a tuple, if defined, a string in the tuple will be randomly removed and added before the generated barcode print (faker_CN.ean(length = 13 , prefixes=( '2020' , '2018' ,))) # 2020148459094 # text(max_nb_chars,) Generate a random string The maximum length of the string is 200 by default, and the minimum length is 5 print (faker_CN.text()) print (faker_CN.text(max_nb_chars = 5 )) # Operation browse. # image_url() Picture connection print (faker_CN.image_url()) # # ipv4_public() IPV4 address print (faker_CN.ipv4_public()) # # mac_address() mac address print (faker_CN.mac_address()) # 19:7b:7b:40:b5:ab # User_agent () generates a user agent Print (faker_CN.user_agent ()) # Opera/8.50. (X11; the Linux the x86_64; the RO-RO) Presto/2.9.168 Version/11.00 duplicated code


Only a part of it is listed here. It should be able to simulate a lot of data needed in testing work. If you need to supplement, you can go to Faker's official documentation to learn. One of the powerful aspects of Python is that there are many open source libraries that can be used directly.

Gitee source code address