承接上篇mysqlHelper类编写
系统注册需要使用手机号,作为自动化测试,那么就最好不是给特定的手机号,现在编写一个自动生成不重复手机号的方法。
# _*_conding:utf-8_*_
# 作者: 杨佳
# 创建时间: 2020/9/16 14:37
# 文件: createphone
import random
from scripts.mysqlconnect import MysqlHelper # 导入之前编写的mysql 的类
def create(n):
phone_list = []
# 手机号有做判断,错误的格式前端会报错,这个需要从开发那边拿到规则
prelist = ["130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "153",
"155", "156", "157", "158", "159", "186", "187", "188"]
while True:
# 可以自定义一次性生成多少个手机号
if len(phone_list) == n:
break
# 生成手机号
phone = random.choice(prelist) + "".join(random.choice("0123456789") for i in range(8))
# 不重复手机号
if phone not in phone_list:
# 初始化mysql类
mydb = MysqlHelper()
# 只有生成的手机号不在数据库中存在才能被注册
if not mydb.get_data(str(phone)):
phone_list.append(str(phone))
return phone_list
if __name__ == '__main__':
i = 0
for dat in create(100):
print(dat)
下一篇:excel 模块的编写