# 脚本名称:通过faker模块实现测试数据mock
# 应用场景:自动化测试过程中需要模拟数据,可通过faker模块构造测试参数
# 模块安装:pip3 install faker


from faker import Faker

# 实例化
mock_data = Faker(['en_US', 'zh_CN'])

# 通过列表生成式批量生成指定的测试数据
# 1、邮箱
emails = [mock_data.email() for _ in range(3)]
print(emails)
# 输出:['gangyao@example.net', 'qiulei@example.org', 'davidprince@example.com']

# 2、电话
phones = [mock_data.phone_number() for _ in range(5)]
print(phones)
# 输出:['18299171608', '401-499-5151x0307', '15039021618', '13357006186', '(481)060-2127x9183']

# 3、身份证
id_cards = [mock_data.ssn() for _ in range(5)]
print(id_cards)
# 输出:['405-46-6119', '445203200205206567', '496-01-0337', '420701195502069872', '705-33-3709']

# 4、国家
countries = [mock_data.country() for _ in range(5)]
print(countries)
# 输出:['土库曼斯坦', 'Denmark', 'Haiti', '澳大利亚', '刚果']

# 5、城市
cities = [mock_data.city() for _ in range(5)]
print(cities)
# 输出:['Robertsside', 'West Tracey', '张家港市', '瑜县', '博市']

# 可借助dir()查看faker支持的所有方法
print(dir(mock_data))


↙↙↙阅读原文可查看相关链接,并与作者交流