def setUpClass(cls): """ 执行用例前调用 :return: """ cls.send_request = HttpRequest() do_log.info("\n{:=^40s}".format("开始执行登录接口用例"))
def tearDownClass(cls): """ 所有用例结束后调用 :return: """ cls.send_request.close_session() do_log.info("\n{:=^40s}".format("登录接口用例执行结束"))
def tearDownClass(cls): """ 重写父类类方法,在实例方法执行结束之后会被调用一次 :return: """ cls.send_request.close() do_log.info("\n{:=^40s}".format("注册功能用例执行结束"))
def setUpClass(cls): """ 重写父类类方法,在实例方法执行之前会被调用一次 :return: """ cls.send_request = HttpRequest() do_log.info("\n{:=^40s}".format("开始执行注册功能用例"))
def setUpClass(cls): """ 重写父类类方法,在实例方法执行之前会被调用一次 :return: """ cls.send_request = HttpRequest() cls.handle_mysql = HandleMysql() do_log.info("\n{:=^40s}".format("开始执行充值功能用例"))
def __call__(self,method,url,data=None,is_json=False,**kwargs): method = method.lower() if isinstance(data,str): try: data = json.loads(data) except Exception as e: # print(e) do_log.info("将json转换为Python数据类型时出现异常,异常为{}".format(e)) data=eval(data) if method == "get": res = self.one_session.request(method=method,url=url,params=data,**kwargs) elif method == "post": if is_json: res = self.one_session.request(method=method,url=url,json=data,**kwargs) # 如果使用json格式来传参 else: res = self.one_session.request(method=method,url=url,data=data,**kwargs) else: res = None do_log.error("不支持【{}】请求方法".format(method)) return res
def test_add_patient(self, data_namedtuple): do_log.info("\nrunning test method:{}".format(inspect.stack()[0][3])) data = Context().add_patient_parameterization(data_namedtuple.data) id = data_namedtuple.id method = data_namedtuple.method title = data_namedtuple.title url = do_config("login", "url") + data_namedtuple.url result = self.sent_request(method=method, url=url, data=data) if id == 1: self.assertEqual(result.status_code, data_namedtuple.expected, msg="{}期望值与实际值不一致".format(title)) elif id == 2: self.assertEqual(result.status_code, data_namedtuple.expected, msg="{}期望值与实际值不一致".format(title)) else: self.assertEqual(result.json()["message"], data_namedtuple.expected, msg="{}期望值与实际值不一致".format(title))
def test_login(self, data_namedtuple): do_log.info("\nrunning test method:{}".format(inspect.stack()[0][3])) data = data_namedtuple.data url = do_config("login", "url") + data_namedtuple.url res = self.send_request(method=data_namedtuple.method, url=url, data=data) # result = res.json() if data_namedtuple.id == 1: self.assertEqual(res.json()['login'], data_namedtuple.expected, msg='{}的期望值与实际值不一致'.format(data_namedtuple.title)) elif data_namedtuple.id == 2: self.assertEqual(res.json()['login'], data_namedtuple.expected, msg='{}的期望值与实际值不一致'.format(data_namedtuple.title)) elif data_namedtuple.id == 3: self.assertEqual(res.json()['login'], data_namedtuple.expected, msg='{}的期望值与实际值不一致'.format(data_namedtuple.title)) elif data_namedtuple.id == 4: self.assertEqual(res.status_code, data_namedtuple.expected, msg='{}的期望值与实际值不一致'.format(data_namedtuple.title)) elif data_namedtuple.id == 5: self.assertEqual(res.status_code, data_namedtuple.expected, msg='{}的期望值与实际值不一致'.format(data_namedtuple.title)) elif data_namedtuple.id == 6: self.assertEqual(res.json()['rows'][0]['status'], data_namedtuple.expected, msg='{}的期望值与实际值不一致'.format(data_namedtuple.title)) elif data_namedtuple.id == 7: self.assertEqual(res.json()['rows'][0]['status'], data_namedtuple.expected, msg='{}的期望值与实际值不一致'.format(data_namedtuple.title)) else: self.assertEqual(res.json()['rows'][0]['status'], data_namedtuple.expected, msg='{}的期望值与实际值不一致'.format(data_namedtuple.title))
def test_register(self, data_namedtuple): """ 测试注册功能 :return: """ # print("\nrunning test method:{}".format(inspect.stack()[0][3])) do_log.info("\nrunning test method:{}".format(inspect.stack()[0][3])) # 获取两个负数相乘的结果 # 将实际结果写入Excel run_success_msg = do_config("msg", "success_result") run_fail_msg = do_config("msg", "fail_result") new_data = Context.register_parameterization(data_namedtuple.data) new_url = do_config("api", "prefix_url") + data_namedtuple.url response = self.send_request(data_namedtuple.method, url=new_url, data=new_data) try: self.assertEqual(data_namedtuple.expected, response.text, msg="测试{}失败".format(data_namedtuple.title)) except AssertionError as e: do_log.error("具体异常为:{}".format(e)) do_excel.write_result(row=data_namedtuple.case_id + 1, actual=response.text, result=do_config("msg", "fail_result")) # self.assertRaises(TypeError) # assertRaises可以直接断言异常 # raise关键字是将某个异常主动抛出 raise e else: # ws.cell(row=case_id+1,column=7,value="Pass") # self.test_obj.write_result(row=case_id+1,actual=real_result,result="Pass") do_excel.write_result(row=data_namedtuple.case_id + 1, actual=response.text, result=do_config("msg", "success_result"))
def test_recharge(self, data_namedtuple): """ 测试充值功能 :return: """ do_log.info("\nrunning test method:{}".format(inspect.stack()[0][3])) # 将实际结果写入Excel run_success_msg = do_config("msg", "success_result") run_fail_msg = do_config("msg", "fail_result") new_data = Context.register_parameterization(data_namedtuple.data) new_url = do_config("api", "prefix_url") + data_namedtuple.url check_sql = data_namedtuple.check_sql # 需要执行一条sql语句,判断check_sql存不存在 if check_sql: check_sql = Context.recharge_parameterization(check_sql) mysql_data = self.handle_mysql(check_sql) # 获取到的是decimal.Decimal类型的数据,需要转化为float类型 amount_before_recharge = float(mysql_data["LeaveAmount"]) # 因为float转化有可能不是两位小数,而是后面跟着一大串随机数,所以这边我们需要四舍五入,保留两位小数 amount_before_recharge = round(amount_before_recharge, 2) response = self.send_request(data_namedtuple.method, url=new_url, data=new_data) # 如果code因为网络不好或者其他原因获取不到,那么我们就不需要往下执行了,所以这边也加个断言 try: self.assertEqual(200, response.status_code, msg="测试{}请求失败,状态码为{}".format( data_namedtuple.title, response.status_code)) except AssertionError as e: do_log.error("具体异常为{}".format(e)) raise code = response().json().get["code"] try: # excel当中的expected是数字类型,但是.json.get[]获取的是字符串,所以我们断言时得转换成字符串类型 self.assertEqual(str(data_namedtuple.expected), code, msg="测试{}失败".format(data_namedtuple.title)) # 我们上面断言了状态码,但是充值金额没有断言,所以还要继续断言下充值金额 if check_sql: check_sql = Context.recharge_parameterization(check_sql) mysql_data = self.handle_mysql(check_sql) amount_after_recharge = float(mysql_data["LeaveAmount"]) amount_after_recharge = round(amount_after_recharge) # 获取Excel,data中amount,首先把data的json格式数据转化为字典 one_dict = json.loads(new_data, encoding="utf-8") # current_recharge_amount = one_dict["amount"] # 当前的金额 current_recharge_amount = one_dict.get( "amount") # 或者字典中可以通过get来获取 actual_amount = round(amount_before_recharge + current_recharge_amount, 2) # 实际金额 self.assertEqual(actual_amount, amount_after_recharge, msg="充值金额有误") except AssertionError as e: do_log.error("具体异常为:{}".format(e)) do_excel.write_result(row=data_namedtuple.case_id + 1, actual=response.text, result=do_config("msg", "fail_result")) # self.assertRaises(TypeError) # assertRaises可以直接断言异常 # raise关键字是将某个异常主动抛出 raise e else: # ws.cell(row=case_id+1,column=7,value="Pass") # self.test_obj.write_result(row=case_id+1,actual=real_result,result="Pass") do_excel.write_result(row=data_namedtuple.case_id + 1, actual=response.text, result=do_config("msg", "success_result"))
def test_invest(self, data_namedtuple): """ 测试投资功能 :return: """ do_log.info("\nrunning test method:{}".format(inspect.stack()[0][3])) # 将实际结果写入Excel run_success_msg = do_config("msg", "success_result") run_fail_msg = do_config("msg", "fail_result") new_data = Context.invest_parameterization(data_namedtuple.data) new_url = do_config("api", "prefix_url") + data_namedtuple.url response = self.send_request(data_namedtuple.method, url=new_url, data=new_data) # 如果code因为网络不好或者其他原因获取不到,那么我们就不需要往下执行了,所以这边也加个断言 try: self.assertEqual(200, response.status_code, msg="测试{}请求失败,状态码为{}".format( data_namedtuple.title, response.status_code)) except AssertionError as e: do_log.error("具体异常为{}".format(e)) raise # 如果接口返回的是加标成功,再判断sql if response.json().get("msg") == "加标成功": check_sql = data_namedtuple.check_sql if check_sql: check_sql = Context.invest_parameterization(check_sql) mysql_data = self.handle_mysql(check_sql) # 获取标ID # loan_id = mysql_data.get("Id") # 标ID要获取全局的 # global LOAN_ID # LOAN_ID = mysql_data.get("Id") # 给Context创建一个loan_id的类属性 # 方法一: Context.loan_id = mysql_data.get("Id") # 方法二:(方法一更好理解) # setattr(Context,"loan_id") code = response().json().get["code"] try: # excel当中的expected是数字类型,但是.json.get[]获取的是字符串,所以我们断言时得转换成字符串类型 self.assertEqual(str(data_namedtuple.expected), code, msg="测试{}失败".format(data_namedtuple.title)) # 我们上面断言了状态码,但是充值金额没有断言,所以还要继续断言下充值金额 except AssertionError as e: do_log.error("具体异常为:{}".format(e)) do_excel.write_result(row=data_namedtuple.case_id + 1, actual=response.text, result=do_config("msg", "fail_result")) # self.assertRaises(TypeError) # assertRaises可以直接断言异常 # raise关键字是将某个异常主动抛出 raise e else: # ws.cell(row=case_id+1,column=7,value="Pass") # self.test_obj.write_result(row=case_id+1,actual=real_result,result="Pass") do_excel.write_result(row=data_namedtuple.case_id + 1, actual=response.text, result=do_config("msg", "success_result"))
def tearDownClass(cls): cls.sent_request.close_session() do_log.info("\n{:=^40s}".format("新增患者功能用例执行结束"))
def setUpClass(cls): cls.sent_request = HttpRequest() do_log.info("\n{:=^40s}".format("开始执行新增患者功能用例"))