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)