class TestLogin(unittest.TestCase): file_name = handle_excel.Test_case( os.path.join(handle_path.DATA_PATH, "test_case.xlsx"), "login") test_case = file_name.test_cases() @myddt.data(*test_case) def test_login(self, item): case_id = item["case_id"] + 1 method = item["method"] url = handle_conf.Conf.get("env", "url") + item["url"] item["data"] = handle_data.replace_data(item["data"], TestLogin) params = eval(item["data"]) expected = eval(item["expected"]) headers = eval(handle_conf.Conf.get("env", "headers")) response = requests.request(method=method, url=url, json=params, headers=headers) res = response.json() print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) except AssertionError as e: handle_logs.log.error("用例{}未通过".format(item["title"])) handle_logs.log.exception(e) self.file_name.write_case(row=case_id, coulmn=8, value="失败") raise e else: handle_logs.log.info("用例{}通过".format(item["title"])) self.file_name.write_case(row=case_id, coulmn=8, value="成功")
class TestLogin(unittest.TestCase): case_file = handle_excel.Test_case( os.path.join(handle_path.DATA_PATH, "GL_test_case.xlsx"), "order_huodong") test_case = case_file.test_cases() @classmethod def setUpClass(cls): fixture.submit_order_fixture(cls) @myddt.data(*test_case) # 集采活动商品,现金支付 def test_order_pay_type8(self, items): url = handle_conf.Conf.get("env", "url") + items["url"] headers = self.headers method = items["method"] case_id = items["case_id"] + 1 if "#orderId#" in items["data"]: items["data"] = items["data"].replace("#orderId#", str(tools.find_order_id())) if "merchantNo" in items["data"]: items["data"] = items["data"].replace( "#merchantNo#", str( tools.get_merchant_no( handle_conf.Conf.get( "goods", "goods_info_item_no_bwd_baopin")))) params = eval(items["data"]) expected = eval(items["expected"]) res = requests.request(url=url, json=params, headers=headers, method=method).json() print(res) try: self.assertEqual(expected["status"], res["status"]) self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["message"], res["message"]) except AssertionError as e: handle_logs.log.error("用例执行失败,记录信息为-----{}-----".format( items['title'])) handle_logs.log.error(e) self.case_file.write_case(row=case_id, coulmn=8, value="失败") raise e else: handle_logs.log.info("用例执行成功,记录信息为-----{}-----".format( items['title'])) self.case_file.write_case(row=case_id, coulmn=8, value="成功")
class Add(unittest.TestCase): file_name = handle_excel.Test_case(os.path.join(handle_path.DATA_PATH, "test_case.xlsx"), "add") test_case = file_name.test_cases() @classmethod def setUpClass(cls): method = "post" url = handle_conf.Conf.get("env", "url") + "/member/login" params = {"mobile_phone": handle_conf.Conf.get("test_data", "mobile"), "pwd": handle_conf.Conf.get("test_data", "pwd")} headers = eval(handle_conf.Conf.get("env", "headers")) response = requests.request(method=method, url=url, json=params, headers=headers) res = response.json() tk = jsonpath.jsonpath(res, "$..token")[0] cls.user_id = jsonpath.jsonpath(res, "$..id")[0] cls.token = "Bearer" + " " + tk @myddt.data(*test_case) def test_add(self, item): url = handle_conf.Conf.get("env", "url") + item["url"] headers = eval(handle_conf.Conf.get("env" , "headers")) headers["Authorization"] = self.token case_id = item["case_id"] + 1 item["data"] = handle_data.replace_data(item["data"], Add) params = eval(item["data"]) expected = eval(item['expected']) method = item["method"] response = requests.request(method=method, token=self.token, headers=headers, url=url, json=params) res = response.json() print("预期结果:{}0".format(expected)) print("实际结果:{}".format(res)) try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected['msg'], res['msg']) if item["check_sql"]: info = handle_db.database_info.find_data(item["check_sql"].format(self.uesr_id)) self.assertTrue(info) except AssertionError as e: handle_logs.log.error("失败,记录信息{}".format(item["title"])) handle_logs.log.exception(e) self.file_name.write_case(row=case_id, coulmn=8, value="失败") raise e else: handle_logs.log.info("成功,记录信息为{}".format(item["title"])) self.file_name.write_case(row=case_id, coulmn=8, value="成功")
class Test_Register(unittest.TestCase): file_name = handle_excel.Test_case(os.path.join(handle_path.DATA_PATH, "test_case.xlsx"), "register") test_case = file_name.test_cases() @myddt.data(*test_case) def test_register(self, item): url = handle_conf.Conf.get("env", "url") + item["url"] headers = eval(handle_conf.Conf.get("env", "headers")) case_id = item["case_id"] + 1 method = item["method"] if "#mobile#" in item["data"]: phone = self.random_phone() item["data"] = item["data"].replace("#mobile#", str(phone)) params = eval(item["data"]) expected = eval(item["expected"]) response = requests.request(url=url, headers=headers, method=method, json=params) res = response.json() print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) if item["check_sql"]: check_data = handle_db.database_info.find_data(item["check_sql"].format(phone)) self.assertTrue(check_data) except AssertionError as e: handle_logs.log.error("失败,信息为{}".format(item['title'])) handle_logs.log.exception(e) self.file_name.write_case(row=case_id, coulmn=8, value="失败") raise e else: handle_logs.log.info("成功,信息为{}".format(item["title"])) self.file_name.write_case(row=case_id, coulmn=8, value="成功") @staticmethod def random_phone(): while True: phone = random.randint(13300000000, 13399999999) sql = "SELECT mobile_phone FROM futureloan.member where mobile_phone = {}".format(phone) res = handle_db.database_info.find_data(sql) if not res: return phone
class TestLogin(unittest.TestCase): case_file = handle_excel.Test_case(os.path.join(handle_path.DATA_PATH, "GL_test_case.xlsx"), "order_bwd_baoping") test_case = case_file.test_cases() @classmethod def setUpClass(cls): fixture.setup_login(cls) @myddt.data(*test_case) # 纯霸王豆支付,线上爆品 def test_order_pay_type8(self, items): url = handle_conf.Conf.get('env', 'url') + items["url"] method = items["method"] headers = self.normal_header case_id = items["case_id"] + 1 check_token = self.check_token_value # 将excel内的替换参数进行参数化替换; if "#checktoken#" in items["data"]: items["data"] = items["data"].replace("#checktoken#", check_token) if "#goodsInfoId#" in items["data"]: items["data"] = items["data"].replace("#goodsInfoId#", str(tools.get_good_info_id( handle_conf.Conf.get("goods", "goods_info_item_no_bwd_baopin")))) if "#payId#" in items["data"]: items["data"] = items["data"].replace("#payId#", handle_conf.Conf.get("goods", "payId_bwd_baopin")) if "#merchantNo#" in items["data"]: items["data"] = items["data"].replace("#merchantNo#", str( tools.get_merchant_no(handle_conf.Conf.get("goods", "goods_info_item_no_bwd_baopin")))) params = eval(items["data"]) expected = eval(items["expected"]) res = requests.request(url=url, json=params, headers=headers, method=method).json() try: self.assertEqual(expected["status"], res["status"]) self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["message"], res["message"]) except AssertionError as e: handle_logs.log.error("用例执行失败,记录信息为-----{}-----".format(items['title'])) handle_logs.log.error(e) self.case_file.write_case(row=case_id, coulmn=8, value="失败") raise e else: handle_logs.log.info("用例执行成功,记录信息为-----{}-----".format(items['title'])) self.case_file.write_case(row=case_id, coulmn=8, value="成功")
class TestLogin(unittest.TestCase): case_file = handle_excel.Test_case( os.path.join(handle_path.DATA_PATH, "GL_test_case.xlsx"), "login") test_case = case_file.test_cases() @myddt.data(*test_case) def test_login_pass(self, items): case_id = items["case_id"] if items["title"] != "密码错误": login_header = { "Content-Type": "application/json", "GL_DEVICE_ID": "test", "GL_CLIENT_ID": "test", "GL_CLIENT_VER": "test", "GL_TIMESTAMP": "test", "GL_REQ_SIGN": "{}".format( tools.get_login_sign_from_db( eval(self.test_case[case_id - 1]["data"])["username"])[0]) } else: login_header = { "Content-Type": "application/json", "GL_DEVICE_ID": "test", "GL_CLIENT_ID": "test", "GL_CLIENT_VER": "test", "GL_TIMESTAMP": "test", "GL_REQ_SIGN": "{}".format( tools.get_error_pwd_sign( mobile=eval(self.test_case[case_id - 1]["data"])["username"], error_pwd=eval( (self.test_case[case_id - 1])["data"])["password"])) } url = handle_conf.Conf.get("env", "url") + items["url"] # items["data"] = handle_data.replace_data(items["data"], TestLogin).format( # self.test_case[case_id - 1]["data"]["username"]) params = eval(items["data"]) expected = eval(items["expected"]) res = requests.request(url=url, method=items["method"], json=params, headers=login_header).json() try: self.assertEqual(expected["result"], res["result"]) self.assertEqual(expected["description"], res["description"]) except AssertionError as e: handle_logs.log.error("用例{}未通过".format(items["title"])) handle_logs.log.exception(e) self.case_file.write_case(row=case_id + 1, coulmn=8, value="失败") raise e else: handle_logs.log.info("用例{}通过".format(items["title"])) self.case_file.write_case(row=case_id + 1, coulmn=8, value="成功")
class Test_Aduit(unittest.TestCase): case_file = handle_excel.Test_case( os.path.join(handle_path.DATA_PATH, "test_case.xlsx"), "audit") test_case = case_file.test_cases() @classmethod def setUpClass(cls): fixture.setup_login(cls) fixture.setup_login_admin(cls) def setUp(self): url = handle_conf.Conf.get("env", "url") + "/loan/add" headers = eval(handle_conf.Conf.get("env", "headers")) headers["Authorization"] = self.token params = { "member_id": self.user_id, "title": "fix_title", "amount": 3000, "loan_rate": 12.0, "loan_term": 3, "loan_date_type": 1, "bidding_days": 5 } response = requests.post(url=url, headers=headers, json=params) res = response.json() loan_id = jsonpath.jsonpath(res, "$..id")[0] Test_Aduit.loan_id = loan_id @myddt.data(*test_case) def test_audit(self, item): url = os.path.join(handle_conf.Conf.get("env", "url")) + item["url"] headers = eval(handle_conf.Conf.get("env", "headers")) headers["Authorization"] = self.admin_token method = item["method"] item["data"] = handle_data.replace_data(item['data'], Test_Aduit) params = eval(item["data"]) expected = eval(item["expected"]) case_id = item["case_id"] + 1 response = requests.request(method=method, url=url, headers=headers, json=params) res = response.json() print("预期结果:{}0".format(expected)) print("实际结果:{}".format(res)) try: self.assertEqual(res["code"], expected["code"]) self.assertEqual(res["msg"], expected["msg"]) if item["check_sql"]: status = handle_db.database_info.find_data( item["check_sql"].format(params["loan_id"]))[0] self.assertEqual(expected["status"], status["status"]) except AssertionError as e: handle_logs.log.error("未通过,记录信息为{}".format(item["title"])) handle_logs.log.exception(e) self.case_file.write_case(row=case_id, coulmn=8, value="失败") raise e else: handle_logs.log.info("通过,记录信息为{}".format(item["title"])) self.case_file.write_case(row=case_id, coulmn=8, value="成功")
class Test_Recharge(unittest.TestCase): file_name = handle_excel.Test_case( os.path.join(handle_path.DATA_PATH, "test_case.xlsx"), "recharge") test_case = file_name.test_cases() @classmethod def setUpClass(cls): fixture.setup_login(cls) # method = "post" # url = handle_conf.Conf.get("env", "url") + "/member/login" # params = {"mobile_phone": handle_conf.Conf.get("test_data", "mobile"), # "pwd": handle_conf.Conf.get("test_data", "pwd")} # headers = eval(handle_conf.Conf.get("env", "headers")) # response = requests.request(method=method, url=url, json=params, headers=headers) # res = response.json() # tk = jsonpath.jsonpath(res, "$..token")[0] # cls.user_id = jsonpath.jsonpath(res, "$..id")[0] # cls.token = "Bearer" + " " + tk @myddt.data(*test_case) def test_recharge(self, item): url = handle_conf.Conf.get("env", "url") + item["url"] method = item['method'] headers = eval(handle_conf.Conf.get("env", "headers")) headers["Authorization"] = self.token item["data"] = handle_data.replace_data(item["data"], Test_Recharge) params = eval(item["data"]) expected = eval(item["expected"]) case_id = item["case_id"] + 1 if item["check_sql"]: old_amount = handle_db.database_info.find_data( item["check_sql"].format(self.user_id)) old_money = old_amount[0]["leave_amount"] response = requests.request(method=method, url=url, json=params, headers=headers, token=self.token) res = response.json() print("预期结果:{}0".format(expected)) print("实际结果:{}".format(res)) try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res['msg']) if item["check_sql"]: new_amount = handle_db.database_info.find_data( item["check_sql"].format(self.user_id)) new_money = new_amount[0]["leave_amount"] self.assertEqual(float(new_money - old_money), params["amount"]) except AssertionError as e: handle_logs.log.error("失败,信息为{}".format(item['title'])) handle_logs.log.exception(e) self.file_name.write_case(row=case_id, coulmn=8, value="失败") raise e else: handle_logs.log.info("成功,信息为{}".format(item["title"])) self.file_name.write_case(row=case_id, coulmn=8, value="成功")
class Test_Invest(unittest.TestCase): case_file = handle_excel.Test_case( os.path.join(handle_path.DATA_PATH, "test_case.xlsx"), "invest") test_case = case_file.test_cases() @classmethod def setUpClass(cls): fixture.setup_login_admin(cls) fixture.setup_login(cls) fixture.setup_login_invest_user(cls) fixture.setup_add(cls) aduit_url = handle_conf.Conf.get("env", "url") + "/loan/audit" headers = eval(handle_conf.Conf.get("env", "headers")) headers["Authorization"] = cls.admin_token params = {"loan_id": cls.loan_id, "approved_or_not": True} response = requests.patch(url=aduit_url, headers=headers, json=params) # res = response.json() @myddt.data(*test_case) def test_invest(self, item): url = handle_conf.Conf.get("env", "url") + item["url"] headers = eval(handle_conf.Conf.get('env', 'headers')) headers["Authorization"] = self.invest_token item["data"] = handle_data.replace_data(item["data"], Test_Invest) params = eval(item["data"]) method = item['method'] expected = eval(item["expected"]) case_id = item["case_id "] + 1 if item["check_sql"]: """查询原始表该投资用户在表内的投资记录""" sql1 = "SELECT * FROM futureloan.invest WHERE member_id ={} and loan_id ={}".format( self.invest_id, self.loan_id) """余额""" sql2 = "SELECT * FROM futureloan.member WHERE member_id ={}".format( self.invest_id) """流水记录""" sql3 = "SELECT * FROM futureloan.financelog WHERE pay_member_id = {}".format( self.invest_id) f_invest = len(handle_db.database_info.find_data(sql1)) f_amount = handle_db.database_info.find_data(sql2)[0]["amount"] f_financelog = len(handle_db.database_info.find_data(sql3)) response = requests.request(method=method, url=url, headers=headers, json=params) res = response.json() print("预期结果:{}0".format(expected)) print("实际结果:{}".format(res)) try: self.assertEqual(expected['code'], res["code"]) self.assertEqual(expected['msg'], res['msg']) if item["check_sql"]: sql1 = "SELECT * FROM futureloan.invest WHERE member_id ={} and loan_id ={}".format( self.invest_id, self.loan_id) sql2 = "SELECT * FROM futureloan.member WHERE member_id ={}".format( self.invest_id) sql3 = "SELECT * FROM futureloan.financelog WHERE pay_member_id = {}".format( self.invest_id) s_invest = len(handle_db.database_info.find_data(sql1)) s_amount = handle_db.database_info.find_data(sql2)[0]["amount"] s_financelog = len(handle_db.database_info.find_data(sql3)) self.assertEqual(s_invest - f_invest, 1) self.assertEqual(s_amount - f_amount, params["amount"]) self.assertEqual(s_financelog - f_financelog, 1) except AssertionError as e: handle_logs.log.error("失败,记录信息为{}".format(item['title'])) handle_logs.log.exception(e) self.case_file.write_case(row=case_id, coulmn=8, value="失败") else: handle_logs.log.info("成功,记录信息为{}".format(item['title'])) self.case_file.write_case(row=case_id, coulmn=8, value="成功")