class RegisterTest(unittest.TestCase): do_excel = DoExcel(contants.case_file) cases = do_excel.get_cases('register') @classmethod def setUpClass(cls): cls.request = Request() # 实例化对象 def setUp(self): self.mysql = MysqlUtil(return_dict=True) sql = "select max(mobilephone) as max_phone from future.member" self.max = self.mysql.fetch_one(sql)['max_phone'] @data(*cases) def test_register(self, case): logger.info("开始执行第{}条用例".format(case.id)) data_dict = json.loads(case.data) if data_dict['mobilephone'] == "${register_mobile}": data_dict['mobilephone'] = int(self.max) + 1 # 使用封装好的request 来完成请求 resp = self.request.request(case.method, case.url, data_dict) # 将返回结果和期望结果进行匹配 try: self.assertEqual(case.expected, resp.json()['code'], "login error") if resp.json()['msg'] == '注册成功': sql = 'select * from future.member where mobilephone = {}'.format( data_dict['mobilephone']) results = self.mysql.fetch_all(sql) self.assertEqual(1, len(results)) member = results[0] self.assertEqual(0, member['LeaveAmount']) # 一致就写入Excel的结果为pass,并且 self.do_excel.write_result('register', case.id + 1, resp.text, 'PASS') logger.info("第{}条用例执行结果:PASS".format(case.id)) except AssertionError as e: self.do_excel.write_result('register', case.id + 1, resp.text, 'FAIL') logger.error("第{}条用例执行结果:FAIL".format(case.id)) raise e def tearDown(self): pass @classmethod def tearDownClass(cls): cls.request.session.close()
class RegisterTest(unittest.TestCase): do_excel = DoExcel(contants.excel_path) register_cases = do_excel.read_data('register') @classmethod def setUpClass(cls): cls.request = Request() def setUp(self): self.mysql = MysqlUtil(return_dict=True) sql = 'select max(mobilephone) as max_phone from future.member' self.max = self.mysql.fetch_one(sql)['max_phone'] # logger.info(type(max), max) # @unittest.skip('不要执行') @data(*register_cases) def test_register(self, case): logger.info('开始执行第{0}条用例'.format(case.id)) data_dict = json.loads(case.data) #字典 if data_dict['mobilephone'] == '${phone}': data_dict['mobilephone'] = int(self.max) + 1 if data_dict['mobilephone'] == '${phone2}': data_dict['mobilephone'] = int(self.max) + 2 resp = self.request.request(case.method, case.url, data_dict) try: self.assertEqual(case.expected, resp.text) if resp.json()['msg'] == '注册成功': sql = 'select * from future.member where mobilephone ={0}'.\ format(data_dict['mobilephone']) results = self.mysql.fetch_all(sql) #列表里嵌套字典 self.assertEqual(1, len(results)) #首先判断是否有成功插入数据 member = results[0] #获取到这一条数据,是一个字典 self.assertEqual(0, member['LeaveAmount']) #判断注册成功余额是否为0 self.assertEqual(1, member['Type']) # 判断注册用户类型是否为1 self.assertNotEqual(data_dict['pwd'], member['Pwd']) # 判断密码是否加密 if 'regname' in data_dict.keys(): self.assertEqual(data_dict['regname'], member['RegName']) else: self.assertEqual('小蜜蜂', member['RegName']) #判断用户名是小蜜蜂 self.do_excel.write_data('register', case.id + 1, resp.text, 'PASS') logger.info('第{0}条用例执行结果:pass'.format(case.id)) except AssertionError as e: self.do_excel.write_data('register', case.id + 1, resp.text, 'FAIL') logger.error('第{0}条用例执行结果:fail'.format(case.id)) raise e def tearDown(self): self.mysql.close() @classmethod def tearDownClass(cls): cls.request.session.close()
class RegisterTest(unittest.TestCase): do_excel = DoExcel(contants.case_file) # 传入cases.xlsx cases = do_excel.get_cases('register') request = Request() # 实例化对象 def setUp(self): self.mysql = MysqlUtil(return_dict=True) # 创建数据连接 sql = "select max(mobilephone) as max_phone from future.member" # 查询最大手机号 self.max = self.mysql.fetch_one(sql)[ 'max_phone'] # 执行SQL,并且返回最近的一条数据,是元祖,使用下标取第一个值 @data(*cases) def test_register(self, case): print("开始执行第{0}用例".format(case.id)) # 替换手机号码 data = case.data # excel里面读取出来是字符串 # 字符串的查找并替换 # if data.find('${register_mobile}') > -1: # data.s.replace('${register_mobile}', self.max) # 字典,根据KEY取值,然后判断,是否需要替换 import json data = json.loads(data) if data['mobilephone'] == '${register_mobile}': data['mobilephone'] = int(self.max) + 1 # 使用封装好的request 来完成请求 resp = self.request.request(case.method, case.url, data) # 这里data传str或者dict都OK ,因为方法里面会做判断 # 将返回结果和期望结果进行匹配 try: self.assertEqual(case.expected, resp.text, "register error ") if resp.json()['msg'] == '注册成功': sql = 'select * from future.member where mobilephone = {0}' \ .format(data['mobilephone']) results = self.mysql.fetch_all(sql) # 首先判断是否有成功插入数据 self.assertEqual(1, len(results)) member = results[0] # 获取到这一条数据,是一个字典 self.assertEqual(0, member['LeaveAmount']) # 判断注册成功余额应该是0 # 一致就写入Excel的结果为PASS,并且 self.do_excel.write_result("register", case.id + 1, resp.text, 'PASS') print("第{0}用例执行结果:PASS".format(case.id)) except AssertionError as e: self.do_excel.write_result("register", case.id + 1, resp.text, 'FAIL') print("第{0}用例执行结果:FAIL".format(case.id)) raise e def tearDown(self): self.mysql.close()
class WithdrawTest(unittest.TestCase): do_excel = DoExcel(contants.excel_path) withdraw_cases = do_excel.read_data("withdraw") @classmethod def setUpClass(cls): cls.request = Request() def setUp(self): self.mysql = MysqlUtil(return_dict=True) # logger.info(type(max), max) @data(*withdraw_cases) def test_withdraw(self, case): logger.info('开始执行第{0}条用例'.format(case.id)) data_dict = json.loads(case.data) sql_1 = 'select LeaveAmount from future.member where mobilephone =18566668888' results_1 = self.mysql.fetch_one(sql_1) member_1 = results_1['LeaveAmount'] resp = self.request.request(case.method, case.url, data_dict) try: self.assertEqual(case.expected, resp.json()['code']) if resp.json()['msg'] == '取现成功': sql_2 = 'select * from future.member where mobilephone ={0}'. \ format(data_dict['mobilephone']) results_2 = self.mysql.fetch_all(sql_2) member_2 = results_2[0] self.assertEqual(member_1 - 500000, member_2['LeaveAmount'])#判断取现后余额是否正确 sql_3 = 'select paymembermoney from future.financelog where paymemberid = 1115526 ' \ 'ORDER BY createtime DESC LIMIT 1' results_3 = self.mysql.fetch_one(sql_3) member_3 = results_3['paymembermoney'] self.assertEqual(member_2['LeaveAmount'],member_3)#判断会员流水记录表余额是否与会员表余额一致 self.do_excel.write_data('withdraw',case.id + 1, resp.text, 'PASS') logger.info('第{0}条用例执行结果:pass'.format(case.id)) except AssertionError as e: self.do_excel.write_data('withdraw',case.id + 1, resp.text, 'FAIL') logger.error('第{0}条用例执行结果:fail'.format(case.id)) raise e def tearDown(self): self.mysql.close() @classmethod def tearDownClass(cls): cls.request.session.close()