class TestLogin(unittest.TestCase): datapath = os.path.join(DataDir, 'cases.xlsx') excel = read_excel.ReadExcel(datapath, 'login') login_data = excel.read_excel() rest = HanderRequest() @data(*login_data) def test_login(self, case): login_data = eval(case['data']) expected = eval(case['expected']) case_id = case['case_id'] url = case['url'] login_url = base_url + url method = case['method'] response = self.rest.send(url=login_url, method=method, json=login_data, headers=header) res = response.json() try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=case_id + 1, column=8, value='未通过') my_logger.info('用例-->{}:执行未通过'.format(case['title'])) my_logger.error(e) print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) raise e else: self.excel.write_excel(row=case_id + 1, column=8, value='已通过') my_logger.info('用例-->{}:执行已通过'.format(case['title']))
class TestInvest(unittest.TestCase): excel = ReadExcel(data_path, 'invest') invest_datas = excel.read_excel() http = HanderRequest() @data(*invest_datas) def test_invest(self, case): # 1、准备用例数据 url = conf.get('env', 'url') + case['url'] method = case['method'] case['data'] = replace_data(case['data']) data = eval(case['data']) headers = eval(conf.get('env', 'header')) if case['interface'] != 'login': headers['Authorization'] = getattr(TestData, 'token_data') expected = eval(case['expected']) row = case['case_id'] + 1 # 2、发送请求 response = self.http.send(url=url, method=method, json=data, headers=headers) res = response.json() if case['interface'] == 'login': # 提取member_id和token member_id = jsonpath.jsonpath(res, '$..id')[0] setattr(TestData, 'member_id', str(member_id)) token_type = jsonpath.jsonpath(res, '$..token_type')[0] token = jsonpath.jsonpath(res, '$..token')[0] token_data = token_type + ' ' + token setattr(TestData, 'token_data', token_data) elif case['interface'] == 'add': loan_id = jsonpath.jsonpath(res, '$..id')[0] setattr(TestData, 'loan_id', str(loan_id)) # 3、断言 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') my_logger.info('用例:{}--->执行未通过'.format(case['title'])) my_logger.error(e) print("预取结果:{}".format(expected)) print("实际结果:{}".format(res)) raise e else: self.excel.write_excel(row=row, column=8, value='已通过') my_logger.info('用例:{}--->执行已通过'.format(case['title']))
class TestRecharge(unittest.TestCase): datapath = os.path.join(DataDir, 'cases.xlsx') excel = ReadExcel(datapath, 'recharge') recharge_data = excel.read_excel() rest = HanderRequest() @classmethod def setUpClass(cls): # 创建数据库对象 cls.db = HandDB() # 登录,获取用户的id以及鉴权需要用到的token login_url = base_url + '/member/login' login_data = { "mobile_phone": conf.get('login_data', 'phone'), "pwd": conf.get('login_data', 'pwd') } expected = {'code': 0, 'msg': 'OK'} header = eval(conf.get('env', 'header')) response = requests.post(url=login_url, json=login_data, headers=header) res = response.json() # 获取用户id member_id = jsonpath.jsonpath(res, '$..id')[0] setattr(TestData, "member_id", str(member_id)) # 获取token类型 token_type = jsonpath.jsonpath(res, '$..token_type')[0] # 获取token值 token = jsonpath.jsonpath(res, '$..token')[0] token_data = token_type + ' ' + token setattr(TestData, 'token_data', token_data) @data(*recharge_data) def test_recharge(self, case): case['data'] = replace_data(case['data']) data = eval(case['data']) expected = eval(case['expected']) row = case['case_id'] + 1 url = case['url'] recharge_url = base_url + url method = case['method'] header = eval(conf.get('env', 'header')) header['Authorization'] = getattr(TestData, 'token_data') # 请求之前数据库看一下当前用户有多少钱 # 请求后数据库看一下当前用户有多少钱 # 然后比对增加的钱和用例中的钱 if case['check_sql']: sql = case['check_sql'].format(conf.get('login_data', 'phone')) before_amount = self.db.get_one(sql)[0] response = self.rest.send(url=recharge_url, method=method, json=data, headers=header) res = response.json() leave_amount = jsonpath.jsonpath(res, '$..leave_amount') try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: sql = case['check_sql'].format(conf.get('login_data', 'phone')) after_amount = self.db.get_one(sql)[0] expected_amount = eval(case['data'])['amount'] self.assertEqual(decimal.Decimal(str(expected_amount)), (after_amount - before_amount)) my_logger.info('充值之前金额为:{}\n,充值金额为:{}\n,充值后金额为:{}'.format( before_amount, expected_amount, after_amount)) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') my_logger.info('用例-->{}:执行未通过'.format(case['title'])) my_logger.error(e) print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) raise e else: self.excel.write_excel(row=row, column=8, value='已通过') my_logger.info('用例-->{}:执行已通过'.format(case['title'])) @classmethod def tearDownClass(cls): # 关闭数据的连接和游标对象 cls.db.close()
class TestAudit(unittest.TestCase): excel = ReadExcel(case_path, 'audit') case_data = excel.read_excel() http = HanderRequest() db=HandDB() @classmethod def setUpClass(cls): url = conf.get('env', 'url') + '/member/login' data = { "mobile_phone": conf.get('login_data', 'admin_phone'), "pwd": conf.get('login_data', 'admin_pwd') } headers = eval(conf.get('env', 'header')) response = cls.http.send(url=url, method='post', json=data, headers=headers) res = response.json() # 获取member_id admin_member_id = jsonpath.jsonpath(res, '$..id')[0] setattr(TestData, 'admin_member_id', str(admin_member_id)) # 获取token类型 token_type = jsonpath.jsonpath(res, '$..token_type')[0] # 获取token值 token = jsonpath.jsonpath(res, '$..token')[0] token_data = token_type + ' ' + token setattr(TestData, 'token_data', token_data) def setUp(self): url = conf.get('env', 'url') + '/loan/add' data = { "member_id": getattr(TestData, 'admin_member_id'), "title": "借钱实现财富自由", "amount": 2000, "loan_rate": 12.0, "loan_term": 3, "loan_date_type": 1, "bidding_days": 5 } headers = eval(conf.get('env', 'header')) headers['Authorization'] = getattr(TestData, 'token_data') response = self.http.send(url=url, method='post', json=data, headers=headers) res = response.json() # 获取项目id loan_id = jsonpath.jsonpath(res, '$..id')[0] setattr(TestData, 'loan_id', str(loan_id)) @data(*case_data) def test_audit(self, case): pass # 准备用例数据 url = conf.get('env', 'url') + case['url'] method = case['method'] case['data'] = replace_data(case['data']) data = eval(case['data']) expected = eval(case['expected']) headers = eval(conf.get('env', 'header')) headers['Authorization'] = getattr(TestData, 'token_data') row = case['case_id'] + 1 # 发送请求 response = self.http.send(url=url, method=method, json=data, headers=headers) res = response.json() # 断言 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: sql=replace_data(case['check_sql']) result_status=self.db.get_one(sql)[0] self.assertEqual(expected['status'],result_status) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') my_logger.info('用例--->{}:执行未通过'.format(case['title'])) my_logger.error(e) print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) raise e else: self.excel.write_excel(row=row, column=8, value='已通过') my_logger.info('用例--->{}:执行已通过'.format(case['title']))
class TestRegister(unittest.TestCase): datapath = os.path.join(DataDir, 'cases.xlsx') excel = read_excel.ReadExcel(datapath, 'register') register_data = excel.read_excel() rest = HanderRequest() db = HandDB() @data(*register_data) def test_register(self, case): # 判断是否有手机号码需要替换 if '#phone#' in case['data']: phone = self.random_phone() setattr(TestData,'phone',phone) case['data']=case['data'].replace('#phone#',phone) data = eval(case['data']) expected = eval(case['expected']) case_id = case['case_id'] url = case['url'] register_url = base_url + url method = case['method'] response = self.rest.send(url=register_url, method=method, json=data, headers=header) res = response.json() try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: # 去数据库查询当前注册的账号是否存在 sql=replace_data(case['check_sql']) my_logger.info('check_sql:{}'.format(sql)) # 获取数据库中没有没有该用户的信息 count = self.db.get_count(sql) # 数据库中返回的数据做断言,判断是否有一条数据 self.assertEqual(count, 1) except AssertionError as e: self.excel.write_excel(row=case_id + 1, column=8, value='未通过') my_logger.info('用例-->{}:执行未通过'.format(case['title'])) my_logger.error(e) print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) raise e else: self.excel.write_excel(row=case_id + 1, column=8, value='已通过') my_logger.info('用例-->{}:执行已通过'.format(case['title'])) @classmethod def random_phone(cls): while True: phone = '186' for i in range(8): phone += str(random.randint(0, 9)) # 去数据库查询号码是否注册过 sql='select * from futureloan.member where mobile_phone ={}'.format(phone) count=cls.db.get_count(sql) if count ==0: return phone @classmethod def tearDownClass(cls): # 关闭数据的连接和游标对象 cls.db.close()
class TestWithDraw(unittest.TestCase): datapath = os.path.join(DataDir, 'cases.xlsx') excel = ReadExcel(datapath, 'withdraw') withdraw_data = excel.read_excel() rest = HanderRequest() db = HandDB() @classmethod def setUpClass(cls): phone = conf.get('login_data', 'phone') setattr(TestData, 'phone', phone) pwd = conf.get('login_data', 'pwd') setattr(TestData, 'pwd', pwd) @data(*withdraw_data) def test_withdraw(self, case): # 准备用例数据 # 拼接url url = conf.get('env', 'url') + case['url'] method = case['method'] case['data'] = replace_data(case['data']) data = eval(case['data']) expected = eval(case['expected']) row = case['case_id'] + 1 header = eval(conf.get('env', 'header')) if case['interface'] != '登录': header['Authorization'] = TestData.token_data # 判断是否需要校验 if case['check_sql']: sql = case['check_sql'].format(getattr(TestData, 'phone')) # 获取充值前的余额 before_amount = self.db.get_one(sql)[0] response = self.rest.send(url=url, method=method, json=data, headers=header) res = response.json() if case['interface'] == '登录': # 获取member_id member_id = jsonpath.jsonpath(res, '$..id')[0] setattr(TestData, 'member_id', str(member_id)) # 获取token类型 token_type = jsonpath.jsonpath(res, '$..token_type')[0] # 获取token值 token = jsonpath.jsonpath(res, '$..token')[0] token_data = token_type + ' ' + token setattr(TestData, 'token_data', token_data) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: sql = case['check_sql'].format(getattr(TestData, 'phone')) after_amount = self.db.get_one(sql)[0] expected_amount = decimal.Decimal(str(data['amount'])) my_logger.info('提现前余额:{}\n,提现余额:{}\n,提现后余额:{}'.format( before_amount, expected_amount, after_amount)) self.assertEqual(expected_amount, (before_amount - after_amount)) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') my_logger.info('用例-->{}:执行未通过'.format(case['title'])) my_logger.error(e) print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) raise e else: self.excel.write_excel(row=row, column=8, value='已通过') my_logger.info('用例-->{}:执行已通过'.format(case['title'])) @classmethod def tearDownClass(cls): cls.db.close()
class TestAddLoan(unittest.TestCase): excel = ReadExcel(datapath, 'add_loan') addloan_data = excel.read_excel() http = HanderRequest() @classmethod def setUpClass(cls): cls.db = HandDB() # admin_phone=conf.get('login_data','admin_phone') # setattr(TestData,'admin_phone',admin_phone) # admin_pwd=conf.get('login_data','admin_pwd') # setattr(TestData, 'admin_pwd', admin_pwd) pass @data(*addloan_data) def test_addloan(self, case): # 准备用例数据 # 拼接完整的接口地址 url = conf.get('env', 'url') + case['url'] case['data']=replace_data(case['data']) data = eval(case['data']) expected = eval(case['expected']) row = case['case_id']+1 header = eval(conf.get('env', 'header')) if case['interface'] != '登录': header['Authorization'] = getattr(TestData,'token_data') method = case['method'] if case['check_sql']: sql = case['check_sql'].format(getattr(TestData, 'member_id')) before_count = self.db.get_count(sql) # 发送请求 response = self.http.send(url=url, method=method, json=data, headers=header) res = response.json() if case['interface']=='登录': # 获取用户id member_id=jsonpath.jsonpath(res,'$..id')[0] setattr(TestData,'member_id',str(member_id)) # 获取token类型 token_type=jsonpath.jsonpath(res,'$..token_type')[0] # 获取token值 token=jsonpath.jsonpath(res,'$..token')[0] token_data=token_type+' '+token setattr(TestData,'token_data',token_data) # 断言 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: sql = case['check_sql'].format(getattr(TestData,'member_id')) after_count = self.db.get_count(sql) self.assertEqual(1,after_count-before_count) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') my_logger.info("用例:{}--->执行未通过".format(case["title"])) my_logger.error(e) print("预取结果:{}".format(expected)) print("实际结果:{}".format(res)) raise e else: self.excel.write_excel(row=row, column=8, value='已通过') my_logger.info("用例:{}--->执行已通过".format(case["title"])) @classmethod def tearDownClass(cls): cls.db.close()
class TestAddLoan(unittest.TestCase): excel = ReadExcel(datapath, 'add_loan') addloan_data = excel.read_excel() http = HanderRequest() @classmethod def setUpClass(cls): cls.db = HandDB() cls.admin_phone=conf.get('login_data','admin_phone') cls.admin_pwd=conf.get('login_data','admin_pwd') @data(*addloan_data) def test_addloan(self, case): # 准备用例数据 # 拼接完整的接口地址 url = conf.get('env', 'url') + case['url'] if '#member_id#' in case['data']: case['data'] = case['data'].replace('#member_id#', str(self.member_id)) if '#admin_phone#' in case['data']: case['data'] = case['data'].replace('#admin_phone#', str(self.admin_phone)) if '#admin_pwd#' in case['data']: case['data'] = case['data'].replace('#admin_pwd#', str(self.admin_pwd)) data = eval(case['data']) expected = eval(case['expected']) row = case['case_id']+1 header = eval(conf.get('env', 'header')) if case['interface'] != '登录': header['Authorization'] = self.token_data method = case['method'] # 发送请求 response = self.http.send(url=url, method=method, json=data, headers=header) res = response.json() if case['interface']=='登录': # 获取用户id TestAddLoan.member_id=jsonpath.jsonpath(res,'$..id')[0] # 获取token类型 token_type=jsonpath.jsonpath(res,'$..token_type')[0] # 获取token值 token=jsonpath.jsonpath(res,'$..token')[0] TestAddLoan.token_data=token_type+' '+token # 断言 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if res['msg'] == 'ok': sql = 'select * from futureloan.loan WHERE member_id={}'.format(self.member_id) count = self.db.get_count(sql) self.assertEqual(count, 1) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') my_logger.info("用例:{}--->执行未通过".format(case["title"])) my_logger.error(e) print("预取结果:{}".format(expected)) print("实际结果:{}".format(res)) raise e else: self.excel.write_excel(row=row, column=8, value='已通过') my_logger.info("用例:{}--->执行已通过".format(case["title"])) @classmethod def tearDownClass(cls): cls.db.close()