接口自动化如何造测试数据--faker

一、基本的四要素数据

姓名:name = fk.name()
身份证:ssn = fk.ssn()
手机号:phone = fk.phone_number()
银行卡:fk.credit_card_number()

二、个人信息

地址:address = fk.address()  地址+邮编
国家:country = fk.country()
省份:pro = fk.province()
城市:res = fk.city()
邮编:res1 = fk.postcode()
邮箱:email = fk.email()
公司:company = fk.company()
岗位:job = fk.job()
生成所有的人物信息【复杂】:info = fk.profile()
生成所有的人物信息【简单】:simple = fk.simple_profile()

三、随机数

nums = fk.random_int(min=100,max=999)

四、文本:评论、备注信息

随机字符串:pystr = fk.pystr()
随机生成1个词语:word = fk.word()
随机生成多个词语:words = fk.words()
随机生成一句话:sents = fk.sentence()
随机生成一篇文章:text = fk.text()

五、时间相关的

生成年份:year = fk.year()   1970————现在
生成月份:month = fk.month()  1————12
生成年月日:date = fk.date()
生成当前年:this_year = fk.date_this_year()
生成当前月:this_month = fk.date_this_month()
年-月-日 时:分:秒:res = fk.date_time()
指定时间范围的年月日:between = fk.date_between(start_date= "-1y",end_date="today")
指定时间范围的年月日时分秒 --时间戳:between1 = fk.date_time_between(start_date= "-2y",end_date="now")
未来时间年月日:res1 = fk.future_date()
未来时间年月日时分秒:res2 = fk.future_datetime()

六、数据不重复

注意:生成多个数据的时候用---列表推导的方法
phones = [fk.unique.phone_number() for i in range(5)]

七、代码演示

from faker import Faker

"""
个人信息类,姓名、身份证、银行卡、手机号、地址、国家、省份、城市、邮编、邮箱、公司、岗位,复杂或简单的基本信息
"""
fk = Faker(locale="zh-cn")
name = fk.name()
print(name)
list1 = []
try:
    for ph in range(10):  # 次数
        if ph <= 10:
            phone = fk.phone_number()
            if phone[0:3] == "152":
                list1.append(phone)
except Exception as e:
    print(e)
print(list1)

ssn = fk.ssn()
print(ssn)
card = fk.credit_card_number()
print(card)

address = fk.address()
print(address)
country = fk.country()
print(country)
pro = fk.province()
print(pro)

city = fk.city()
print(city)
res = fk.postcode()
print(res)
res = fk.email()
print(res)
company = fk.company()
print(company)
job = fk.job()
print(job)

info = fk.profile()
print(info)
info1 = fk.simple_profile()
print(info1)

# 随机数
nums = fk.random_int(min=100, max=999)
print(nums)

# 文本:评论、备注信息
"""
随机生成字符串、词语、多个词语、一句话、一篇文章
"""
pystr = fk.pystr()
print(pystr)

word = fk.word()
print(word)

word = fk.words()
print(word)

sentence = fk.sentence()
print(sentence)
text = fk.text()
print(text)

"""
时间相关的
"""
year = fk.year()  # 1970-现在
print(year)
month = fk.month()  # 1----12
print(month)
date = fk.date()
print(date)

this_year = fk.date_this_year()  # 生成当前年的时间 2021-06-13
print(this_year)
this_month = fk.date_this_month()  # 生成当前月的时间 2021-12-09
print(this_month)

# 年月日时分秒  1974-11-02 12:32:17
res = fk.date_time()
print(res)

"""
指定范围的年月日和时分秒
"""
between = fk.date_between(start_date="-1y", end_date="today")
print(between)  # 2021-09-15
between = fk.date_time_between(start_date="-2y", end_date="now")
print(between)  # 2020-12-12 00:06:23

"""
未来时间的年月日:
"""
res1 = fk.future_date()
print(res1)  # 2022-01-04
res2 = fk.future_datetime()
print(res2)  # 2022-01-04 02:53:23

# 列表推导法
phones = [fk.unique.phone_number() for i in range(5)]
print(phones)

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试小白00

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值