def assert_contains(content, target): """ 断言包含 :param content: 文本内容 :param target: 目标文本 :return: """ return HandleAssert.contains(content, target)
def assert_equal(ex, re): """ 断言相等 :param ex:预期结果 :param re:实际结果 :return: """ return HandleAssert.eq(ex, re)
def testRecharge(self, case): logger.info("*****************开始执行充值接口用例**************************") # 替换 case = replace_case_with_re(case) logger.info("当前测试用例为:\n {}".format(case)) # 发起请求 # 判断是否要传递token值 if hasattr(Data, "token"): res = self.hr.send_requests(case["method"], case["url"], case["request_data"], token=getattr(Data, "token")) else: res = self.hr.send_requests(case["method"], case["url"], case["request_data"]) # 如果有提取字段,那么需要从响应中提取对应数据,设置为Data.token if case["extract"]: set_dataclass_attr_from_resp(res.json(), case["extract"]) # 如果有预期结果,需要把响应结果与预期结果进行比较 if case["expected"]: # 响应结果 actual = res.json() logger.info("用例实际执行结果:\n {}".format(actual)) expected = eval(case["expected"]) logger.info("用例预期结果:\n {}".format(expected)) # 断言 try: assert actual["code"] == expected["code"] assert actual["msg"] == expected["msg"] if actual["data"]['leave_amount']: assert actual["data"]['leave_amount'] == float( getattr(Data, "money")) except AssertionError: logger.exception("断言失败!") raise # 把异常抛给unittest框架 except Exception: logger.exception("除断言以外的异常报错!") raise # 如果有数据库校验,则进行数据库校验 if case["check_sql"]: ha = HandleAssert() ha.assert_sql(case["check_sql"])
def test_register(self, case): logger.info( "*************************** 开始执行注册接口测试用例 ***************************" ) # 替换手机号 case = replace_case_with_re(case) # 替换需要替换的--系统中已存在的手机号--从配置文件读取 if case["request_data"].find("*phone*") != -1: phone = conf.get("user", "user") case["request_data"] = case["request_data"].replace( "*phone*", phone) # 替换需要替换的--系统中已存在的手机号--从配置文件读取 if case["request_data"].find("*pwd*") != -1: pwd = conf.get("user", "pwd") case["request_data"] = case["request_data"].replace("*pwd*", pwd) # 发起请求 resp = self.hr.send_requests(case["method"], case["url"], case["request_data"]) logger.info("当前测试用例为:\n {}".format(case)) if case["expected"]: # 响应结果 actual = resp.json() logger.info("用例实际执行结果:\n {}".format(actual)) expected = eval(case["expected"]) logger.info("用例预期结果:\n {}".format(expected)) # 断言 try: assert actual["code"] == expected["code"] assert actual["msg"] == expected["msg"] except AssertionError: logger.exception("断言失败!") raise # 把异常抛给unittest框架 except Exception: logger.exception("除断言以外的异常报错!") raise if case["check_sql"]: sleep(0.5) # 等待服务器数据与数据库交互完成 ha = HandleAssert() ha.assert_sql(case["check_sql"])
def setUpClass(cls) -> None: cls.hr = HandleRequests() cls.hassert = HandleAssert()