python 上下文管理器with
with 上下文管理器
- 上下文管理器
- 示例如下:
- 若想不使用with关键字
上下文管理器
任何实现了 enter() 和 exit() 方法的对象都可称之为上下文管理器,上下文管理器对象可以使用 with 关键字。
- 必须同时具有
__enter__
和__exit__
,就可以使用with语句 __enter__
:表示开始__exit__
:表示结束
示例如下:
with allure.step(f'step作用是为allure设置流程顺序'):
pass
若想不使用with关键字
- 需要将
__enter__
和__exit__
方法中的代码复制出来,用作开始和结束
plugin_manager.hook.start_step(uuid=self.uuid, title='step作用是为allure设置流程顺序', params=self.params)
plugin_manager.hook.stop_step(uuid=self.uuid, title='step作用是为allure设置流程顺序', exc_type=exc_type, exc_val=exc_val, exc_tb=exc_tb)