def test_001(self,item): global result#指明全球变量 global COOKIES#设置cookies为全局变量 url=item["url"] method=item["method"] param=item["param"] case_id=item["case_id"] modular=item["modular"] title=item["title"] expected=item["expected"] sql=item["sql"] print(sql) param=get_data.GetData().replace(param) print("现在的param是{}".format(param)) if sql is not None: sql=get_data.GetData().replace(sql) print("现在的sql是{}".format(sql)) if sql is not None and eval(sql)["sql_3"] is not None: #投资之前查询数据库用户余额保存下来 before_amount=DoMysql().do_mysql(eval(sql)["sql_3"],1)[0] print("before_sql查询结果是{}".format(before_amount)) my_log.my_info("正在发起{}模块中的第{}条用例:{}".format(modular,case_id,title)) my_log.my_info("测试数据是:{}".format(param)) test_result=DoRequests(url,method,eval(param)).request(cookies=getattr(get_data.GetData,"COOKIES")) #获取用户的member_id if sql is not None and eval(sql)["sql_1"] is not None: member_id=DoMysql().do_mysql(eval(sql)["sql_1"],1)[0] setattr(get_data.GetData,"normal_member_id",str(member_id)) #获取loan_id if sql is not None and eval(sql)["sql_2"] is not None: sql=get_data.GetData().replace(sql) loan_id=DoMysql().do_mysql(eval(sql)["sql_2"],1)[0] setattr(get_data.GetData,"loan_id",str(loan_id)) #投资之后,再做一个查询,查询数据库里面用户的余额 #增加一个判断 if test_result.cookies:#判断请求cookies是否为空 setattr(get_data.GetData,"COOKIES",test_result.cookies) my_log.my_info("测试结果是:{}".format(test_result.json())) my_log.my_info("预期结果是:{}".format(eval(expected))) try: self.assertEqual(eval(expected),test_result.json()) #增加一个判断,判断是否投资之后用户余额减少 if sql is not None and eval(sql)["sql_3"] is not None: after_amount=DoMysql().do_mysql(eval(sql)["sql_3"],1)[0] print("after_sql查询结果是{}".format(after_amount)) invest_amount=eval(param)["amount"] expected_amount=before_amount-after_amount self.assertEqual(invest_amount,expected_amount) result="pass" my_log.my_info("测试通过了") except AssertionError as e: my_log.my_error("测试失败,http请求发生错误") result="failed" raise e finally: row=case_id+1 DoExcel(project_path.case_path,"invest").write_data(row,test_result.text,result)
def test_001(self, item, login): global result # 指明全球变量 global COOKIES # 设置cookies为全局变量 # 获取item中的用例数据 url = item["url"] method = item["method"] param = item["param"] # 返回的是字符串类型的数据 case_id = item["case_id"] modular = item["modular"] title = item["title"] expected = item["expected"] sql = item["sql"] header = eval(item["header"]) # 如果有sql则把sql中需要参数化的量去进行替换 if sql is not None: sql = get_data.GetData().replace(sql) my_log.my_info("现在的sql是{}".format(sql)) # param不管里面是否有参数化都可以替代,反正返回的还是param本身,找到一个返回一个 param = get_data.GetData().replace(param) my_log.my_info("正在发起{}模块中的第{}条用例:{}".format(modular, case_id, title)) my_log.my_info("测试数据是:{}".format(param)) # 执行接口测试,cookies利用反射进行动态的获取 if "application/json" in header["Content-Type"]: new_param = param else: new_param = eval(param) test_result = DoRequests(url, method, new_param).request( headers=header, cookies=getattr(get_data.GetData, "COOKIES")) print(test_result.text) # 增加一个判断 是在完成完请求之后才去判断 if test_result.cookies: cookies = requests.utils.dict_from_cookiejar(test_result.cookies) setattr(get_data.GetData, "COOKIES", cookies) new_expected = json.loads(expected) # 处理Null无法识别的问题 my_log.my_info("测试结果是:{}".format(test_result.json())) my_log.my_info("预期结果是:{}".format(new_expected)) try: assert new_expected["code"] == test_result.json()["code"] result = "pass" my_log.my_info("测试通过了") except AssertionError as e: my_log.my_error("测试失败,http请求发生错误") result = "failed" raise e finally: row = case_id + 1 # 这里需要注意写回测试数据的时候,需要把测试数据转换成字符串类型 DoExcel(project_path.case_path, "getserialnumber").write_data(row, test_result.text, result)
def test_001(self, item): global result #指明全球变量 global COOKIES #设置cookies为全局变量 url = item["url"] method = item["method"] param = item["param"] #返回的是字符串类型的数据 case_id = item["case_id"] modular = item["modular"] title = item["title"] expected = item["expected"] sql = item["sql"] if sql != None: sql = get_data.GetData().replace(sql) print(sql) param = get_data.GetData().replace(param) print(param) my_log.my_info("正在发起{}模块中的第{}条用例:{}".format(modular, case_id, title)) my_log.my_info("测试数据是:{}".format(param)) test_result = DoRequests( url, method, eval(param)).request(cookies=getattr(get_data.GetData, "COOKIES")) #增加一个判断是否查询数据库 if sql != None: #如果测试用例中的query不为none就进行数据库查询操作 sql_data = DoMysql().do_mysql(eval(sql)["sql"], 1) member_id = sql_data[0] setattr(get_data.GetData, "normal_member_id", str(member_id)) print(getattr(get_data.GetData, "normal_member_id")) #增加一个判断 是在完成完请求之后才去判断 if test_result.cookies: #判断请求cookies是否为空 setattr(get_data.GetData, "COOKIES", test_result.cookies) my_log.my_info("测试结果是:{}".format(test_result.json())) my_log.my_info("预期结果是:{}".format(eval(expected))) # expected=eval(expected) # test_result=test_result.json() # # if case_id==2: # expected["data"]=test_result["data"] try: self.assertEqual( eval(expected)["status"], test_result.json()["status"]) self.assertEqual( eval(expected)["code"], test_result.json()["code"]) self.assertEqual(eval(expected)["msg"], test_result.json()["msg"]) result = "pass" my_log.my_info("测试通过了") except AssertionError as e: my_log.my_error("测试失败,http请求发生错误") result = "failed" raise e finally: row = case_id + 1 DoExcel(project_path.case_path, "get_InvestsByMemberId").write_data( row, test_result.text, result)
def test_001(self, item): global result #指明全球变量 global COOKIES #设置cookies为全局变量 url = item["url"] method = item["method"] param = item["param"] case_id = item["case_id"] modular = item["modular"] title = item["title"] expected = item["expected"] sql = item["sql"] print(sql) param = get_data.GetData().replace(param) print("现在的param是{}".format(param)) if sql is not None: sql = get_data.GetData().replace(sql) print(type(sql)) print("现在的sql是{}".format(sql)) my_log.my_info("正在发起{}模块中的第{}条用例:{}".format(modular, case_id, title)) my_log.my_info("测试数据是:{}".format(param)) test_result = DoRequests( url, method, eval(param)).request(cookies=getattr(get_data.GetData, "COOKIES")) #获取loan_id if sql is not None and eval(sql)["sql"] is not None: print("测试的sql{}".format(eval(sql)["sql"])) sql = get_data.GetData().replace(sql) loan_id = DoMysql().do_mysql(eval(sql)["sql"], 1)[0] setattr(get_data.GetData, "loan_id", str(loan_id)) #增加一个判断 if test_result.cookies: #判断请求cookies是否为空 setattr(get_data.GetData, "COOKIES", test_result.cookies) my_log.my_info("测试结果是:{}".format(test_result.json())) my_log.my_info("预期结果是:{}".format(eval(expected))) try: self.assertEqual( eval(expected)["status"], test_result.json()["status"]) self.assertEqual( eval(expected)["code"], test_result.json()["code"]) self.assertEqual(eval(expected)["msg"], test_result.json()["msg"]) result = "pass" my_log.my_info("测试通过了") except AssertionError as e: my_log.my_error("测试失败,http请求发生错误") result = "failed" raise e finally: row = case_id + 1 DoExcel(project_path.case_path, "get_InvestsByLoanId").write_data(row, test_result.text, result)
def test_001(self, item): global result # 指明全局变量 global COOKIES # 设置cookies为全局变量 global before_amount url = item["url"] method = item["method"] param = item["param"] case_id = item["case_id"] modular = item["modular"] title = item["title"] expected = item["expected"] sql = item["sql"] param = get_data.GetData().replace(param) print("目前的param是{}".format(param)) if sql is not None: sql = get_data.GetData().replace(sql) print("目前的sql是{}".format(sql)) before_amount = DoMysql().do_mysql(eval(sql)["sql"])[0] my_log.my_info("充值之前账户余额:{}".format(before_amount)) my_log.my_info("正在发起{}模块中的第{}条用例:{}".format(modular, case_id, title)) my_log.my_info("测试数据是:{}".format(param)) test_result = DoRequests( url, method, eval(param)).request(cookies=getattr(get_data.GetData, "COOKIES")) # 增加一个判断 if test_result.cookies: # 判断请求cookies是否为空 setattr(get_data.GetData, "COOKIES", test_result.cookies) my_log.my_info("测试结果是:{}".format(test_result.text)) my_log.my_info("预期结果是:{}".format(eval(expected))) try: self.assertEqual( eval(expected)["code"], test_result.json()["total"]) # if sql is not None: # after_amount = DoMysql().do_mysql(eval(sql)["sql"], 1)[0] # recharge_amount = int(eval(param)["amount"]) # my_log.my_info("充值的金额是:{}".format(str(recharge_amount))) # expected_amount = recharge_amount + before_amount # my_log.my_info("充值之后的用户余额:{}".format(expected_amount)) # self.assertEqual(expected_amount, after_amount) # expected = expected.replace("expectedamount", str(expected_amount)) # self.assertEqual(eval(expected)["status"], test_result.json()["status"]) # self.assertEqual(eval(expected)["code"], test_result.json()["code"]) # self.assertEqual(eval(expected)["msg"], test_result.json()["msg"]) result = "pass" my_log.my_info("测试通过了") except AssertionError as e: my_log.my_error("测试失败,http请求发生错误") result = "failed" raise e finally: row = case_id + 1 DoExcel(project_path.case_path, "invest").write_data(row, test_result.text, result)
def test_001(self,item): global result#指明全球变量 global COOKIES#设置cookies为全局变量 url=item["url"] method=item["method"] param=item["param"] case_id=item["case_id"] modular=item["modular"] title=item["title"] expected=item["expected"] sql=item["sql"] # print(sql) param=get_data.GetData().replace(param) print("现在的param是{}".format(param)) # if sql is not None: # sql=get_data.GetData().replace(sql) # print("现在的sql是{}".format(sql)) # # if sql is not None and eval(sql)["sql_2"] is not None: # sql_result=DoMysql().do_mysql(eval(sql)["sql_2"],2) # my_log.my_info("交易记录是:{}".format(sql_result)) my_log.my_info("正在发起{}模块中的第{}条用例:{}".format(modular,case_id,title)) my_log.my_info("测试数据是:{}".format(param)) if case_id == 1: new_param = json.dumps(eval(param)) headers = DoRequests(url, method, new_param).get_header(index=1) else: new_param = json.loads(param) headers = DoRequests(url, method, new_param).get_header(index=2) test_result=DoRequests(url,method,new_param).request(headers=headers,cookies=getattr(get_data.GetData,"COOKIES")) print(test_result.text) #获取loan_id # if sql is not None and eval(sql)["sql_1"] is not None: # sql=get_data.GetData().replace(sql) # member_id=DoMysql().do_mysql(eval(sql)["sql_1"],1)[0] # setattr(get_data.GetData,"normal_member_id",str(member_id)) # if sql is not None and eval(sql)["sql_2"] is not None: # sql=get_data.GetData().replace(sql) # sql_result=DoMysql().do_mysql(eval(sql)["sql_2"],1)[0] #增加一个判断 if test_result.cookies:#判断请求cookies是否为空 # c = requests.cookies.RequestsCookieJar()# c.set('cookie-name', 'cookie-value', path='/', domain='.abc.com')# s.cookies.update(c) cookies = requests.utils.dict_from_cookiejar(test_result.cookies) setattr(get_data.GetData,"COOKIES",cookies) new_expected=json.loads(expected) my_log.my_info("测试结果是:{}".format(test_result.json())) my_log.my_info("预期结果是:{}".format(new_expected)) try: self.assertEqual(new_expected["code"],test_result.json()["code"]) result="pass" my_log.my_info("测试通过了") except Exception as e: my_log.my_error("测试失败,http请求发生错误") result="failed" raise e finally: row=case_id+1 DoExcel(project_path.case_path,"getWarningDetail").write_data(row,test_result.text,result)
def test_001(self,item): global param global url global method global case_id global modular global title global expected url=item["url"] method=item["method"] param=item["param"] case_id=item["case_id"] modular=item["modular"] title=item["title"] expected=item["expected"] sql=item["sql"] param=get_data.GetData().replace(param) mylog.my_info("参数化之后param是{}".format(param)) headers = {"User-Agent": "Mozilla/5.0"} test_result=DoRequests(url,method,eval(param)).request(cookies=getattr(get_data.GetData,"COOKIES")) # do_session = Do_Requests() # # 登录 # test_result=do_session(url=url, method=method, data=param,headers=headers) if test_result.cookies:#判断请求cookies是否为空 setattr(get_data.GetData,"COOKIES",test_result.cookies) mylog.my_info("接口返回的http状态码:{}".format(test_result)) if case_id == 2: mylog.my_info("测试结果是:{}".format(test_result)) mylog.my_info("预期结果是:{}".format(expected)) else: mylog.my_info("测试结果是:{}".format(test_result.json())) mylog.my_info("预期结果是:{}".format(eval(expected))) try: self.assertEqual(eval(expected)["Code"],test_result.json()["Code"]) self.assertEqual(eval(expected)["Message"],test_result.json()["Message"]) result="pass" mylog.my_info("测试通过了") except AssertionError as e: mylog.my_error("测试失败,http请求发生错误") result="failed" raise e finally: row=case_id+1 DoExcel(project_path.case_path,"generate_repayments").write_data(row,test_result.text,result)
def test_001(self, item): global result #指明全球变量 global COOKIES #设置cookies为全局变量 global before_amount url = item["url"] method = item["method"] param = item["param"] case_id = item["case_id"] modular = item["modular"] title = item["title"] expected = item["expected"] sql = item["sql"] param = get_data.GetData().replace(param) print("目前的param是{}".format(param)) my_log.my_info("正在发起{}模块中的第{}条用例:{}".format(modular, case_id, title)) my_log.my_info("测试数据是:{}".format(param)) test_result = DoRequests(url, method, param=eval(param)).request( cookies=getattr(get_data.GetData, "COOKIES")) #增加一个判断 if test_result.cookies: #判断请求cookies是否为空 setattr(get_data.GetData, "COOKIES", test_result.cookies) my_log.my_info("测试结果是:{}".format(test_result.json())) my_log.my_info("预期结果是:{}".format(eval(expected))) try: self.assertEqual( eval(expected)["status"], test_result.json()["status"]) self.assertEqual( eval(expected)["code"], test_result.json()["code"]) result = "pass" my_log.my_info("测试通过了") except AssertionError as e: my_log.my_error("测试失败,http请求发生错误") result = "failed" raise e finally: row = case_id + 1 DoExcel(project_path.case_path, "getLoanList").write_data(row, test_result.text, result) #写回的数据格式必须是字符串或者数字类型才可以
def test_001(self, item): global result #指明全球变量 global COOKIES #设置cookies为全局变量 global before_amount global withdraw_amount url = item["url"] method = item["method"] param = item["param"] case_id = item["case_id"] modular = item["modular"] title = item["title"] expected = item["expected"] sql = item["sql"] #参数化测试数据 param = get_data.GetData().replace(param) my_log.my_info("目前的param是{}".format(param)) #判断是否需要执行sql 如果需要执行则运行代码 if sql is not None: sql = get_data.GetData().replace(sql) print("目前的sql是{}".format(sql)) before_amount = DoMysql().do_mysql(eval(sql)["sql"])[0] my_log.my_info("体现之前的用户余额是:{}".format(before_amount)) my_log.my_info("正在发起{}模块中的第{}条用例:{}".format(modular, case_id, title)) #传入参数 进行接口测试 test_result = DoRequests( url, method, eval(param)).request(cookies=getattr(get_data.GetData, "COOKIES")) # print(test_result) #打印测试结果与实际结果 # my_log.my_info("目前的测试结果是{}".format(test_result.json())) # my_log.my_info("预期结果是:{}".format(eval(expected))) #增加一个判断 传递cookies if test_result.cookies: #判断请求cookies是否为空 setattr(get_data.GetData, "COOKIES", test_result.cookies) #打印出测试结果与预期结果 my_log.my_info("测试结果是:{}".format(test_result.json())) my_log.my_info("预期结果是:{}".format(eval(expected))) #测试结果与预期结果进行断言 try: if sql is not None: after_amount = DoMysql().do_mysql(eval(sql)["sql"], 1)[0] my_log.my_info("提现之后的用户余额是:{}".format(after_amount)) withdraw_amount = int(eval(param)["amount"]) my_log.my_info("提现金额是:{}".format(str(withdraw_amount))) expected_amount = before_amount - withdraw_amount self.assertEqual(expected_amount, after_amount) expected = expected.replace("expectedamount", str(expected_amount)) my_log.my_info("预期的用户余额是:{}".format(expected_amount)) self.assertEqual( eval(expected)["status"], test_result.json()["status"]) self.assertEqual( eval(expected)["code"], test_result.json()["code"]) self.assertEqual(eval(expected)["msg"], test_result.json()["msg"]) result = "pass" my_log.my_info("测试通过了") except AssertionError as e: my_log.my_error("测试失败,http请求发生错误") result = "failed" raise e finally: #写回测试结果 row = case_id + 1 DoExcel(project_path.case_path, "withdraw").write_data(row, test_result.text, result)
def test_001(self, item): global result #指明全球变量 global COOKIES #设置cookies为全局变量 #获取测试用例数据,数据类型基本都为字符串类型 url = item["url"] method = item["method"] param = item["param"] case_id = item["case_id"] modular = item["modular"] title = item["title"] expected = item["expected"] sql = item["sql"] #参数化param param = get_data.GetData().replace(param) my_log.my_info("参数化之后param是{}".format(param)) #参数化sql my_log.my_info("参数化之前的sql:".format(sql)) if sql is not None: sql = get_data.GetData().replace(sql) my_log.my_info("参数化之后的sql是{}".format(sql)) my_log.my_info("正在发起{}模块中的第{}条用例:{}".format(modular, case_id, title)) #输入参数进行接口测试 test_result = DoRequests( url, method, eval(param)).request(cookies=getattr(get_data.GetData, "COOKIES")) #获取用户的member_id if sql is not None and eval(sql)["sql_1"] is not None: member_id = DoMysql().do_mysql(eval(sql)["sql_1"], 1)[0] setattr(get_data.GetData, "normal_member_id", str(member_id)) #获取loan_id if sql is not None and eval(sql)["sql_2"] is not None: sql = get_data.GetData().replace(sql) loan_id = DoMysql().do_mysql(eval(sql)["sql_2"], 1)[0] setattr(get_data.GetData, "loan_id", str(loan_id)) #投资之后,再做一个查询,查询数据库里面用户的余额 #增加一个判断 if test_result.cookies: #判断请求cookies是否为空 setattr(get_data.GetData, "COOKIES", test_result.cookies) my_log.my_info("接口返回的http状态码:{}".format(test_result)) my_log.my_info("测试结果是:{}".format(test_result.json())) my_log.my_info("预期结果是:{}".format(eval(expected))) try: self.assertEqual(eval(expected), test_result.json()) result = "pass" my_log.my_info("测试通过了") except AssertionError as e: my_log.my_error("测试失败,http请求发生错误") result = "failed" raise e finally: row = case_id + 1 DoExcel(project_path.case_path, "generate_repayments").write_data(row, test_result.text, result)