1. 程式人生 > 其它 >python--裝飾器的種類和使用方法

python--裝飾器的種類和使用方法

 1 腳手架 fixture(前置後置/夾具)
 2         - 1. 經典的xunit風格
 3             - 函式級
 4                 - setup_function
 5                 - teardown_function
 6                 - 定義在模組中
 7             - 方法級(類中)
 8                 - self.setup_method
 9                 - self.teardown_method
10             - 類級
11
- 類方法 12 - setup_class(cls) 13 - teardown_class(cls) 14 - 模組級別 15 - setup_module 16 - teardown_module 17 - 2. unittest 18 - 方法級 19 self.setUp 前置 20 self.tearDown後置
21 - 類級 22 setUpClass 23 tearDownClass 24 - 3. @pytest.fixture 25 -定義 26 - 通過裝飾器@pytest.fixture可以定義夾具 27 - 使用 28 - 1. @pytest.mark.usefixtures(夾具的函式名字串) 29 - 2. 在測試函式中以夾具函式名作為引數
30 - 使用這個夾具 31 - 接受這個夾具返回值 32 - 3. 通過yield語句實現後置 33 - 作用範圍 @pytest.fixture(scope='function') 34 function 預設範圍,函式範圍,在測試完成後結束 35 class 在類中最後一個測試完成後結束 36 module 在模組中最後一個測試完成後結束 37 package 在包中的最後一個測試完成後結束 38 session 在一次會話中的最有一個測試完成後結束 39 - 自動執行 @pytest.fixture(autouse=True)