class TestAdd(unittest.TestCase): """加标接口""" data_file_path = os.path.join(DATA_DIR, "case_data.xlsx") excel = ReadExcel(data_file_path, 'add') cases = excel.read_data_obj() http = HTTPSession() db = ReadMysql(my_conf.get('mysql', 'host'), my_conf.get('mysql', 'user'), my_conf.get('mysql', 'password'), my_conf.getint('mysql', 'port'), my_conf.get('mysql', 'database')) @classmethod def setUpClass(cls): logger.debug("-------开始加标接口测试{}-------") @classmethod def tearDownClass(cls): logger.debug("-------结束加标接口测试{}-------") @data(*cases) def test_add(self, case): # 第一步:入参和用例数据 url = my_conf.get('url', 'url') + case.url + case.interface case.data = data_replace(case.data) if "*memberId*" in case.data: max_id = self.db.find_one("select max(id) from member")[0] memberId = max_id + 1 case.data = case.data.replace("*memberId*", str(memberId)) # 判断是否需要sql校验 if case.check_sql: case.check_sql = data_replace(case.check_sql) # 获取当前用户加标签的数量 start_count = self.db.find_count(case.check_sql) # 第二步:发送接口请求 res = self.http.request(case.method, url, data=eval(case.data)).json() print(res) res_code = res['code'] # 第三步:校验结果 try: self.assertEqual(str(case.excepted), res_code) if case.check_sql: case.check_sql = data_replace(case.check_sql) # 获取当前用户加标签的数量 end_count = self.db.find_count(case.check_sql) self.assertEqual(1, end_count - start_count) except AssertionError as e: # 用例执行未通过 self.excel.write_data(case.case_id + 1, 8, "fail") logger.info("测试数据为:{}".format(case.data)) logger.info("期望结果是:{}".format(case.excepted_code)) logger.info("实际结果是:{}".format(res)) logger.exception(e) raise e else: self.excel.write_data(case.case_id + 1, 8, "pass") logger.info("测试数据为:{}".format(case.data)) logger.info("期望结果是:{}".format(case.excepted)) logger.info("实际结果是:{}".format(res))
class TestLogin(unittest.TestCase): file_path = os.path.join(DATA_DIR, "case_data.xlsx") do_excel = ReadExcel(file_path, "login") cases = do_excel.read_data_obj() db = ReadMysql(my_conf.get('mysql', 'host'), my_conf.get('mysql', 'user'), my_conf.get('mysql', 'password'), my_conf.getint('mysql', 'port'), my_conf.get('mysql', 'database')) # 随机生成一个手机号码 def random_phone(self): phone = "13" # 数据库中查找该手机号是否存在 while True: for i in range(9): num = random.randint(1, 9) phone += str(num) sql = "select * from member where MobilePhone = {};".format(phone) if not self.db.find_one(sql): break return phone @classmethod def setUpClass(cls): logger.debug("-------开始登录接口测试{}-------") @classmethod def tearDownClass(cls): logger.debug("-------结束登录接口测试{}-------") @data(*cases) def test_login(self, case): # 第一步:准备测试数据和入参 excepted = case.excepted # url = case.url + case.interface url = my_conf.get('url', 'url') + case.url + case.interface case.data = data_replace(case.data) if "*phone*" in case.data: random_phone = self.random_phone() case.data = case.data.replace("*phone*", random_phone) request_data = eval(case.data) # 第二步:请求接口 res = HTTPRequest().request(case.method, url, request_data) # 第三步:预期对比 try: self.assertEqual(json.loads(excepted), res.json()) except AssertionError as e: self.do_excel.write_data(case.case_id + 1, 8, "Fail") logger.info("测试数据为:{}".format(request_data)) logger.info("期望结果是:{}".format(excepted)) logger.info("实际结果是:{}".format(res.json())) logger.exception(e) raise e else: self.do_excel.write_data(case.case_id + 1, 8, "Pass") logger.info("测试数据为:{}".format(request_data)) logger.info("期望结果是:{}".format(excepted)) logger.info("实际结果是:{}".format(res.json()))
class TestRecharge(unittest.TestCase): """充值接口""" data_file_path = os.path.join(DATA_DIR, "case_data.xlsx") excel = ReadExcel(data_file_path, 'recharge') cases = excel.read_data_obj() http = HTTPSession() db = ReadMysql(my_conf.get('mysql', 'host'), my_conf.get('mysql', 'user'), my_conf.get('mysql', 'password'), my_conf.getint('mysql', 'port'), my_conf.get('mysql', 'database')) @data(*cases) def test_recharge(self, case): # 第一步:入参和用例数据 url = case.url + case.interface # 获取充值之前的余额 if case.check_sql: start_money = self.db.find_one(case.check_sql)[0] print("充值之前用户的余额是:{}".format(start_money)) # 第二步:发送接口请求 res = self.http.request(case.method, url, data=eval(case.data)).json() # 第三步:校验结果 try: self.assertEqual(str(case.excepted_code), res["code"]) # 获取充值之后的余额 if case.check_sql: end_money = self.db.find_one(case.check_sql)[0] print("充值之后用户的余额是:{}".format(end_money)) self.assertEqual(float(eval(case.data)["amount"]), float(end_money-start_money)) except AssertionError as e: # 用例执行未通过 self.excel.write_data(case.case_id+1, 10, "fail") logger.info("测试数据为:{}".format(case.data)) logger.info("期望结果是:{}".format(case.excepted_code)) logger.info("实际结果是:{}".format(res)) logger.exception(e) raise e else: self.excel.write_data(case.case_id + 1, 10, "pass") logger.info("测试数据为:{}".format(case.data)) logger.info("期望结果是:{}".format(case.excepted_code)) logger.info("实际结果是:{}".format(res))
class TestAudit(unittest.TestCase): """加标接口""" data_file_path = os.path.join(DATA_DIR, "case_data.xlsx") excel = ReadExcel(data_file_path, 'audit') cases = excel.read_data_obj() http = HTTPSession() db = ReadMysql(my_conf.get('mysql', 'host'), my_conf.get('mysql', 'user'), my_conf.get('mysql', 'password'), my_conf.getint('mysql', 'port'), my_conf.get('mysql', 'database')) @classmethod def setUpClass(cls): logger.debug("-------开始审核接口测试{}-------") @classmethod def tearDownClass(cls): logger.debug("-------结束审核接口测试{}-------") @data(*cases) def test_audit(self, case): # 第一步:入参和用例数据 url = my_conf.get('url', 'url') + case.url + case.interface case.data = data_replace(case.data) # 判断是否需要sql校验 if case.check_sql: case.check_sql = data_replace(case.check_sql) # 获取当前审核状态 status = self.db.find_one(case.check_sql) # 第二步:发送接口请求 res = self.http.request(case.method, url, data=eval(case.data)).json() print(res) res_code = res['code'] # 判断是否是执行的加标用例 if case.interface == "audit": # 提取标id loan_id = self.db.find_one("select Id from loan where MemberId='{}' order by id desc limit 1".format(my_conf.getint('data','memberId'))) # 将添加的标id保存为临时变量 setattr(ConText, "loan_id", str(loan_id[0]))
class TestRegister(unittest.TestCase): file_path = os.path.join(DATA_DIR, "case_data.xlsx") do_excel = ReadExcel(file_path, "register") cases = do_excel.read_data_obj() db = ReadMysql(my_conf.get('mysql', 'host'), my_conf.get('mysql', 'user'), my_conf.get('mysql', 'password'), my_conf.getint('mysql', 'port'), my_conf.get('mysql', 'database')) @classmethod def setUpClass(cls): logger.debug("-------开始注册接口测试{}-------") @classmethod def tearDownClass(cls): cls.db.close() logger.debug("-------结束注册接口测试{}-------") @data(*cases) def test_register(self, case): # 第一步:准备测试数据和入参 excepted = case.excepted url = my_conf.get('url', 'url') + case.url + case.interface # 替换动态化参数 random_phone = self.random_phone() case.data = case.data.replace("*phone*", random_phone) request_data = eval(case.data) # 第二步:发送接口请求 res = HTTPRequest().request(case.method, url, request_data) # 第三步:比对结果 try: self.assertEqual(json.loads(excepted), res.json()) try: if case.check_sql: logger.debug("此条用例走了if语句") count = self.db.find_count( case.check_sql.replace("*phone*", random_phone)) self.assertEqual(1, count) logger.info("测试数据为:1") logger.info("期望结果是:{}".format(count)) else: logger.debug("此条用例没有走if语句") except AssertionError as e: logger.exception(e) raise e except AssertionError as e: self.do_excel.write_data(case.case_id + 1, 8, "Fail") logger.info("测试数据为:{}".format(request_data)) logger.info("期望结果是:{}".format(excepted)) logger.info("实际结果是:{}".format(res.json())) logger.exception(e) raise e else: self.do_excel.write_data(case.case_id + 1, 8, "Pass") logger.info("测试数据为:{}".format(request_data)) logger.info("期望结果是:{}".format(excepted)) logger.info("实际结果是:{}".format(res.json())) # 随机生成一个手机号码 def random_phone(self): phone = "13" # 数据库中查找该手机号是否存在 while True: for i in range(9): num = random.randint(1, 9) phone += str(num) sql = "select * from member where MobilePhone = {};".format(phone) if not self.db.find_one(sql): break return phone