class AuditTestCase(unittest.TestCase): """加标接口""" excel = ReadExcel(data_file_path, 'audit') cases = excel.read_data_obj() http = HTTPSession() db = ReadSQL() @data(*cases) def test_audit(self, case): # 第一步:准备用例数据 # 拼接接口路径 case.url = myconf.get('url', 'url') + case.url # 替换用例参数 case.data = data_replace(case.data) # 判断是否有*load_id* 的参数要替换 if "*loan_id*" in case.data: max_id = self.db.find_one("SELECT max(id) FROM loan")[0] loan_id = max_id + 1 case.data = case.data.replace("*loan_id*", str(loan_id)) # 第二步 发送请求,获取结果 response = self.http.request(method=case.method, url=case.url, data=eval(case.data)) res = response.json() res_code = res['code'] # 判断是否是执行的加标用例, if case.interface == "加标": loan_id = self.db.find_one( "SELECT Id FROM loan WHERE MemberId='{}' ORDER BY id DESC". format(myconf.get('data', 'memberId'))) # 将添加的标id,保存为临时变量 setattr(ConText, 'loan_id', loan_id[0])
class AuditTestCase(unittest.TestCase): """审核接口""" excel = ReadExcel(os.path.join(DATA_DIR, "cases1.xlsx"), "audit") cases = excel.read_data_obj() http = HTTPSession() #类属性 db = ReadSQL() #创建db对象 @data(*cases) def test_case_audit(self, case): """审核接口用例执行的逻辑""" #准备测试用例 url = myconf.get('url', 'url') + case.url # 拼接完整的URL地址 row = case.case_id + 1 # 替换用例参数 case.data = replace(case.data) # 判断是否有*memberId*的参数需要替换 if "*loan_id*" in case.data: max_id = self.db.find_one("select max(id) from loan")[ 0] # findone()返回的是元组,要加下标才能获取到元素 loan_id = max_id + 1 # 在数据库中查找到最大的id号,加1后替换给*memberId* case.data = case.data.replace("*loan_id*", str(loan_id)) #发送请求到接口,获取结果 log.info("正在请求地址{}".format(url)) response = self.http.request(method=case.method, url=url, data=eval(case.data)) res = response.json() print(res) #判断是否是执行的加标用例 if case.interface == "加标": loan_id = self.db.find_one( "select id from loan where memberId ='{}' order by id desc". format(myconf.get("data", "memberId")))[0] #"SELECT Id FROM loan WHERE MemberId='{}' ORDER BY id DESC".format(myconf.get('data', 'memberId'))) # 将添加的标id,保存为临时变量 setattr(ConText, 'loan_id', loan_id) #对象(类)属性名 属性值 #对比预期结果 try: self.assertEqual(str(case.excepted_code), res['code']) # 判断是否需要进行SQL校验 if case.check_sql: case.check_sql = replace(case.check_sql) #要记的把参数化的sql语句替换为实际的值 #获取返回的状态码 status = self.db.find_one( case.check_sql)[0] #查询语句默认返回的是元组,要加下标 self.assertEqual(eval(case.data)["status"], status) except AssertionError as e: """测试用例未通过""" self.excel.write_data(row, 8, '未通过') log.info("{},该条用例执行未通过".format(case.title)) log.exception(e) raise e else: self.excel.write_data(row, 8, '通过') log.info("{},该条用例执行通过".format(case.title))
def test_case_recharge(self, case): """充值接口用例执行的逻辑""" #准备测试用例数据 url = myconf.get('url', "url") + case.url # 拼接完整的URL地址 row = case.case_id + 1 #替换用例参数 case.data = replace(case.data) if case.check_sql: case.check_sql = replace(case.check_sql) start_money = ReadSQL().find_one(case.check_sql)[0] print('充值之前用户的余额为{}'.format(start_money))
class WithdrawTestCase(unittest.TestCase): """取现接口""" excel = ReadExcel(os.path.join(DATA_DIR, "cases1.xlsx"), "withdraw") cases = excel.read_data_obj() http = HTTPSession() #类属性 db = ReadSQL() #创建db对象 @data(*cases) def test_case_withdraw(self, case): """取现接口用例执行的逻辑""" #准备测试用例数据 url = myconf.get('url', "url") + case.url # 拼接完整的URL地址 data = eval(case.data) method = case.method row = case.case_id + 1 # 替换用例参数 case.data = replace(case.data) if case.check_sql: case.check_sql = replace(case.check_sql) start_money = ReadSQL().find_one(case.check_sql)[0] print('取现之前用户的余额为{}'.format(start_money)) #发送请求到接口,获取结果 log.info("正在请求地址{}".format(url)) response = self.http.request(method=method, url=url, data=eval(case.data)) res = response.json() #对比预期结果 try: # 判断是否需要进行SQL校验 if case.check_sql: # 如果此字段有数据,条件成立 # 获取取现用例执行之后的余额 end_money = self.db.find_one(case.check_sql)[0] print('取现之后用户的余额为{}'.format(end_money)) # 获取本次充值的金额 money = eval(case.data)['amount'] money = decimal.Decimal(str(money)) print('本次取现的金额{}'.format(money)) # 获取数据库变化的金额 change_money = start_money - end_money self.assertEqual(money, change_money) self.assertEqual(str(case.excepted_code), res["code"]) except AssertionError as e: """测试用例未通过""" self.excel.write_data(row, 8, '未通过') log.info("{},该条用例执行未通过".format(case.title)) log.exception(e) raise e else: self.excel.write_data(row, 8, '通过') log.info("{},该条用例执行通过".format(case.title))
class AddTestCase(unittest.TestCase): """加标接口""" excel = ReadExcel(os.path.join(DATA_DIR, "cases1.xlsx"), "add") cases = excel.read_data_obj() http = HTTPSession() #类属性 db = ReadSQL() #创建db对象 @data(*cases) def test_case_add(self, case): """加标接口用例执行的逻辑""" #准备测试用例 url = myconf.get('url', 'url') + case.url # 拼接完整的URL地址 row = case.case_id + 1 # 替换用例参数 case.data = replace(case.data) #判断是否有*memberId*的参数需要替换 if "*memberId*" in case.data: max_id = self.db.find_one("select max(id) from member")[ 0] #findone()返回的是元组,要加下标才能获取到元素 memberid = max_id + 1 #在数据库中查找到最大的id号,加1后替换给*memberId* case.data = case.data.replace( "*memberId*", str(memberid)) #字符串替换的方法,要记得转换类型为str,否则会报错 #判断是否需要SQL校验 if case.check_sql: #如果case文件里的check_sql有数据,就做替换。 case.check_sql = replace( case.check_sql) #调用封装好的替换类,把其中含有“#memberId#"的替换为从配置文件中获取的值 # 获取当前用户加标前的标数量 start_count = self.db.find_count(case.check_sql) #发送请求到接口,获取结果 log.info("正在请求地址{}".format(url)) response = self.http.request(method=case.method, url=url, data=eval(case.data)) res = response.json() #对比预期结果 try: self.assertEqual(str(case.excepted_code), res["code"]) # 判断是否需要进行SQL校验 if 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(row, 8, '未通过') log.info("{},该条用例执行未通过".format(case.title)) log.exception(e) raise e else: self.excel.write_data(row, 8, '通过') log.info("{},该条用例执行通过".format(case.title))
def test_case_withdraw(self, case): """取现接口用例执行的逻辑""" #准备测试用例数据 url = myconf.get('url', "url") + case.url # 拼接完整的URL地址 data = eval(case.data) method = case.method row = case.case_id + 1 # 替换用例参数 case.data = replace(case.data) if case.check_sql: case.check_sql = replace(case.check_sql) start_money = ReadSQL().find_one(case.check_sql)[0] print('取现之前用户的余额为{}'.format(start_money))
class RegisterTestCase(unittest.TestCase): """注册接口""" excel = ReadExcel(os.path.join(DATA_DIR, "cases1.xlsx"), "register") cases = excel.read_data_obj() http = HTTPRequest() #类属性 db = ReadSQL() #创建db对象 @data(*cases) def test_case_register(self, case): """登注册接口用例执行的逻辑""" #准备测试用例 url = myconf.get('url', 'url') + case.url # 拼接完整的URL地址 method = case.method excepted = eval(case.excepted) row = case.case_id + 1 # 替换用例参数 case.data = replace(case.data) #随机生成手机号码 phone = self.random_phone() print(phone) #替换动态化的参数,字符串替换方法 case.data = case.data.replace("*phone*", phone) #加星号是为了避免替换掉其他的同名字符,如mobilephone #发送请求到接口,获取结果 log.info("正在请求地址{}".format(url)) response = self.http.request(method=method, url=url, data=eval(case.data)) res = response.json() #对比预期结果 try: self.assertEqual(excepted, res) # 判断是否需要进行SQL校验 if case.check_sql: # 如果此字段有数据,条件成立 # 用随机生成的手机号替换需要校验的SQL语句中的手机号 case.check_sql = case.check_sql.replace('*phone*', phone) db_res = self.db.find_count(case.check_sql) self.assertEqual(1, db_res) except AssertionError as e: """测试用例未通过""" self.excel.write_data(row, 8, '未通过') log.info("{},该条用例执行未通过".format(case.title)) log.exception(e) raise e else: self.excel.write_data(row, 8, '通过') log.info("{},该条用例执行通过".format(case.title)) def random_phone(self): """随机生成手机号""" while True: phone = "13" 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_count(sql): return phone