2025年4月21日 星期一 乙巳(蛇)年 正月廿二 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

使用faker生成测试数据

时间:07-03来源:作者:点击数:30

使用faker生成测试数据

  • # 数据类(构造常用数据类型、UUID、文本、词组、文件链接、文件路径)
  • # 安全类(构造操作系统信息、HASH加密、密码)
  • # 信息类(构造个人信息数据和表单信息数据:姓名、地址、电话、工作、证件号、
  • # 银行卡号、公司、邮箱、生日)
  • # 网络类(构造IP MAC HTTP的客户端类型和文件类型,反反爬)
  • from faker import Factory
  • fake = Factory().create('zh_CN') # create('zh_CN'):制定地区
  • def random_python_data():
  • return fake.pystr(), \
  • fake.pyint(), \
  • fake.pybool(), \
  • fake.pyfloat(), \
  • fake.pytuple(nb_elements=2), \
  • fake.pylist(nb_elements=2), \
  • fake.pydict(nb_elements=2) # nb_elements长度
  • def random_uuid():
  • return fake.uuid4()
  • def random_text():
  • return fake.text()
  • def random_word():
  • return fake.word(), fake.words()
  • def random_image_url(): # 图片地址
  • return fake.image_url()
  • def random_file_path(): # 文件路径
  • return fake.file_path()
  • def random_os_info(os_type: str = 'win'): # 操作系统
  • if os_type == 'win':
  • return fake.windows_platform_token() + ' ' + fake.linux_processor()
  • if os_type == 'linux':
  • return fake.linux_processor()
  • if os_type == 'mac':
  • return fake.mac_platform_token()
  • if os_type == 'ios':
  • return fake.ios_platform_token()
  • if os_type == 'android':
  • return fake.android_platform_token()
  • return None
  • def random_hash(raw_output: bool = False): # raw_output是否以二进制形式返回 hash值
  • return {'md5': fake.md5(raw_output), 'sha1': fake.sha1(raw_output), 'sha256': fake.sha256(raw_output)}
  • def random_password(length=10, special_chars=False, digits=True, upper_case=False, lower_case=False): # 密码
  • return fake.password(length=length,
  • special_chars=special_chars,
  • digits=digits,
  • upper_case=upper_case,
  • lower_case=lower_case)
  • def random_name(): # 名称
  • return fake.name()
  • def random_address(): # 地址
  • return fake.address()
  • def random_phone_number(): # 手机号码
  • return fake.phone_number()
  • def random_job(): # 职业
  • return fake.job()
  • def random_ssn(*args): # 证件号
  • return fake.ssn(*args)
  • def random_credit_card(): # 银行卡号
  • return fake.credit_card_full(), fake.credit_card_number()
  • def random_company(): # 公司信息
  • return fake.company()
  • def random_email(domain): # 邮箱
  • return fake.email(domain)
  • def random_birth(minimum_age=18, maximum_age=25): # 生日
  • return fake.date_of_birth(minimum_age=minimum_age, maximum_age=maximum_age)
  • def random_profile(): # 个人简介信息
  • return fake.profile()
  • def random_ip4(private=False, public=False): # ip4
  • if private:
  • return fake.ipv4_private() # 局域网ip
  • if public:
  • return fake.ipv4_public() # 公网ip
  • return fake.ipv4() # 随机ip
  • def random_ip6(): # ip6
  • return fake.ipv6()
  • def random_mac_address(): # mac地址
  • return fake.mac_address()
  • def random_user_agent(): # user_agent
  • return fake.user_agent()
  • def random_mime_type(mime_type: str = 'application'): # 文件类型
  • return fake.mime_type(mime_type)
  • print(random_mime_type('image'))

 

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门