def login(self, user): headers = { "X-Lemonban-Media-Type": "lemonban.v2", "Content-Type": "application/json" } res = Response(self.yaml_data["url"] + "/member/login", 'post').get_response(headers=headers, json=user) member_id = jsonpath(res, "$..id")[0] token_type = jsonpath(res, "$..token_type")[0] token = token_type + ' ' + jsonpath(res, "$..token")[0] return member_id, token
def test_recharge(self, data_item): logger.info('正在执行{}用例第{}条>>>'.format(data_item["interface"], data_item["case_id"])) data_item["headers"] = even_data.data_replace(data_item["headers"]) data_item["data"] = even_data.data_replace(data_item["data"]) db = Handler.db_class() sql = "select leave_amount from futureloan.member where id={};".format( even_data.member_id) # 访问接口前获取充值前的余额 before_amount = db.query(sql)["leave_amount"] db.close() # print(type(before_amount),before_amount) # print(type(amount),amount) exp_res = json.loads(data_item['expected_result']) act_res = Response(url_header + data_item["url"], data_item["method"]).get_response( headers=json.loads(data_item["headers"]), json=json.loads(data_item["data"])) # print(act_res) try: for k, v in exp_res.items(): self.assertTrue(act_res[k] == v) if act_res['code'] == 0: logger.info('充值成功!') amount = json.loads(data_item['data'])["amount"] # 校验响应中的数据和期望数据一致 self.assertTrue( Decimal(str(jsonpath(act_res, "$..leave_amount")[0])) == Decimal(str(amount)) + before_amount) logger.info('响应数据中余额与期望金额一致。') db = Handler.db_class() leave_amount = db.query(sql)["leave_amount"] db.close() # print(type(leave_amount),leave_amount) # 校验数据库的数据与期望一致 self.assertTrue(leave_amount == Decimal(str(amount)) + before_amount) logger.info('数据库会员{}余额与期望金额一致'.format(even_data.member_id)) logger.info('用例{}通过。'.format(data_item["case_id"])) # 测试结果回写到excel Handler.excel.data_write('recharge', data_item["case_id"] + 1, 9, "pass") except AssertionError as e: logger.error('用例{}不通过!!'.format(data_item["case_id"])) Handler.excel.data_write('recharge', data_item["case_id"] + 1, 9, "fail") raise e
def test_add(self, data_item): logger.info('正在执行{}用例第{}条>>>'.format(data_item["interface"], data_item["case_id"])) data_item['headers'] = even_data.data_replace(data_item['headers']) data_item['data'] = even_data.data_replace(data_item['data']) sql = "SELECT * FROM futureloan.loan WHERE member_id={};".format( even_data.member_id) # 访问接口前获取loan的记录条数 db = Handler.db_class() before_loan_num = len(db.query(sql, one=False)) db.close() # print(type(before_amount),before_amount) # print(type(amount),amount) exp_res = json.loads(data_item['expected_result']) act_res = Response(url_header + data_item["url"], data_item["method"]).get_response( headers=json.loads(data_item["headers"]), json=json.loads(data_item["data"])) # print(act_res) try: for k, v in exp_res.items(): self.assertTrue(act_res[k] == v) if act_res['code'] == 0: logger.info('项目添加成功!') # 访问接口后获取loan的记录条数 db = Handler.db_class() after_loan_num = len(db.query(sql, one=False)) db.close() # print(type(leave_amount),leave_amount) # 校验数据库的数据与期望一致 self.assertTrue(after_loan_num == before_loan_num + 1) logger.info('数据库新增一条记录') logger.info('用例{}通过。'.format(data_item["case_id"])) # 测试结果回写到excel Handler.excel.data_write('add', data_item["case_id"] + 1, 9, "pass") except AssertionError as e: logger.error('用例{}不通过!!'.format(data_item["case_id"])) # print(act_res, exp_res) Handler.excel.data_write('add', data_item["case_id"] + 1, 9, "fail") raise e
def test_register(self, data_item): logger.info('正在执行{}用例第{}条>>>'.format(data_item["interface"], data_item["case_id"])) if "#mobile_phone#" in data_item["data"]: phone = self.get_random_phone() data_item["data"] = data_item["data"].replace( "#mobile_phone#", phone) # print(data_item["data"]) exp_res = json.loads(data_item['expected_result']) act_res = Response(url_header + data_item["url"], data_item["method"]).get_response( headers=json.loads(data_item["headers"]), json=json.loads(data_item["data"])) # print(act_res, exp_res) # self.assertTrue(act_res["code"] == exp_res["code"]) # self.assertTrue(act_res["msg"] == exp_res["msg"]) try: for k, v in exp_res.items(): self.assertTrue(act_res[k] == v) if act_res['code'] == 0: logger.info('手机号{}注册成功!'.format( json.loads(data_item["data"])["mobile_phone"])) # logger.info('初始化数据库>>>') # db = Handler.db_class() # logger.info('数据库初始化完毕!') logger.info('正在查询{}的数据库信息>>>'.format( json.loads(data_item["data"])["mobile_phone"])) phone = json.loads(data_item["data"])["mobile_phone"] sql = "select * from futureloan.member where mobile_phone={};".format( phone) mysql_data = self.db.query(sql) # db.close() # 注册成功的手机号需能在数据库中查询到 self.assertTrue(mysql_data) logger.info('此用户已成功存入数据库!') logger.info('用例{}通过。'.format(data_item["case_id"])) # 测试结果回写到excel Handler.excel.data_write('register', data_item["case_id"] + 1, 9, "pass") except AssertionError as e: logger.error('用例{}不通过!!'.format(data_item["case_id"])) Handler.excel.data_write('register', data_item["case_id"] + 1, 9, "fail") raise e
def get_loan_id(self): headers = { "X-Lemonban-Media-Type": "lemonban.v2", "Content-Type": "application/json", "Authorization": self.token } data = { "member_id": self.member_id, "title": "xx借钱买手机", "amount": 1000.00, "loan_rate": 10.11, "loan_term": 2, "loan_date_type": 1, "bidding_days": 1 } res = Response(self.yaml_data["url"] + "/loan/add", 'post').get_response(headers=headers, json=data) loan_id = jsonpath(res, "$..id")[0] return loan_id
def test_register(self, data_item): logger.info('正在执行{}用例第{}条>>>'.format(data_item["interface"], data_item["case_id"])) exp_res = json.loads(data_item['expected_result']) act_res = Response(url_header + data_item["url"], data_item["method"]).get_response( headers=json.loads(data_item["headers"]), json=json.loads(data_item["data"])) try: for k, v in exp_res.items(): self.assertTrue(act_res[k] == v) logger.info('用例{}通过。'.format(data_item["case_id"])) # 将测试结果回写到excel Handler.excel.data_write('login', data_item["case_id"] + 1, 9, "pass") except AssertionError as e: logger.error('用例{}不通过!!'.format(data_item["case_id"])) Handler.excel.data_write('login', data_item["case_id"] + 1, 9, "fail") raise e
def test_audit(self, data_item): logger.info('正在执行{}用例第{}条>>>'.format(data_item["interface"], data_item["case_id"])) data_item['headers'] = env_data.data_replace(data_item['headers']) if "#pass_loan_id#" in data_item['data']: loan_id = self.db.query( "SELECT id FROM futureloan.loan WHERE status != 1")["id"] data_item['data'] = data_item['data'].replace( '#pass_loan_id#', str(loan_id)) else: data_item['data'] = env_data.data_replace(data_item['data']) # db = Handler.db_class() # sql = "SELECT status FROM futureloan.loan WHERE id={};".format(json.loads(data_item['data'])["loan_id"]) # 访问接口前查询新建项目的status # if db.query(sql): # before_status = db.query(sql)["status"] # print(before_status) # db.close() # print(type(before_amount),before_amount) # print(type(amount),amount) exp_res = json.loads(data_item['expected_result']) act_res = Response(url_header + data_item["url"], data_item["method"]).get_response( headers=json.loads(data_item["headers"]), json=json.loads(data_item["data"])) # print(act_res) try: for k, v in exp_res.items(): self.assertTrue(act_res[k] == v) if act_res['code'] == 0: logger.info('项目审核完毕!') # 访问接口后获取loan的status # db = Handler.db_class() sql = "SELECT status FROM futureloan.loan WHERE id={};".format( json.loads(data_item['data'])["loan_id"]) # 查询审核是否通过 after_loan_id = self.db.query(sql)["status"] # db.close() # print(type(leave_amount),leave_amount) # 校验数据库的status与期望一致 # self.assertTrue(data_item["status"] = after_loan_id) if json.loads(data_item["data"])["approved_or_not"]: self.assertTrue(after_loan_id == 2) logger.info('审核通过') else: self.assertTrue(after_loan_id == 5) logger.info('审核不通过') logger.info('用例{}通过。'.format(data_item["case_id"])) # 测试结果回写到excel Handler.excel.data_write('audit', data_item["case_id"] + 1, 9, "pass") except AssertionError as e: logger.error('用例{}不通过!!'.format(data_item["case_id"])) print(act_res, exp_res) Handler.excel.data_write('audit', data_item["case_id"] + 1, 9, "fail") raise e