class TestRegister(unittest.TestCase): def setUp(self): print("开始测试") self.mysql = MysqlUtil() self.sql = "select l.MobilePhone from future.member l where l.MobilePhone !='' " \ "order by l.MobilePhone desc limit 1" self.max_phone = self.mysql.fetch_one( self.sql)['MobilePhone'] # ['MobilePhone'] def tearDown(self): self.mysql.close() print("测试结束") @data(*cases) def test_register(self, case): # 必须传输一条case data = json.loads(case.data) if data['mobilephone'] == '${register}': data['mobilephone'] = str(int(self.max_phone) + 1) print(data) res = Request(method=case.method, url=case.url, data=data) print(res.get_json()) self.assertEqual(str(case.expected), res.get_json()['code']) # 数据库数据校验 sql = "select l.MobilePhone from future.member l where l.MobilePhone ={}".format( int(self.max_phone) + 1) # print(sql) expected = int(self.max_phone) + 1 member = self.mysql.fetch_one(sql) if member is not None: self.assertEqual(expected, member['MobilePhone'])
class InvestTest(unittest.TestCase): def setUp(self): self.mysql = MysqlUtil() # 投资前账户余额 self.select_member = 'select * from future.member where mobilephone = {0}'.format(Context.normal_user) self.before_amount = self.mysql.fetch_one(self.select_member)['LeaveAmount'] # 自己去添加 pass @data(*cases) def test_invest(self, case): # 参数化处理 data = DoRegex.replace(case.data) # 将测试数据由字符串序列化成字典 data = json.loads(data) if hasattr(Context, 'cookies'): # 判断是否有cookies cookies = getattr(Context, 'cookies') # 获取放到上下文里面的cookies else: cookies = None # 通过封装的Request类来完成接口的调用 resp = Request(method=case.method, url=case.url, data=data, cookies=cookies) print(resp.get_text()) resp_dict = resp.get_json() # 获取请求响应,字典 # 优先判断响应返回的code 是否与期望结果一致 self.assertEqual(case.expected, int(resp_dict['code'])) # 判断有没有cookie if resp.get_cookies(): # 判断返回里面是否有cookies setattr(Context, 'cookies', resp.get_cookies()) # 放入到上下文中 # 当创建标的成功时,根据借款人ID查看数据库loan表是否与添加数据 if resp_dict['msg'] == '加标成功': select_loan = 'select * from future.loan where memberId = {0} order by createtime desc limit 1'.format(Context.loan_member_id) loan = self.mysql.fetch_one(select_loan) if loan is not None: # 如果从数据库里面查询出来不是空,则创建标的成功 # 判断数据库里面的标的详情是否与测试数据一致 self.assertEqual(data['amount'], loan['Amount']) # 多个字段一致才assert通过 # 其他的自己加 # 将创建成功的标的ID写入到上下文中,用于之后投资用 setattr(Context, 'loan_id', str(loan['Id'])) # 放一个str类型的进去,以备后面正则替换 else: raise AssertionError # 如果数据库里面没有数据,就测试失败 # 当审核成功,需校验数据库loan表中status字段更改,自己添加 # 当投资成功时,根据投资人ID查看数据member表中验证余额是否减少 if resp_dict['msg'] == '竞标成功': amount = data['amount'] # 投资金额 actual = self.mysql.fetch_one(self.select_member)['LeaveAmount'] # 投资后的金额 expect = float(self.before_amount) - float(amount) # 期望 = 投资前的余额 - 投资金额 self.assertEqual(expect, actual) elif resp_dict['code'] != '10001': # 投资失败,余额不变 actual = self.mysql.fetch_one(self.select_member)['LeaveAmount'] # 投资后的金额 expect = float(self.before_amount) # 期望 = 投资前的余额 self.assertEqual(expect, actual) def tearDown(self): self.mysql.close() pass
def setUpClass(cls): global mysql mysql = MysqlUtil() # 查询标的statue为1(非竞标) 和 5(满标)最大标的 global cannot_load_id, full_loan_id, max_loan_id select_1 = 'SELECT * from future.loan where `Status` = "1" ORDER BY Amount DESC LIMIT 1' # select_5 = 'SELECT * from future.loan where `Status` = "5" ORDER BY Amount DESC LIMIT 1' select_max = 'SELECT * from future.loan ORDER BY Id DESC LIMIT 1' cannot_load_id = mysql.fetch_one(select_1)['Id'] # 获取到非竞标中的标的 # full_loan_id = mysql.fetch_one(select_5)['Id'] # 获取到竞标中的标的 max_loan_id = mysql.fetch_one(select_max)['Id'] # 最大标的
def setUpClass(cls): global mysql mysql = MysqlUtil() sql_max = 'select mobilephone from future.member where ' \ ' mobilephone != "" order by mobilephone desc limit 1 ' global max_phone max_phone = mysql.fetch_one(sql_max) # 从数据库中查找出最大的手机号码用来测试非注册用户登录用例
def setUpClass(cls): global mysql mysql = MysqlUtil() sql = 'select mobilephone from future.member where ' \ ' mobilephone != "" order by mobilephone desc limit 1 ' global max_phone max_phone = mysql.fetch_one(sql)['mobilephone'] # 查询标的statue为1(非竞标) 和 5(满标)最大标的 global cannot_load_id, full_loan_id, max_loan_id select_1 = 'SELECT * from future.loan where `Status` = "1" ORDER BY Amount DESC LIMIT 1' select_5 = 'SELECT * from future.loan where `Status` = "5" ORDER BY Amount DESC LIMIT 1' select_max = 'SELECT * from future.loan ORDER BY Id DESC LIMIT 1' cannot_load_id = mysql.fetch_one(select_1)['Id'] # 获取到非竞标中的标的 full_loan_id = mysql.fetch_one(select_5)['Id'] # 获取到竞标中的标的 max_loan_id = mysql.fetch_one(select_max)['Id'] # 最大标的
def setUpClass(cls): print("准备最大手机号码") global mysql mysql = MysqlUtil() sql = 'select mobilephone from future.member where ' \ ' mobilephone != "" order by mobilephone desc limit 1 ' global max_phone max_phone = mysql.fetch_one(sql)['mobilephone']
class TestWithdraw(unittest.TestCase): def setUp(self): print("开始测试") self.mysql = MysqlUtil() self.sql = "select l.LeaveAmount from future.member l where l.MobilePhone ={}".format( Context.normal_user) self.start_amount = self.mysql.fetch_one(self.sql)['LeaveAmount'] @data(*cases) def test_withdraw(self, case): my_logger = MyLog() my_logger.debug("执行第{}条案例".format(case.case_id)) data = DoRegex.replace(case.data) data = json.loads(data) if hasattr(Context, 'cookies'): cookies = getattr(Context, 'cookies') else: cookies = None res = Request(method=case.method, url=case.url, data=data, cookies=cookies) if res.get_cookies(): setattr(Context, 'cookies', res.get_cookies()) my_logger.debug(res.get_json()) try: self.assertEqual(case.expected, int(res.get_json()['code'])) except AssertionError as e: my_logger.error("断言失败!") raise e if res.get_json()['msg'] == '取现成功': expected = float(self.start_amount) - float(data['amount']) actual = self.mysql.fetch_one(self.sql)['LeaveAmount'] try: self.assertEqual(expected, actual) except AssertionError as e: raise e else: expected = float(self.start_amount) actual = self.mysql.fetch_one(self.sql)['LeaveAmount'] self.assertEqual(expected, actual) def tearDown(self): self.mysql.close() print("测试结束")
class TestBidLoan(unittest.TestCase): def setUp(self): print("开始测试") self.mysql = MysqlUtil() self.sql = "select * from future.member where MobilePhone ={}".format(Context.normal_user) self.start_amount = self.mysql.fetch_one(self.sql)['LeaveAmount'] @data(*cases) def test_bidLoan(self, case): data = DoRegex.replace(case.data) data = json.loads(data) print("执行第{}条案例".format(case.case_id)) if hasattr(Context, 'cookies'): cookies = getattr(Context, 'cookies') else: cookies = None res = Request(method=case.method, url=case.url, data=data, cookies=cookies) print(res.get_json()) if res.get_cookies(): setattr(Context, 'cookies', res.get_cookies()) if res.get_json()['msg'] == "竞标成功": expect = float(self.start_amount) - float(data['amount']) actual = self.mysql.fetch_one(self.sql)['LeaveAmount'] try: self.assertEqual(expect, actual) # 一直报错,实际值不正确,数据库数据没有变化 ??? print(expect) print(actual) except Exception as e: print("投标成功断言失败!") raise e else: expect = float(self.start_amount) actual = self.mysql.fetch_one(self.sql)['LeaveAmount'] try: self.assertEqual(expect, actual) print(expect) print(actual) except Exception as e: print("投标失败断言失败!") raise e def tearDown(self): self.mysql.close() print("结束测试")
class TestRecharge(unittest.TestCase): def setUp(self): # 充值前账户余额记录 self.mysql = MysqlUtil() # 查询投资用户的账户信息 self.sql = 'select * from future.member where mobilephone = {0}'.format(Context.normal_user) self.before_amount = self.mysql.fetch_one(self.sql)['LeaveAmount'] # 账户余额 print("充值前的金额", self.before_amount) @data(*cases) def test_recharge(self, case): # 参数化处理 data = DoRegex.replace(case.data) # 将字符串序列化成字典 data = json.loads(data) if hasattr(Context, 'cookies'): # 判断是否有cookies cookies = getattr(Context, 'cookies') # 获取放到上下文里面的cookies else: cookies = None resp = Request(method=case.method, url=case.url, data=data, cookies=cookies) # 通过封装的Request类来完成接口的调用 # 判断有没有cookie if resp.get_cookies(): # 判断返回里面是否有cookies setattr(Context, 'cookies', resp.get_cookies()) # 放入到上下文中 resp_dict = resp.get_json() # 获取请求响应,字典 self.assertEqual(case.expected, int(resp_dict['code'])) # 判断响应返回的code 是否与期望结果一致 actual = self.mysql.fetch_one(self.sql)['LeaveAmount'] # 再次获取账户余额 print("测试{0}后的金额".format(case.title), actual) # 充值成功,判断余额增加 if resp_dict['code'] == '10001' and resp_dict['msg'] == "充值成功": amount = float(data['amount']) # 充值金额 print('充值金额', amount) expect = float(self.before_amount) + amount # 充值成功,期望的账户余额= 原来余额 + 充值金额 self.assertEqual(expect, actual) # 判断期望结果与实际结果是否一致 elif resp_dict['code'] != '10001': expect = self.before_amount # 充值失败,期望结果:余额未增加 self.assertEqual(expect, actual) # 判断期望结果与实际结果是否一致 def tearDown(self): self.mysql.close()
class TestAdd(unittest.TestCase): def setUp(self): print("开始测试") self.mysql = MysqlUtil() def tearDown(self): self.mysql.close() print("测试结束") print("--------------------------------------") @data(*cases) def test_add(self, case): # 必须传输一条case my_logger = MyLog() data = DoRegex.replace(case.data) # 首先从excel中取出来,然后做正则 data = json.loads(data) # 然后在loads if hasattr(Context, 'cookies'): # 第一次登陆没有cookies,所以要判断 cookies = getattr(Context, 'cookies') else: cookies = None res = Request(method=case.method, url=case.url, data=data, cookies=cookies) # 判断有没有cookies if res.get_cookies(): setattr(Context, 'cookies', res.get_cookies()) my_logger.debug(res.get_json()) try: self.assertEqual(str(case.expected), res.get_json()['code']) except AssertionError as e: my_logger.error("断言失败") raise e resp_dict = res.get_json() if resp_dict['msg'] == '加标成功': select_loan = 'select * from future.loan where memberId = {0} order by createtime desc limit 1'.format( Context.loan_member_id_1) loan = self.mysql.fetch_one(select_loan) if loan is not None: # 如果从数据库里面查询出来不是空,则创建标的成功 # 判断数据库里面的标的详情是否与测试数据一致 self.assertEqual(data['amount'], loan['Amount']) # 多个字段一致才assert通过 # 其他的自己加 # 将创建成功的标的ID写入到上下文中,用于之后投资用 setattr(Context, 'loan_id', str(loan['Id'])) # 放一个str类型的进去,以备后面正则替换 else: raise AssertionError # 如果数据库里面没有数据,就测试失败
class TestRecharge(unittest.TestCase): def setUp(self): print("开始测试") self.sql = "select l.LeaveAmount from future.member l where l.MobilePhone ={}".format( Context.normal_user) self.mysql = MysqlUtil() self.leaveamount = self.mysql.fetch_one(self.sql)['LeaveAmount'] def tearDown(self): self.mysql.close() print("测试结束") print("--------------------------------------") @data(*cases) def test_recharge(self, case): # 必须传输一条case my_logger = MyLog() data = DoRegex.replace(case.data) # 首先从excel中取出来,然后做正则 data = json.loads(data) # 然后在loads if hasattr(Context, 'cookies'): # 第一次登陆没有cookies,所以要判断 # 参数化处理 cookies = getattr(Context, 'cookies') else: cookies = None res = Request(method=case.method, url=case.url, data=data, cookies=cookies) # 判断有没有cookies if res.get_cookies(): setattr(Context, 'cookies', res.get_cookies()) my_logger.debug(res.get_json()) try: self.assertEqual(str(case.expected), res.get_json()['code']) except AssertionError as e: my_logger.error("{} not found".format(e)) raise e if res.get_json()['msg'] == '充值成功': try: self.assertEqual(str(self.leaveamount + 10), res.get_json()['data']['leaveamount']) except AssertionError as a: my_logger.error("余额不正确,报错{}".format(a))
class TestLogin(unittest.TestCase): # @classmethod # def setUpClass(cls): # 必须使用@classmethod 装饰器,所有test运行前运行一次 # global mysql # mysql = MysqlUtil() # sql = 'select mobilephone from future.member where ' \ # ' mobilephone != "" order by mobilephone desc limit 1 ' # # global max_phone # max_phone = mysql.fetch_one(sql)['mobilephone'] def setUp(self): #每个测试函数运行前运行 self.mysql = MysqlUtil() self.sql = 'select mobilephone from future.member where ' \ ' mobilephone != "" order by mobilephone desc limit 1 ' self.max_phone = self.mysql.fetch_one(self.sql)['mobilephone'] @data(*cases) def test_login(self,case): register = int(self.max_phone)+1 data = re.sub(pattern='\$\{(.*?)\}', repl=str(register),string=case.data) # 使用正则匹配替换excel中的数据 print('data:',data) data = json.loads(data) #将str反序列化为字典 resp = Request(method=case.method,url=case.url,data=data) print("响应结果:",resp.get_status_code()) resp_dict = resp.get_json() resp_text = json.dumps(resp_dict, ensure_ascii=False, indent=4) print('response: ', resp_text) # 打印响应 #优先判断期望结果与实现结果 expected = json.loads(case.expected) self.assertEqual(expected['code'],resp_dict['code']) sql = 'select * from future.member where mobilephone = "{0}"'.format(str(register)) if resp_dict['msg'] == "注册成功": # 注册成功的数据校验,判断数据库有这条数据 expected = int(self.max_phone) + 1 member = self.mysql.fetch_one(sql) if member is not None: # 正常注册成功就不应该返回None self.assertEqual(expected,int(member['MobilePhone'])) TestResult = 'PASS' do_excel.write_result_by_case_id('register',case.case_id,resp.get_text(),TestResult) else: # 返回None则代表注册成功之后但是数据库里面没有插入数据 TestResult = 'Fail' do_excel.write_result_by_case_id('register',case.case_id,resp.get_text(),TestResult) raise AssertionError else: #注册失败的数据校验,判断数据库没有这条数据,自己写 if resp_dict['msg'] == "手机号码已被注册": expected = data['mobilephone'] mobilephone = self.mysql.fetch_one('select * from future.member where mobilephone = {0}'.format(expected)) if mobilephone is not None: # 若数据库里有这条数据,则说明该手机号已被注册,用例pass self.assertEqual(expected,mobilephone['MobilePhone']) TestResult = 'PASS' do_excel.write_result_by_case_id('register',case.case_id,resp_text,TestResult) else: # 若数据库里没有这条数据,则说明该手机号未被重复注册,用例fail TestResult = 'Fail' do_excel.write_result_by_case_id('register',case.case_id,resp_text,TestResult) else: # 其他所有注册失败用例 expected = register member = self.mysql.fetch_one(sql) if member is None: # 查询数据库里没有这条数据,说明该手机号未被注册,用例pass TestResult = 'PASS' do_excel.write_result_by_case_id('register',case.case_id,resp.get_text(),TestResult) else: TestResult = 'Fail' do_excel.write_result_by_case_id('register',case.case_id,resp.get_text(),TestResult) # def tearDown(self): # print('数据清除') # @classmethod # def tearDownClass(cls): # mysql.close() def tearDown(self): self.mysql.close()
# -*- coding:utf-8 _*- """ @author:Elena @time: 2019/1/4 10:03 @email:[email protected] @function: """ import hashlib from common.mysql_util import MysqlUtil mysql = MysqlUtil() sql = 'SELECT * from future.member where MobilePhone = "18602153084"' PWD = mysql.fetch_one(sql)['Pwd'] print('PWD', PWD) pwd = '123456' m = hashlib.md5() m.update(pwd.encode('utf-8')) pwd_md5 = m.hexdigest() pwd_md5 = pwd_md5.swapcase() # 大小写互换 print('pwd', pwd_md5) if pwd_md5 == PWD: print('True') else: print('false')
class TestInvest(unittest.TestCase): #准备测试数据-- @classmethod def setUpClass(cls): global mysql mysql = MysqlUtil() sql = 'select mobilephone from future.member where ' \ ' mobilephone != "" order by mobilephone desc limit 1 ' global max_phone max_phone = mysql.fetch_one(sql)['mobilephone'] # 查询标的statue为1(非竞标) 和 5(满标)最大标的 global cannot_load_id, full_loan_id, max_loan_id select_1 = 'SELECT * from future.loan where `Status` = "1" ORDER BY Amount DESC LIMIT 1' select_5 = 'SELECT * from future.loan where `Status` = "5" ORDER BY Amount DESC LIMIT 1' select_max = 'SELECT * from future.loan ORDER BY Id DESC LIMIT 1' cannot_load_id = mysql.fetch_one(select_1)['Id'] # 获取到非竞标中的标的 full_loan_id = mysql.fetch_one(select_5)['Id'] # 获取到竞标中的标的 max_loan_id = mysql.fetch_one(select_max)['Id'] # 最大标的 def setUp(self): self.mysql = MysqlUtil() # 投资前账户余额 self.select_member = 'select * from future.member where mobilephone = {0}'.format( Context.normal_user) self.before_amount = self.mysql.fetch_one( self.select_member)['LeaveAmount'] # 自己添加 @data(*cases) def test_invest(self, case): setattr(Context, 'cannot_loan_id', str(cannot_load_id)) setattr(Context, 'full_loan_id', str(full_loan_id)) setattr(Context, 'not_exist_loan_id', str(int(max_loan_id) + 2)) # 跑用例之前最大id,运行完之后id+1,保证不存在需再加1 data = DoRegex.replace(case.data) # 正则匹配,用配置文件的基础数据替换正则表达式代替的内容 data = json.loads(data) # str反序列化为dict # 判断是否有cookies,解决数据依赖 if hasattr(Context, 'cookies'): cookies = Context.cookies else: cookies = None # 发起request请求 resp = Request(method=case.method, url=case.url, data=data, cookies=cookies) print("用例名称:", case.title) #请求成功后,返回response resp_dict = resp.get_json() print(resp.get_text()) # 优化对excel中expected和实际返回结果作断言 expectVal = case.expected if (expectVal is None) or (str(expectVal).strip().strip('\n') is ''): # 对空单元格异常处理 expectVal = '' self.assertEqual(str(expectVal), resp_dict['code']) # 判断response中是否有cookies,反射方法写到Context类中 if resp.get_cookies(): setattr(Context, 'cookies', resp.get_cookies()) if resp_dict['msg'] == '登录成功': try: self.assertEqual(case.expected, eval(resp_dict['code'])) TestResult = 'PASS' do_excel.write_result_by_case_id('invest', case.case_id, resp.get_text(), TestResult) except AssertionError as e: TestResult = 'Fail' do_excel.write_result_by_case_id('invest', case.case_id, resp.get_text(), TestResult) raise e # 当创建标的成功时,根据借款人ID查看数据库loan表是否与添加数据 if resp_dict['msg'] == '加标成功': select_loan = 'select * from future.loan where memberId = {0} ' \ 'order by createtime desc limit 1'.format(Context.loan_member_id) loan = self.mysql.fetch_one(select_loan) #查询到投标金额 if loan is not None: self.assertEqual(data['amount'], loan['Amount']) # 实际加标金额与数据库加标金币是否一致 self.assertEqual(data['loanRate'], loan['LoanRate']) self.assertEqual(data['loanTerm'], loan['LoanTerm']) self.assertEqual(data['loanDateType'], loan['LoanDateType']) self.assertEqual(data['repaymemtWay'], loan['RepaymemtWay']) self.assertEqual(data['biddingDays'], loan['BiddingDays']) # 将创建成功的标的ID写入到上下文中,用于之后投资用 setattr(Context, 'loan_id', str(loan['Id'])) # 放一个str类型的进去,以备后面正则替换 TestResult = 'PASS' do_excel.write_result_by_case_id('invest', case.case_id, resp.get_text(), TestResult) else: TestResult = 'Fail' do_excel.write_result_by_case_id('invest', case.case_id, resp.get_text(), TestResult) raise AssertionError # 当审核成功,需校验数据库loan表中status字段更改,自己添加 if resp_dict['msg'] == '更新状态成功:竞标开始,当前标为竞标中状态': select_loan = 'select * from future.loan where memberId = {0} order by createtime desc limit 1'.format( Context.loan_member_id) loan = self.mysql.fetch_one(select_loan) #查询到投标金额 if loan is not None: self.assertEqual(data['status'], loan['Status']) TestResult = 'PASS' do_excel.write_result_by_case_id('invest', case.case_id, resp.get_text(), TestResult) else: TestResult = 'Fail' do_excel.write_result_by_case_id('invest', case.case_id, resp.get_text(), TestResult) raise AssertionError # 当投资成功时,根据投资人ID查看数据member表中验证余额是否减少 if resp_dict['msg'] == '竞标成功': amount = data['amount'] actual = self.mysql.fetch_one(self.select_member)['LeaveAmount'] expect = float(self.before_amount) - float(amount) self.assertEqual(expect, actual) print("竞标成功后余额为{0}:".format(actual)) TestResult = 'PASS' do_excel.write_result_by_case_id('invest', case.case_id, resp.get_text(), TestResult) #投资失败,余额不变 elif resp_dict['status'] == 0: # 投资失败 try: actual = self.mysql.fetch_one( self.select_member)['LeaveAmount'] expect = float(self.before_amount) # 余额应等于投资前的金额 self.assertEqual(expect, actual) print("竞标失败余额为{0}:".format(actual)) TestResult = 'PASS' do_excel.write_result_by_case_id('invest', case.case_id, resp.get_text(), TestResult) except AssertionError as e: TestResult = 'Fail' do_excel.write_result_by_case_id('invest', case.case_id, resp.get_text(), TestResult) raise e def tearDown(self): self.mysql.close()