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 QueryPointResultTestCase(unittest.TestCase): """try积分接口""" excel = ReadExcel(data_file_path, 'queryPointResult') cases = excel.read_data_obj() http = HTTPRequest() db = ReadSQL() @data(*cases) def test_case_pointpark(self, case): # 准备测试用例数据 url = case.url # data = eval(case.data) method = case.method excepted = eval(case.excepted) row = case.case_id + 1 headers = eval(case.headers) # 处理case用例里需要替换的uid参数 if "#uid#" in case.data: case.data = data_replace(case.data) # 处理case用例里需要替换的serialNo参数 serialNo = self.random_serialNo() if "*serialNo*" in case.data: case.data = case.data.replace("*serialNo*", serialNo)
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()))
class AdvertiseConfigs(unittest.TestCase): """根据广告配置code获取广告配置""" excel = ReadExcel(data_file_path, 'advertiseConfigs') cases = excel.read_data_obj() http = HTTPRequest() db = ReadSQL() @data(*cases) def test_advertiseConfigs(self, case): # 准备测试用例数据 url = case.url # excepted = case.excepted method = case.method row = case.case_id + 1 headers = eval(case.headers) # 发送请求到接口,获取结果 log.info('正在请求地址{}'.format(url)) response = self.http.request(method=method, url=url, headers=headers) # 获取返回的内容,该接口无返回参数,只需获取状态码即可 res = response.status_code # 比对预期结果和实际结果,断言用例是否通过 try: self.assertEqual(200, res) if case.check_sql: db_res = self.db.find_count(case.check_sql) #如果数据库检索出条数,就与1比对 if db_res == 1: self.assertEqual(1, db_res) # 如果数据库检索不出条数,就与0比对 else: self.assertEqual(0, db_res) print('实际该接口查询的sql为:{}'.format(case.check_sql)) print('实际该接口执行sql查询出的数据条数为:{}'.format(db_res)) print('实际该接口请求实际返回的状态码为:{}'.format(res)) except AssertionError as e: # 测试用例未通过 # 获取当前用例所在行 self.excel.write_data(row=row, column=9, value='未通过') log.debug('{},该条用例执行未通过'.format(case.title)) raise e else: # 测试用例执行通过 self.excel.write_data(row=row, column=9, value='通过') log.debug('{},该条用例执行通过'.format(case.title))
class TishiyuTestCase(unittest.TestCase): """快递接口""" excel = ReadExcel(data_file_path, 'tishiyu') cases = excel.read_data_obj() http = HTTPRequest() @data(*cases) def test_case_kuaidi(self, case): # 登录接口用例执行的逻辑 # 第一步:准备测试用例数据 url = case.url data = case.data method = case.method excepted = eval(case.excepted) row = case.case_id + 1 headers = {'Content-Type': 'application/x-www-form-urlencoded'} # 第二步:发送请求到接口,获取结果 log.info('正在请求地址{}'.format(url)) response = self.http.request(method=method, url=url, data=data, headers=headers) # 获取返回的内容 res = json.loads(response.text) # 第三步:比对预期结果和实际结果,断言用例是否通过 try: # self.assertEqual(excepted['EBusinessID'],res['EBusinessID']) # self.assertEqual(excepted['ShipperCode'],res['ShipperCode']) # self.assertEqual(excepted['State'],res['State']) self.assertEqual(excepted['Success'], res['Success']) self.assertEqual(excepted['Reason'], res['Reason']) except AssertionError as e: # 测试用例未通过 # 获取当前用例所在行 self.excel.write_data(row=row, column=8, value='未通过') log.debug('{},该条用例执行未通过'.format(case.title)) raise e else: # 测试用例执行通过 self.excel.write_data(row=row, column=8, value='通过') log.debug('{},该条用例执行通过'.format(case.title))
class GetAdvertisesPageId(unittest.TestCase): """通过pageId获取保底广告""" excel = ReadExcel(data_file_path, 'getAdvertises-pageId') cases = excel.read_data_obj() http = HTTPRequest() db = ReadSQL() @data(*cases) def test_getAdvertisespageId(self, case): # 准备测试用例数据 url = case.url # excepted = case.excepted method = case.method row = case.case_id + 1 headers = eval(case.headers) # sql = case.check_sql # 发送请求到接口,获取结果 log.info('正在请求地址{}'.format(url)) response = self.http.request(method=method, url=url, headers=headers) # 获取返回的内容,该接口无返回参数,只需获取状态码即可 res = response.status_code # 获取返回的内容 response = response.json() # 比对预期结果和实际结果,断言用例是否通过 try: self.assertEqual(200, res) # self.assertEqual(excepted,response) print('实际该接口请求实际返回的状态码为:{}'.format(res)) except AssertionError as e: # 测试用例未通过 # 获取当前用例所在行 self.excel.write_data(row=row, column=9, value='未通过') log.debug('{},该条用例执行未通过'.format(case.title)) raise e else: # 测试用例执行通过 self.excel.write_data(row=row, column=9, value='通过') log.debug('{},该条用例执行通过'.format(case.title))
class StraightSendPrizeTestCase(unittest.TestCase): """try积分接口""" excel = ReadExcel(data_file_path, 'straightSendPrize') cases = excel.read_data_obj() http = HTTPRequest() def random_serialNo(self): '''随机生成serialNo''' while True: serialNo = "13" for i in range(9): num = random.randint(1, 9) serialNo += str(num) # 数据库查询serialNo是否存在 sql = "SELECT * FROM pointpark.point_activity_info where serialNo='{}'".format( serialNo) if not self.db.find_count(sql): return serialNo @data(*cases) def test_case_pointpark(self, case): # 准备测试用例数据 url = case.url # data = eval(case.data) method = case.method excepted = eval(case.excepted) row = case.case_id + 1 headers = eval(case.headers) # 处理case用例里需要替换的uid参数 if "#uid#" in case.data: case.data = data_replace(case.data) # 处理case用例里需要替换的serialNo参数 serialNo = self.random_serialNo() if "*serialNo*" in case.data: case.data = case.data.replace("*serialNo*", serialNo)
class LoginTestCase(unittest.TestCase): """登录接口""" excel=ReadExcel(os.path.join(DATA_DIR,"cases1.xlsx"),"login") cases=excel.read_data_obj() http=HTTPRequest() #类属性 @data(*cases) def test_case_login(self,case): """登录接口用例执行的逻辑""" #准备测试用例 url=myconf.get('url',"url")+case.url#字符串连接用+ #url=case.url #data=eval(case.data) method=case.method excepted=eval(case.excepted) row=case.case_id + 1 # 替换用例参数 # if "#phone#" in case.data: #手机号和密码都要替换,这样比较繁琐,封装正则类来替换 # case.data.replace("#phone#",myconf.get("data","phone")) data=replace(case.data) #发送请求到接口,获取结果 log.info("正在请求地址{}".format(url)) response=self.http.request(method=method,url=url,data=eval(data)) res=response.json() #对比预期结果 #self.assertEqual(excepted,res) try: self.assertEqual(excepted,res) except AssertionError as e: """测试用例未通过""" self.excel.write_data(row, 8, '未通过') log.debug("{},该条用例执行未通过".format(case.title)) log.error(e) raise e else: self.excel.write_data(row, 8, '通过') log.debug("{},该条用例执行通过".format(case.title))
class LoginTestCase(unittest.TestCase): """登录接口""" excel = ReadExcel( r'C:\project\python21\class21_api_project\data\cases.xlsx', 'login') cases = excel.read_data_obj() http = HTTPRequest() @data(*cases) def test_case_login(self, case): # 登录接口用例执行的逻辑 # 第一步:准备测试用例数据 url = case.url data = eval(case.data) method = case.method excepted = eval(case.excepted) row = case.case_id + 1 # 第二步:发送请求到接口,获取结果 log.info('正在请求地址{}'.format(url)) response = self.http.request(method=method, url=url, data=data) # 获取返回的内容 res = response.json() # 第三步:比对预期结果和实际结果,断言用例是否通过 try: self.assertEqual(excepted, res) except AssertionError as e: # 测试用例未通过 # 获取当前用例所在行 self.excel.write_data(row=row, column=8, value='未通过') log.debug('{},该条用例执行未通过'.format(case.title)) raise e else: # 测试用例执行通过 self.excel.write_data(row=row, column=8, value='通过') log.debug('{},该条用例执行通过'.format(case.title))
def setUpClass(cls): cls.request = HTTPRequest() cls.db = readmysql()
class ConsumePointTestCase(unittest.TestCase): """try积分接口""" excel = ReadExcel(data_file_path, 'consumePoint') cases = excel.read_data_obj() http = HTTPRequest() db = ReadSQL() @data(*cases) def test_case_pointpark(self, case): # 准备测试用例数据 url = case.url # data = eval(case.data) method = case.method excepted = eval(case.excepted) row = case.case_id + 1 headers = eval(case.headers) # 处理case用例里需要替换的uid参数 # if '#uid#' in case.data: # case.data = data_replace(case.data) # 处理case用例里需要替换的serialNo参数 serialNo = self.random_serialNo() if '*serialNo*' in case.data: case.data = case.data.replace('*serialNo*', serialNo) return case.data # if 'point' in case.data: # sql = "SELECT * FROM pointpark.point_activity_info where serialNo='{}'".format(serialNo)[8] # if self.find_count(sql): # excepted = case.data.replace('*point*',sql) # return excepted # 发送请求到接口,获取结果 log.info('正在请求地址{}'.format(url)) response = self.http.request(method=method, url=url, data=eval(case.data), headers=headers) # 获取返回的内容 res = response.json() # 比对预期结果和实际结果,断言用例是否通过 try: self.assertEqual(excepted, res) if case.check_sql: db_res = self.db.find_count(case.check_sql) self.assertEqual('1', db_res) print('实际接口请求结果为:{}'.format(res)) print('预期接口请求结果为:{}'.format(excepted)) except AssertionError as e: # 测试用例未通过 # 获取当前用例所在行 self.excel.write_data(row=row, column=9, value='未通过') log.debug('{},该条用例执行未通过'.format(case.title)) raise e else: # 测试用例执行通过 self.excel.write_data(row=row, column=9, value='通过') log.debug('{},该条用例执行通过'.format(case.title))
def setUpClass(cls): logger.info("用例开始执行") cls.request = HTTPRequest() cls.db = readmysql()
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