接口自动化测试之随机生成手机号并进行数据库校验
文章目录- 一、python 生成随机手机号
- 二、数据库校验
代码如下(示例):
#不使用真实号段
import random
from Common.com_db import db
def __generator_phone():
p='1'
for i in range(0,10):#生成后10位
p +=str(random.randint(0,9))#生成0-9随机数
# print(p)
return p
print(__generator_phone())
#使用真实号段
p=[178,180,182,156,130,134,136,137,147,139,155]
def __phone():
index=random.randint(0,len(p)-1)
phone=str(p[index])
for i in range(0,8):#生成后8位
phone+=str(random.randint(0,9))#生成0-9随机数
# print(phone)
return phone
二、数据库校验
def get_newphone():
#连接数据库
while True:
#1、生成手机号
#phone = __phone()
phone=__generator_phone()
#2、校验数据库是否存在,存在则重新生成
try:
count=db.get_count('select * from futureloan.member where mobile_phone="{}"'.format(phone))
if count == 0:#手机号码没有在数据库查到,表示未注册的手机号
db.close()
print('手机号未注册')
return phone
except:
print('手机号生成有误')
raise
new_phone=get_newphone()
print(new_phone)



