当自动化测试过程中有需要用到数据库这部分的时候,我们应该怎么办呢?有没有什么好的方法呢?本篇教程会给大家讲解一下数据库的三层架构的案例应用。比如我们都知道这样一个场景。用户一般登录时候首先要输入账号和秘密,然后点击登录,通过发送request给数据库,数据库这时候需要做的两个事情,一个是检查有没有这个用户,第二个检查用户信息是否正确。我们接下来针对这个应用场景结合mysql去了解一下三层架构的处理逻辑,,,,
第一层:数据库的查询层 我们先把操作数据查询这部分,我们写一个类:
class MySQLHeper(object):
def __init__(self):
self.conn=conn()
def get_one(self,sql,params): #我们查询一条语句,返回一条语句'''查询数据返回'''cur=self.conn.cursor()
cur.execute(sql,params) # 执行语句returncur.fetchone()
cur.close() # 关闭游标连接池
self.conn.close() # 关闭数据库连接池
her = MySQLHeper() #实例化
sql = "select * from USER WHERE id=%s"
params = (1,)
print(her.get_one(sql,params))
返回结果:
C:\Python27\python2.exe D:/project/JieKou/page/mysqTests.py
(1L, 'fighter', 'admin', 'sanpang')
或者我们调用时这样去写,
class MySQLHeper(object):
def __init__(self):
self.conn=conn()
def get_one(self,sql,params):'''查询数据返回'''cur=self.conn.cursor()
cur.execute(sql,params) # 执行语句returncur.fetchone()
cur.close() # 关闭游标连接池
self.conn.close() # 关闭数据库连接池
her=MySQLHeper() #实例化
sql= "select * from user WHERE username=%s and passsword=%s"
params = ('fighter','admin')
print(her.get_one(sql,params))
返回结果:
(1L, 'fighter', 'admin', 'sanpang')
首先用户输入账号和密码发送请求给数据库去查询,1.检查数据在不在,2.数据对不对。要经过数据层的处理,,,
第二层:检查数据是否存在
然后我们定义一个类 CheckUserinfo 去检查数据库中是否存在这个用户名和密码这一部分。
classCheckUserinfo(MySQLHeper):
def __init__(self):
self.__helper=MySQLHeper() #继承类
def userinfo(self,sql,params):return self.__helper.get_one(sql,params)
sql = "select * from user WHERE username=%s and passsword=%s"
params = ('fighter','admin')
her = CheckUserinfo() #实例化
print (her.userinfo(sql,params))
返回结果:
(1L, 'fighter', 'admin', 'sanpang')
第三层:交互层代码如下,如web上面的表单 输入信息 看到的东西输入账号和密码 界面给我们的一些信息的返回
def info():
check=CheckUserinfo()
username= raw_input('请输入您的账号:')
password= raw_input('请输入您的密码:')
sql= "select * from user WHERE username=%s and passsword=%s"
params =(username,password)
# print (check.userinfo(sql,params))
result= check.userinfo(sql,params)
print (result)
print (result[1])ifresult:
print ('恭喜你!{} 登录成功!'.format(result[1]))else:
print ('sorry!您的用户名或者密码错误!请检查!')
info()
查询结果:
请输入您的账号:fighter
请输入您的密码:admin
(1L, 'fighter', 'admin', 'sanpang')
fighter
恭喜你!fighter 登录成功!
输入正确的用户名和错误的密码:
def info():
check=CheckUserinfo()
username= raw_input('请输入您的账号:')
password= raw_input('请输入您的密码:')
sql= "select * from user WHERE username=%s and passsword=%s"
params =(username,password)
# print (check.userinfo(sql,params))
result= check.userinfo(sql,params)ifresult:
print ('恭喜你!{} 登录成功!'.format(result[1]))else:
print ('sorry!您的用户名或者密码错误!请检查!')
info()
输入结果如下:
C:\Python27\python2.exe D:/project/JieKou/page/mysqTests.py
请输入您的账号:fighter
请输入您的密码:1231sorry!您的用户名或者密码错误!请检查!
Mysql的三层架构的实例完整代码如下:
#第三层:数据库的查询层 查询数据class MySQLHeper(object):
def __init__(self):
self.conn=conn()
def get_one(self,sql,params):'''查询数据返回'''cur=self.conn.cursor()
cur.execute(sql,params) # 执行语句returncur.fetchone()
cur.close() # 关闭游标连接池
self.conn.close() # 关闭数据库连接池
#第二层:数据库检查数据classCheckUserinfo(MySQLHeper):
def __init__(self):
self.__helper=MySQLHeper() #继承类
def userinfo(self,sql,params):return self.__helper.get_one(sql,params)
#第一层:交互层
def info():
check=CheckUserinfo()
username= raw_input('请输入您的账号:')
password= raw_input('请输入您的密码:')
sql= "select * from user WHERE username=%s and passsword=%s"
params =(username,password)
# print (check.userinfo(sql,params))
result= check.userinfo(sql,params)ifresult:
print ('恭喜你!{} 登录成功!'.format(result[1]))else:
print ('sorry!您的用户名或者密码错误!请检查!')
info()