def zhuche(u, p): db = DBTool() sql = "select * from t_class where cname='{}'".format(u) sql2 = "insert into t_class(cname,teacher)values({},{})".format(u, p) res = db.query(sql) if u == '' or p == '': return None #'注册失败,账号或密码不能为空' if res == False: #查询数据库失败 return False #注册失败 else: if len(res) == 0: db.commit(sql2) return '注册成功' else: return '注册失败,账号已存在'
def test_01_login(): # 构造请求 u = "http://192.144.148.91:2333/login" d = {"username": "******", "password": "******"} res = requests.post(url=u, json=d) # 断言 assert res.status_code == 200 assert res.json()["status"] == 200 # 查询数据库 db = DBTool(host="192.144.148.91", username="******", password="******", db="ljtestdb") sql = "select * from t_user where username='******'".format(d["username"]) r = db.query(sql) assert len(r) != 0 print("测试通过!")
def reg(username, password): if username == "" or password == "": print("用户名或密码格式不正确") return False # 查询用户是否已经注册注册了 db = DBTool() sql = "select * from tbl_user where username='******'".format(username) if len(db.query(sql)) != 0: print("用户名已存在,不能重复注册!") return False else: sql = "INSERT INTO tbl_user VALUES('{}', '{}', 'f', 20)".format( username, password) if db.commit(sql) == True: print("注册成功!") return True else: print("注册失败!") return False
def login(username, password): # 判断输入的用户和密码是不是有问题 if username == "" or password == "": return False # 去数据库查询用户表,用户是否是否存在 db = DBTool() sql = "select * from tbl_user where username='******'".format(username) res = db.query(sql) # 失败了:False,成功:结果了 print(res) # 2. 判断数据库查询是否成功 if res == False: # res=Flase:查询数据库失败了 return False # 登录失败,返回False else: # 否则就是查询成功 print(res) if len(res) == 0: # 没有找到该用户,登录失败 return False else:# 有这个用户,判断密码是否一致 if res[0][1] == password: return True # 密码一致,登录成功,返回True else: return False # 密码不一致,登录失败,返回False
import requests import traceback from dbtools import DBTool u = "http://192.144.148.91:2333/login" d = {"username": "******", "password": "******"} res = requests.post(url=u, json=d) # url=u:指定url使用u参数 try: # 断言 assert res.status_code == 200 assert res.json()["status"] == 200 # 对比数据库 db = DBTool(host="192.144.148.91", username="******", password="******", db="ljtestdb") sql = "select * from t_user where username='******'".format(d["username"]) r = db.query(sql) assert r != False # 为了判断数据库查询成功,如果不成功则数据查询失败,代码抛出异常 assert len(r) != 0 # 为了判断数据库表里面存在这个用户 print("测试用例执行成功!") except: traceback.print_exc() print("测试用例执行失败!")