class Http_test(unittest.TestCase): register_data=Do_excel().read_excel("testdatas.xlsx","register") login_data=Do_excel().read_excel("testdatas.xlsx","login") recharge_data=Do_excel().read_excel("testdatas.xlsx","recharge") @data(*register_data) @log_decorator def test_1register(self,item): """用unpack时,测试用例的方法中接受的参数必须跟字典的键相同""" res=Http_request(item.url,item.method,params=item.test_data) actual=res.get_json()["msg"] item.actual=actual self.assertEqual(actual,item.expect["msg"]) @data(*login_data) @log_decorator def test_2login(self,item): res=Http_request(item.url,item.method,params=item.test_data) actual=res.get_json()["msg"] item.actual=actual self.assertEqual(actual,item.expect["msg"]) global cookies cookies=res.get_cookiese() @data(*recharge_data) @log_decorator def test_3recharge(self,item): res=Http_request(item.url,item.method,params=item.test_data,cookies=cookies) actual=res.get_json()["msg"] item.actual=actual self.assertEqual(actual,item.expect["msg"])
class Http_test(unittest.TestCase): register_data=Do_excel().read_excel("register") @data(*register_data) @log_decorator def test_1register(self,item): item.test_data=Replace_mobilephone().replace_mobile_info(item.test_data) #利用正则表达式处理数据 res=Http_request(item.url,item.method,params=item.test_data) actual=res.get_json()["code"] item.actual=actual self.assertEqual(actual,item.expect["code"])
def log_decorator(fun): '''日志装饰器类,穿入的参数fun是一个函数,是函数,而在方法new_name(*kwg)中的*kwg是传入函数fun本身的参数,在调用的 地方可以看到kwg相当于self(类的对象),和item,到具体的测试用例中查看这个参数 该装饰器主要作用是把每个测试用例类中方法要用到的共同方法封装,减少代码量。 在此处:1.实现根据不同测试 类和方法名建立日志文件名字 2.检查fun函数的参数,作为日志输出,方便问题定位 3.回写测试结果''' write_excel = Do_excel() def new_name(*kwg): """获取类名""" class_name = str(kwg[0].__class__) class_name = class_name.split("'")[1] funct_name = str(kwg[0]).split("=")[0].split(" ")[0] moudle_name = class_name + "_" + funct_name '''根据不同的测试类名字出给日志收集类,使得根据不同的测试类名新建不同的日志文件''' log = Record_logging(funct_name.split("_")[1], get_moudle_name=moudle_name[0:-2]) log.info('测试用例{}开始执行'.format(moudle_name)) try: begin_time = time.clock() fun(*kwg) Result = "Pass" except AssertionError as e: Result = "Fail" raise e finally: end_time = time.clock() '''日志输出请求信息''' log.info('你请求的URL地址是:{}'.format(kwg[1].url)) log.info('你的请求方式是:{}'.format(kwg[1].method)) log.info('你的请求参数是:{}'.format(kwg[1].test_data)) log.info('你的预期结果是:{}'.format(kwg[1].expect["code"])) log.info('请求的实际结果是:{}'.format(kwg[1].actual)) if Result == "Pass": log.info('测试用例开始执行的结果是:{}'.format(Result)) else: log.error('测试用例开始执行的结果是:{}'.format(Result)) '''回写测试结果''' write_excel.write_excel(kwg[1].sheet_name, kwg[1].id + 1, 7, kwg[1].actual) write_excel.write_excel(kwg[1].sheet_name, kwg[1].id + 1, 8, Result) log.info('测试用例{}结束执行,一共执行{}'.format(moudle_name, end_time - begin_time)) return new_name
class Http_test(unittest.TestCase): bidloan_data = Do_excel().read_excel("bidloan") def setUp(self): amount = Do_excel() amount_sql = "select LeaveAmount from member where Id='23499'" @data(*bidloan_data) @log_decorator def test_5bidloan(self, item): item.test_data = Replace_mobilephone().replace_mobile_info( item.test_data) #利用正则表达式处理数据 if hasattr(Context, "cookies"): if item.expect["msg"] == "竞标成功": """ 竞标成功则去数据库判断用户金额是否跟投资金额匹配""" memberId = str(item.test_data["memberId"]) begin_amount = (Connect_db().get_one( "select LeaveAmount from member where Id='" + memberId + "'"))[0] cookies = getattr(Context, "cookies") res = Http_request(item.url, item.method, params=item.test_data, cookies=cookies) end_amount = (Connect_db().get_one( "select LeaveAmount from member where Id='" + memberId + "'"))[0] amount = item.test_data["amount"] self.assertEqual(begin_amount - amount, end_amount) else: cookies = getattr(Context, "cookies") res = Http_request(item.url, item.method, params=item.test_data, cookies=cookies) else: res = Http_request(item.url, item.method, params=item.test_data) setattr(Context, "cookies", res.get_cookiese()) actual = res.get_json()["code"] item.actual = actual self.assertEqual(actual, item.expect["code"])
def log_decorator(fun): write_excel = Do_excel() def new_name(*kwg): begin_time = time.clock() end_time = time.clock() """获取类名""" class_name = str(kwg[0].__class__) class_name = class_name.split("'")[1] funct_name = str(kwg[0]).split("=")[0].split(" ")[0] moudle_name = class_name + "_" + funct_name log = Record_logging(funct_name.split("_")[1], get_moudle_name=moudle_name[0:-2]) log.info('测试用例{}开始执行'.format(moudle_name)) try: fun(*kwg) Result = "Pass" except AssertionError as e: Result = "Fail" raise e finally: log.info('你请求的URL地址是:{}'.format(kwg[1].url)) log.info('你的请求方式是:{}'.format(kwg[1].method)) log.info('你的请求参数是:{}'.format(kwg[1].test_data)) log.info('你的预期结果是:{}'.format(kwg[1].expect["msg"])) log.info('请求的实际结果是:{}'.format(kwg[1].actual)) if Result == "Pass": log.info('测试用例开始执行的结果是:{}'.format(Result)) phone = kwg[1].test_data else: log.error('测试用例开始执行的结果是:{}'.format(Result)) write_excel.write_excel(kwg[1].book_name, kwg[1].sheet_name, kwg[1].id + 1, 7, kwg[1].actual) write_excel.write_excel(kwg[1].book_name, kwg[1].sheet_name, kwg[1].id + 1, 8, Result) log.info('测试用例{}结束执行,一共执行{}'.format(moudle_name, end_time - begin_time)) return new_name
class Http_test(unittest.TestCase): recharge_data = Do_excel().read_excel("recharge") # money_sql="select leaveAmont from member where mobilephone=''" @data(*recharge_data) @log_decorator def test_3recharge(self, item): item.test_data = Replace_mobilephone().replace_mobile_info( item.test_data) #利用正则表达式处理数据 if hasattr(Context, "cookies"): cookies = getattr(Context, "cookies") res = Http_request(item.url, item.method, params=item.test_data, cookies=cookies) else: res = Http_request(item.url, item.method, params=item.test_data) setattr(Context, "cookies", res.get_cookiese()) actual = res.get_json()["code"] item.actual = actual self.assertEqual(actual, item.expect["code"])
def setUp(self): amount = Do_excel() amount_sql = "select LeaveAmount from member where Id='23499'"