def test_register(self, case): # 准备用例数据 method = case["method"] url = conf.get("env", "url") + case["url"] if case["interface"] == "register": # 注册接口,则随机生成一个用户名和email EnvData.name = self.random_username() EnvData.email = self.random_email() data = eval(replace_data(case["data"])) expected = eval(replace_data(case["expected"])) row = case["case_id"] + 1 # 第二步:发送请求获取实际结果 res = (requests.request(method=method, url=url, json=data)).json() # 第三步:断言 try: assert_dict(expected, res) # 判断是否需要进行sql校验 if case["check_sql"]: sql = replace_data(case["check_sql"]) res = db.find_count(sql) self.assertEqual(1, res) except AssertionError as e: # 结果回写excel中 log.error("用例--{}--执行未通过".format(case["title"])) log.debug("预期结果:{}".format(expected)) log.debug("实际结果:{}".format(res)) log.exception(e) self.excel.write_data(row=row, column=8, value="未通过") raise e else: # 结果回写excel中 log.info("用例--{}--执行通过".format(case["title"])) self.excel.write_data(row=row, column=8, value="通过")
def test_interface(self, case): login() newproject() # 1.准备用例数据 EnvData.name = random_intername() url = conf.get("env", "url") + case["url"] method = case["method"] data = eval(replace_data(case["data"])) expected = eval(replace_data(case["expected"])) headers = {"Authorization": getattr(EnvData, "token")} row = case["case_id"] + 1 if case["check_sql"]: sql = replace_data(case["check_sql"]) start_count = db.find_count(sql) res = (request(method=method, url=url, json=data, headers=headers)).json() # 第三步,断言预期结果和实际结果 try: assert_dict(expected, res) if case["check_sql"]: sql = replace_data(case["check_sql"]) end_count = db.find_count(sql) self.assertEqual(1, end_count - start_count) except AssertionError as e: log.error("用例--{}--执行未通过".format(case["title"])) log.debug("预期结果:{}".format(expected)) log.debug("实际结果:{}".format(res)) log.exception(e) self.excel.write_data(row=row, column=8, value="未通过") raise e else: # 结果回写excel中 log.info("用例--{}--执行通过".format(case["title"])) self.excel.write_data(row=row, column=8, value="通过")
def test_login(self, case): method = case["method"] url = conf.get("env", "url") + case["url"] data = eval(case["data"]) expected = eval(case["expected"]) row = case["case_id"] + 1 res = (request(method, url, json=data)).json() try: assert_dict(expected, res) except AssertionError as e: # 结果回写excel中 log.error("用例--{}--执行未通过".format(case["title"])) log.debug("预期结果:{}".format(expected)) log.debug("实际结果:{}".format(res)) log.exception(e) self.excel.write_data(row=row, column=8, value="未通过") raise e else: # 结果回写excel中 log.info("用例--{}--执行通过".format(case["title"])) self.excel.write_data(row=row, column=8, value="通过")