【大爆炸】case
#!/usr/bin/env python # -*- coding: utf-8 -*- # author:jiaoyaxiong time:2021/3/5 __author__ = 'jiaoyaxiong' import time import os import sys import random import uuid import nose from allure.constants import AttachmentType sys.path.append("../") import lib.base_case_lib_openapi from nose.tools import set_trace from aliyunsdkcore.profile import region_provider from aliyunsdkcore import client #处理中文ok reload(sys) sys.setdefaultencoding('utf-8') class test_sgw(lib.base_case_lib_openapi.common_lib): """ 测试目的:CreateGatewayLogging 线上 测试步骤: 使用已有网关创建日志监控 查询网关日志监控 Enabled Disable 查询网关日志监控 Disable enable 查询网关日志监控 Enabled 删除日志监控 查询日志监控状态 None """ def __init__(self): """ 我是初始化 """ file_name = os.path.split(os.path.abspath(__file__))[-1].replace(".py","") super(test_sgw,self).__init__(file_name) self.logger.info(file_name) region_provider.modify_point(self.cfg["global"]["Product_name"], self.cfg["global"]["RegionId_Hangzhou"], self.cfg["global"]["End_Point"]) self.gw_bundles_name = self.cfg["openapi"]["Storage_Bundle_name"] +str(time.strftime("%m%d%H%M%S", time.localtime()))+str(random.randint(1,100)) self.gw_bundles_region_id = self.cfg["openapi"]["RegionId_Hangzhou"] self.gw_bundles_des= "i am des" self.gw_id = self.cfg["openapi"]["gwLog_gwid"] # self.gw_id = "gw-0007vb109u7z49aw279h" self.slsproject = self.cfg["openapi"]["gwLog_slsproject"] self.slslogstore = self.cfg["openapi"]["gwLog_slslogstore"] def setup(self): """ 我是setup :return: """ try: self.do_DeleteGatewayLoggingRequest(GatewayId=self.gw_id) except: pass @nose.allure.severity("hard") #@nose.allure.label("label_key","label_value") @nose.allure.feature('gwLog') @nose.allure.story('Online') def test(self): # 根据需要增加一些环境变量 nose.allure.environment(product_name="sgw") result = self.get_gateway_version(bundleid="sb-0007va9bniddh5xfbh5v", gwid=self.gw_id) nose.allure.environment(sgw_version=str(result)) with nose.allure.step(u"创建网关日志监控 " ): self.logger.info("step::create Logging !") result = self.do_CreateGatewayLoggingRequest(GatewayId=self.gw_id,SlsProject=self.slsproject,SlsLogstore=self.slslogstore) self.logger.info("result is %s " % str(result)) if not result["Success"]: self.logger.error(" create logging fail !") assert False,"create logging fail" self.logger.info("api response:%s " % result["Response"]) with nose.allure.step(u"查询网关日志监控 " ): self.logger.info("step::search Logging") result = self.do_DescribeGatewayLoggingRequest(GatewayId=self.gw_id) if not result["Success"]: self.logger.error(" search logging fail !") assert False self.logger.info("api response:%s " % result["Response"]) if result["GatewayLoggingStatus"] != "Enabled": self.logger.error("status is not None") assert False with nose.allure.step(u"disable " ): self.logger.info("step::disable Logging") result = self.do_DisableGatewayLoggingRequest(GatewayId=self.gw_id) if not result["Success"]: self.logger.error(" disable logging fail !") assert False self.logger.info("api response:%s " % result["Response"]) with nose.allure.step(u"查询网关日志监控 " ): self.logger.info("step::search Logging") result = self.do_DescribeGatewayLoggingRequest(GatewayId=self.gw_id) if not result["Success"]: self.logger.error(" search logging fail !") assert False self.logger.info("api response:%s " % result["Response"]) if result["GatewayLoggingStatus"] != "Disabled": self.logger.error("status is not Disabled") assert False with nose.allure.step(u"enable "): self.logger.info("step::enable Logging") result = self.do_EnableGatewayLoggingRequest(GatewayId=self.gw_id) if not result["Success"]: self.logger.error(" enable logging fail !") assert False self.logger.info("api response:%s " % result["Response"]) with nose.allure.step(u"查询网关日志监控 "): self.logger.info("step::enable Logging") result = self.do_DescribeGatewayLoggingRequest(GatewayId=self.gw_id) if not result["Success"]: self.logger.error(" enable logging fail !") assert False self.logger.info("api response:%s " % result["Response"]) if result["GatewayLoggingStatus"] != "Enabled": self.logger.error("status is not Enabled") assert False with nose.allure.step(u"删除网关日志监控 " ): self.logger.info("step::delete Logging") result = self.do_DeleteGatewayLoggingRequest(GatewayId=self.gw_id) if not result["Success"]: self.logger.error(" delete logging fail !") assert False self.logger.info("api response:%s " % result["Response"]) with nose.allure.step(u"查询网关日志监控 " ): self.logger.info("step::search Logging") result = self.do_DescribeGatewayLoggingRequest(GatewayId=self.gw_id) if not result["Success"]: self.logger.error(" search logging fail !") assert False self.logger.info("api response:%s " % result["Response"]) if result["GatewayLoggingStatus"] != "None": self.logger.error("status is not None") assert False def teardown(self): """ 我是teardown :return: """ # 控制失败用例的行为 if sys.exc_info()[0]: with nose.allure.step(u'失败行为'): self.logger.info("Exception message" ) self.logger.info("teardown") with nose.allure.step(u'上传用例测试log'): nose.allure.attach("text log ", open(self.txt_path, "rb").read(), AttachmentType.TEXT) self.logger.info("pull log") """ 执行方式:nosetests -s -v --nologcapture --with-allure --logdir=/tmp/tmplog test*.py """ # 大爆炸 复习 继承 with 上下文管理器 # 继承类的构造方法: # # 1.经典类的写法: 父类名称.__init__(self,参数1,参数2,...) # # 2. 新式类的写法:super(子类,self).__init__(参数1,参数2,....) # 如果我们只是简单的在子类Chinese中定义一个构造函数,其实就是在重构。这样子类就不能继承父类的属性了。所以我们在定义子类的构造函数时,要先继承再构造,这样我们也能获取父类的属性了。 # 情况一:子类需要自动调用父类的方法:子类不重写__init__()方法,实例化子类后,会自动调用父类的__init__()的方法。 # # 情况二:子类不需要自动调用父类的方法:子类重写__init__()方法,实例化子类后,将不会自动调用父类的__init__()的方法。 # # 情况三:子类重写__init__()方法又需要调用父类的方法:使用super关键词: # 上下文管理器 https://www.cnblogs.com/wongbingming/p/10519553.html # 如何写上下文管理器? # # 要自己实现这样一个上下文管理,要先知道上下文管理协议。 # # 简单点说,就是在一个类里,实现了__enter__和__exit__的方法,这个类的实例就是一个上下文管理器。 # # 例如这个示例: # # CopyCopy # # # class Resource(): # def __enter__(self): # print('===connect to resource===') # return self # # def __exit__(self, exc_type, exc_val, exc_tb): # print('===close resource connection===') # # def operate(self): # print('===in operation===') # # # with Resource() as res: # res.operate() # 我们执行一下,通过日志的打印顺序。可以知道其执行过程。 # # CopyCopy # == =connect # to # resource == = # == = in operation == = # == =close # resource # connection == = # 从这个示例可以很明显的看出,在编写代码时,可以将资源的连接或者获取放在__enter__中,而将资源的关闭写在__exit__ # 中。 # 这就是上下文管理协议的一个强大之处,异常可以在__exit__ 进行捕获并由你自己决定如何处理,是抛出呢还是在这里就解决了。在__exit__ 里返回 True(没有return 就默认为 return False),就相当于告诉 Python解释器,这个异常我们已经捕获了,不需要再往外抛了。 # # 在 写__exit__ 函数时,需要注意的事,它必须要有这三个参数: # # exc_type:异常类型 # exc_val:异常值 # exc_tb:异常的错误栈信息 # 当主逻辑代码没有报异常时,这三个参数将都为None。 # 上下文管理器 https://www.cnblogs.com/wongbingming/p/10519553.html