Test learning-109- test data batch generation (Faker framework-clear version)

Foreword:

The abbreviated version can be viewed on my previous blog.

As a test development engineer, it is inevitable that you need to use data for testing during testing. Sometimes you need to generate a certain amount of fake data in batches for testing. It is really too troublesome to construct by yourself. Today I will talk about a very good way of constructing fake data. s frame. There is a very powerful library called Faker in Python, which can automatically help us generate a variety of real-looking "fake" data. Let's learn about this Faker library together.

I believe everyone has learned Python, so I won't say more here.

Install Faker library

pip3 install faker

Import Faker library call

# coding:utf8
from faker import Faker
 
# 中文数据
faker = Faker('zh_CN')

A clear version of Faker's various types of data structures

1. Address

faker.address()
# '新疆维吾尔自治区杰县南湖武汉街D座 253105'
faker.building_number()
# 'B座'
faker.city()
# '璐县'
faker.city_name()
# '贵阳'
faker.city_suffix()
# '县'
faker.country()
# '阿拉斯加'
faker.country_code(representation="alpha-2")
# 'CR'
faker.district()
# '西峰'
faker.postcode()
# '726749'
faker.province()
# '福建省'
faker.street_address()
# '余路N座'
faker.street_name()
# '李路'
faker.street_suffix()
# '路'

2. Color

faker.color_name()
# 'DarkKhaki'
faker.hex_color()
# '#97d14e'
faker.rgb_color()
# '107,179,51'
faker.rgb_css_color()
# 'rgb(20,46,70)'
faker.safe_color_name()
# 'navy'
faker.safe_hex_color()
# '#dd2200'

3. Company

faker.bs()
# 'grow rich initiatives'
faker.catch_phrase()
# 'Self-enabling encompassing function'
faker.company()
# '恒聪百汇网络有限公司'
faker.company_prefix()
# '晖来计算机'
faker.company_suffix()
# '信息有限公司'
Credit Card

Credit Card,用于生成信用卡相关数据,如过期时间、银行卡号、安全码等内容,用法如下:

faker.credit_card_expire(start="now", end="+10y", date_format="%m/%y")
# '08/20'
faker.credit_card_full(card_type=None)
# 'Mastercard\n玉兰 范\n5183689713096897 01/25\nCVV: 012\n'
faker.credit_card_number(card_type=None)
# '4009911097184929918'
faker.credit_card_provider(card_type=None)
# 'JCB 15 digit'
faker.credit_card_security_code(card_type=None)
# '259'

4. Date Time

faker.am_pm()
# 'AM'
faker.century()
# 'X'
faker.date(pattern="%Y-%m-%d", end_datetime=None)
# '1997-06-16'
faker.date_between(start_date="-30y", end_date="today")
# datetime.date(2000, 8, 30)
faker.date_between_dates(date_start=None, date_end=None)
# datetime.date(2019, 7, 30)
faker.date_object(end_datetime=None)
# datetime.date(1978, 3, 12)
faker.date_of_birth(tzinfo=None, minimum_age=0, maximum_age=115)
# datetime.date(2012, 6, 3)
faker.date_this_century(before_today=True, after_today=False)
# datetime.date(2011, 6, 12)
faker.date_this_decade(before_today=True, after_today=False)
# datetime.date(2011, 8, 22)
faker.date_this_month(before_today=True, after_today=False)
# datetime.date(2019, 7, 25)
faker.date_this_year(before_today=True, after_today=False)
# datetime.date(2019, 7, 22)
faker.date_time(tzinfo=None, end_datetime=None)
# datetime.datetime(2018, 8, 11, 22, 3, 34)
faker.date_time_ad(tzinfo=None, end_datetime=None, start_datetime=None)
# datetime.datetime(1566, 8, 26, 16, 25, 30)
faker.date_time_between(start_date="-30y", end_date="now", tzinfo=None)
# datetime.datetime(2015, 1, 31, 4, 14, 10)
faker.date_time_between_dates(datetime_start=None, datetime_end=None, tzinfo=None)
# datetime.datetime(2019, 7, 30, 17, 51, 44)
faker.date_time_this_century(before_now=True, after_now=False, tzinfo=None)
# datetime.datetime(2002, 9, 25, 23, 59, 49)
faker.date_time_this_decade(before_now=True, after_now=False, tzinfo=None)
# datetime.datetime(2010, 5, 25, 20, 20, 52)
faker.date_time_this_month(before_now=True, after_now=False, tzinfo=None)
# datetime.datetime(2019, 7, 19, 18, 4, 6)
faker.date_time_this_year(before_now=True, after_now=False, tzinfo=None)
# datetime.datetime(2019, 3, 15, 11, 4, 18)
faker.day_of_month()
# '04'
faker.day_of_week()
# 'Monday'
faker.future_date(end_date="+30d", tzinfo=None)
# datetime.date(2019, 8, 12)
faker.future_datetime(end_date="+30d", tzinfo=None)
# datetime.datetime(2019, 8, 24, 2, 59, 4)
faker.iso8601(tzinfo=None, end_datetime=None)
# '1987-07-01T18:33:56'
faker.month()
# '11'
faker.month_name()
# 'August'
faker.past_date(start_date="-30d", tzinfo=None)
# datetime.date(2019, 7, 25)
faker.past_datetime(start_date="-30d", tzinfo=None)
# datetime.datetime(2019, 7, 18, 22, 46, 51)
faker.time(pattern="%H:%M:%S", end_datetime=None)
# '16:22:30'
faker.time_delta(end_datetime=None)
# datetime.timedelta(0)
faker.time_object(end_datetime=None)
# datetime.time(22, 12, 15)
faker.time_series(start_date="-30d", end_date="now", precision=None, distrib=None, tzinfo=None)
# <generator object Provider.time_series at 0x7fcbce0604f8>
faker.timezone()
# 'Indian/Comoro'
faker.unix_time(end_datetime=None, start_datetime=None)
# 1182857626
faker.year()
# '1970'

5. File file

faker.file_extension(category=None)
# 'flac'
faker.file_name(category=None, extension=None)
# '然后.numbers'
faker.file_path(depth=1, category=None, extension=None)
# '/关系/科技.mov'
faker.mime_type(category=None)
# 'video/ogg'
faker.unix_device(prefix=None)
# '/dev/sdd'
faker.unix_partition(prefix=None)
# '/dev/xvds3'

6. Geo location data

faker.coordinate(center=None, radius=0.001)
# Decimal('-114.420686')
faker.latitude()
# Decimal('-9.772541')
faker.latlng()
# (Decimal('-27.0730915'), Decimal('-5.919460'))
faker.local_latlng(country_code="US", coords_only=False)
# ('41.47892', '-87.45476', 'Schererville', 'US', 'America/Chicago')
faker.location_on_land(coords_only=False)
# ('12.74482', '4.52514', 'Argungu', 'NG', 'Africa/Lagos')
faker.longitude()
# Decimal('40.885895')

7. Internet Internet data

faker.ascii_company_email(*args, **kwargs)
# '[email protected]'
faker.ascii_email(*args, **kwargs)
# '[email protected]'
faker.ascii_free_email(*args, **kwargs)
# '[email protected]'
faker.ascii_safe_email(*args, **kwargs)
# '[email protected]'
faker.company_email(*args, **kwargs)
# '[email protected]'
faker.domain_name(levels=1)
# 'xiulan.cn'
faker.domain_word(*args, **kwargs)
# 'luo'
faker.email(*args, **kwargs)
# '[email protected]'
faker.free_email(*args, **kwargs)
# '[email protected]'
faker.free_email_domain(*args, **kwargs)
# 'yahoo.com'
faker.hostname(*args, **kwargs)
# 'lt-18.pan.cn'
faker.image_url(width=None, height=None)
# 'https://placekitten.com/51/201'
faker.ipv4(network=False, address_class=None, private=None)
# '192.233.68.5'
faker.ipv4_network_class()
# 'a'
faker.ipv4_private(network=False, address_class=None)
# '10.9.97.93'
faker.ipv4_public(network=False, address_class=None)
# '192.51.22.7'
faker.ipv6(network=False)
# 'de57:9c6f:a38c:9864:10ec:6442:775d:5f02'
faker.mac_address()
# '99:80:5c:ab:8c:a9'
faker.safe_email(*args, **kwargs)
# '[email protected]'
faker.slug(*args, **kwargs)
# ''
faker.tld()
# 'cn'
faker.uri()
# 'http://fangfan.org/app/tag/post/'
faker.uri_extension()
# '.php'
faker.uri_page()
# 'about'
faker.uri_path(deep=None)
# 'app'
faker.url(schemes=None)
# 'http://mingli.cn/'
faker.user_name(*args, **kwargs)
# 'jie54'
Job

Job,用于生成和职业相关的数据,用法如下:

faker.job()
# '烫工'

8. Lorem fake text data

faker.paragraph(nb_sentences=3, variable_nb_sentences=True, ext_word_list=None)
# '包括的是报告那些一点.图片地址基本全部.'
faker.paragraphs(nb=3, ext_word_list=None)
# [   '计划规定这样所以组织商品其中.参加成为不同发表地区.精华科技谢谢大家需要.一下手机上海中文工程.',
#     '非常相关是一就是一个一种文章发生.增加那些以及之后以下你的.',
#     '学生应该出来分析增加关系组织.评论来源朋友注册应该需要单位.感觉最后无法发现选择人民.']
faker.sentence(nb_words=6, variable_nb_words=True, ext_word_list=None)
# '介绍结果自己解决处理.'
faker.sentences(nb=3, ext_word_list=None)
# ['查看其实一次学习登录浏览是一他们.', '而且资源的人事情.', '科技价格免费大学教育.']
faker.text(max_nb_chars=200, ext_word_list=None)
# ('只是当前国内中文所以.威望系统在线虽然.\n'
#  '图片人民非常合作这种谢谢更新.名称详细直接社会一直首页完全.\n'
#  '重要更多只要市场.必须只是学生音乐.系统美国类别这些一切环境.\n'
#  '但是的话人民美国关于.\n'
#  '情况专业国际看到研究.音乐环境市场搜索发现.\n'
#  '工具还是到了今天位置人民.留言作者品牌工程项目必须.上海精华现在我们新闻应该关系.\n'
#  '更新经济能力全部资源如果.手机能够登录国内.')
faker.texts(nb_texts=3, max_nb_chars=200, ext_word_list=None)
# [   '成功可能推荐你的行业.地区而且推荐.\n'
#     '网络不断是一主要必须.开始安全服务.\n'
#     '应该网上通过以后通过大学.管理要求有关国际阅读当前.为了应该结果点击公司开始怎么.\n'
#     '成功一次最大生产网站.这种加入她的地址有限.\n'
#     '根据新闻汽车起来非常主题显示必须.有些建设来自作者电话支持.\n'
#     '只是资源还是由于经济事情喜欢.为什中文大小得到服务.网络密码是否免费参加一次社区欢迎.',
#     '部门活动技术.商品影响发生行业密码完成.就是部门结果资料学习当然.或者帮助城市要求首页市场教育你们.\n'
#     '专业完全分析处理城市大学什么.\n'
#     '文件非常国际全部起来积分公司.资料的是电影没有.这是本站需要.\n'
#     '合作重要没有现在市场开发空间.您的会员推荐成功教育进行中国.\n'
#     '文件不是如果评论.因为经验设备规定.\n'
#     '加入一起影响网上大家运行在线如果.工程企业这种以后.',
#     '空间市场出现必须基本电话.显示一个标准其他设计作品.工程不断新闻问题更多更新这么.\n'
#     '一起简介网上内容不会.任何知道各种两个.类别事情经营那么投资市场.\n'
#     '那些使用介绍公司朋友人民你们浏览.应该表示一点一般说明主要谢谢.电话回复起来经验一个来源加入.\n'
#     '地区法律其他表示虽然.参加社会喜欢有限论坛一般发布.类别目前文化可以.\n'
#     '报告质量工作主要.企业发布完全.得到名称作者等级两个论坛只要电话.']
faker.word(ext_word_list=None)
# '注意'
faker.words(nb=3, ext_word_list=None, unique=False)
# ['责任', '组织', '以后']

9. Misc, obfuscating data, such as encrypted content such as password, sha1, sha256, md5, etc.

faker.boolean(chance_of_getting_true=50)
# True
faker.md5(raw_output=False)
# '3166fa26ffd3f2a33e020dfe11191ac6'
faker.null_boolean()
# False
faker.password(length=10, special_chars=True, digits=True, upper_case=True, lower_case=True)
# 'W7Ln8La@%O'
faker.sha1(raw_output=False)
# 'c8301a2a79445439ee5287f38053e4b3a05eac79'
faker.sha256(raw_output=False)
# '1e909d331e20cf241aaa2da894deae5a3a75e5cdc35c053422d9b8e7ccfa0402'
faker.uuid4(cast_to=<class 'str'>)
# '6e6fe387-6877-48d9-94ea-4263c4c71aa5'

10. Person and people related

faker.first_name()
# '颖'
faker.first_name_female()
# '芳'
faker.first_name_male()
# '利'
faker.first_romanized_name()
# 'Jing'
faker.last_name()
# '温'
faker.last_name_female()
# '寇'
faker.last_name_male()
# '陈'
faker.last_romanized_name()
# 'Lei'
faker.name()
# '黄明'
faker.name_female()
# '张凯'
faker.name_male()
# '黄鹏'

print(faker.name()) # 姓名
print(faker.ssn()) #身份证号
print(faker.country()) # 国家
print(faker.postcode()) # 邮编
print(faker.credit_card_number()) # 信用卡号
print(faker.city_suffix()) #
print(faker.file_name()) #文件名带后缀
print(faker.file_path()) #路径带文件
print(faker.domain_name())#域名

 

11. User-Agent related content of the User-Agent browser,

faker.chrome(version_from=13, version_to=63, build_from=800, build_to=899)
# ('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/5332 (KHTML, like Gecko) '
#  'Chrome/40.0.837.0 Safari/5332')
faker.firefox()
# ('Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_8_9; rv:1.9.4.20) '
#  'Gecko/2019-05-02 05:58:44 Firefox/3.6.19')
faker.internet_explorer()
# 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/3.0)'
faker.linux_platform_token()
# 'X11; Linux i686'
faker.linux_processor()
# 'x86_64'
faker.mac_platform_token()
# 'Macintosh; U; PPC Mac OS X 10_12_5'
faker.mac_processor()
# 'U; Intel'
faker.opera()
# 'Opera/9.77.(Windows NT 4.0; vi-VN) Presto/2.9.182 Version/11.00'
faker.safari()
# ('Mozilla/5.0 (Macintosh; PPC Mac OS X 10_7_1 rv:5.0; or-IN) '
#  'AppleWebKit/535.9.4 (KHTML, like Gecko) Version/5.0.2 Safari/535.9.4')
faker.user_agent()
# 'Opera/8.69.(X11; Linux i686; ml-IN) Presto/2.9.170 Version/11.00'
faker.windows_platform_token()
# 'Windows NT 6.1'

This blog refers to the blog: https://blog.csdn.net/qq_42692386/article/details/107359656

Faker's official website, official document link  https://faker.readthedocs.io/en/master/locales/zh_CN.html

12. Supplement faker function

Common operation functions of the Python Faker library (for personal use)
country(): country
province(): province
city_suffix(): city, county
district(): district
street_address(): street address
street_name(): street name
street_suffix(): street, road
country_code(): country code
postcode(): postcode
geo_coordinate(): geographic coordinates
longitude(): longitude
latitude(): latitude
lexify(): replace all question marks? With random events
numerify(): generate a three-digit random number
random_digit(): generate a random number from 0 to 9
random_digit_not_null(): generate a random number from 1 to 9
random_element(): generate a random letter
random_int(): a random number, default 0 ~9999, can be modified by the min and max parameters
random_letter(): random letters
random_number(): random numbers, the number of digits generated by the parameter digits setting
color_name(): random color name
hex_color(): random HEX color
rgb_color(): random RGB color
safe_color_name(): random safe color name
safe_hex_color(): random safe HEX color
bs(): random company service name
company(): random company name (long)
company_prefix(): random company name (short)
company_suffix(): company nature
credit_card_expire(): Random credit card expiration date
credit_card_full(): Generate complete credit card information
credit_card_number(): Credit card number
credit_card_provider(): Credit card type
credit_card_security_code(): Credit card security code
currency_code(): Currency code
am_pm(): AM/PM
century (): Random century
date(): Random date
date_between(): Randomly generate dates within the specified range, parameters: start_date, end_date
date_between_dates(): Randomly generate dates within the specified range, usage is the same as above
date_object(): Randomly produced from 1970-1 -1 to a random date from the specified date.
date_this_month():
date_this_year():
date_time(): randomly generate a specified time (January 1, 1970 to present)
date_time_ad(): generate a random time from 1 AD to the present
date_time_between(): Same usage as dates
future_date(): future date
future_datetime(): future time
month(): random month
month_name(): random month (English)
past_date(): random generation of past dates
past_datetime(): random generation Elapsed
time time(): random 24-hour time
timedelta(): random time difference
time_object(): random 24-hour time, time object
time_series(): random TimeSeries object
timezone(): random time zone
unix_time(): random Unix time
year(): random year
file_extension(): random file extension
file_name(): random file name (including extension, excluding path)
file_path(): random file path (including file name, extension)
mime_type(): random mime Type
ascii_company_email(): random ASCII company mailbox name
ascii_email(): random ASCII mailbox
ascii_free_email():
ascii_safe_email():
company_email():
domain_name(): generate domain name
domain_word(): domain word (ie, suffix not included)
email():
free_email():
free_email_domain():
f.safe_email(): safe mailbox
f.image_url(): random URL address
ipv4(): random IP4 address
ipv6 (): Random IP6 address
mac_address(): Random MAC address
tld(): URL domain name suffix
uri(): Random URI address
uri_extension(): URL file suffix
uri_page(): URL file (not including suffix)
uri_path(): URL File path (not including file name)
url(): random URL address
user_name(): random user name
isbn10(): random ISBN (10 digits)
isbn13(): random ISBN (13 digits)
job(): random position
paragraph( ): randomly generate a paragraph
paragraphs(): randomly generate multiple paragraphs, control the number of paragraphs through the parameter nb, return the array
sentence(): randomly generate a
sentence sentences(): randomly generate multiple sentences, similar to
text() : Randomly generate an article
word(): Randomly generate words
words(): Randomly generate multiple words, usage and paragraphs, sentences, similar
binary(): Randomly generate binary code
boolean(): True/False
language_code(): Randomly generate two-digit language code
locale(): Randomly generate language/international information
md5(): Randomly generate MD5
null_boolean(): NULL/True/ False
password(): randomly generate a password, optional parameters: length: password length; special_chars: whether special characters can be used; digits: whether to include numbers; upper_case: whether to include uppercase letters; lower_case: whether to include lowercase letters
sha1(): random SHA1
sha256(): random SHA256
uuid4(): random UUID
first_name():
first_name_female(): female name
first_name_male(): male name
first_romanized_name(): Roman name
last_name():
last_name_female(): female
last_name_male(): male
last_romanized_name ():
name(): randomly generated name
name_female(): male name
name_male(): female name
romanized_name(): roman name
msisdn(): mobile station international user identification code, that is, the mobile user’s ISDN number
phone_number(): Randomly generate mobile phone number
phonenumber_prefix(): Randomly generate mobile phone number segment
profile(): Randomly generate profile information
simple_profile(): Randomly generate simple profile information

Specify the type of data generated randomly:
pybool ():
pydecimal ():
pydict ():
pyfloat (): = 2 # left_digits generated integer bits,
right_digits generated # = number of decimal places. 1,
positive only if # = True positive
pyint():
pyiterable()
pylist()
pyset()
pystr()
pystruct()
pytuple()
ssn(): generate ID number
chrome(): randomly generate Chrome browser user_agent information
firefox(): randomly generate FireFox Browser user_agent information
internet_explorer(): randomly generate IE browser user_agent information
opera(): randomly generate Opera browser user_agent information
safari(): randomly generate Safari browser user_agent information
linux_platform_token(): random Linux information
user_agent() : Random user_agent information

Guess you like

Origin blog.csdn.net/u013521274/article/details/110005224