Pytest官方教程-17-經(jīng)典xUnit風(fēng)格的setup/teardown

目錄:

  1. 安裝及入門
  2. 使用和調(diào)用方法
  3. 原有TestSuite使用方法
  4. 斷言的編寫和報(bào)告
  5. Pytest fixtures:清晰 模塊化 易擴(kuò)展
  6. 使用Marks標(biāo)記測試用例
  7. Monkeypatching/對(duì)模塊和環(huán)境進(jìn)行Mock
  8. 使用tmp目錄和文件
  9. 捕獲stdout及stderr輸出
  10. 捕獲警告信息
  11. 模塊及測試文件中集成doctest測試
  12. skip及xfail: 處理不能成功的測試用例
  13. Fixture方法及測試用例的參數(shù)化
  14. 緩存: 使用跨執(zhí)行狀態(tài)
  15. unittest.TestCase支持
  16. 運(yùn)行Nose用例
  17. 經(jīng)典xUnit風(fēng)格的setup/teardown
  18. 安裝和使用插件
  19. 插件編寫
  20. 編寫鉤子(hook)方法
  21. 運(yùn)行日志
  22. API參考
    1. 方法(Functions)
    2. 標(biāo)記(Marks)
    3. 鉤子(Hooks)
    4. 裝置(Fixtures)
    5. 對(duì)象(Objects)
    6. 特殊變量(Special Variables)
    7. 環(huán)境變量(Environment Variables)
    8. 配置選項(xiàng)(Configuration Options)
  23. 優(yōu)質(zhì)集成實(shí)踐
  24. 片狀測試
  25. Pytest導(dǎo)入機(jī)制及sys.path/PYTHONPATH
  26. 配置選項(xiàng)
  27. 示例及自定義技巧
  28. Bash自動(dòng)補(bǔ)全設(shè)置

經(jīng)典xUnit風(fēng)格的setup/teardown

本節(jié)介紹了如何在每個(gè)模塊/類/功能的基礎(chǔ)上實(shí)現(xiàn)Fixture(setup和teardown測試狀態(tài))的經(jīng)典而流行的方法。

注意

雖然這些setup/teardown方法對(duì)于來自a unittest或nose的人來說簡單且熟悉,但background你也可以考慮使用pytest更強(qiáng)大的Fixture機(jī)制,該機(jī)制利用依賴注入的概念,允許更模塊化和更可擴(kuò)展的方法來管理測試狀態(tài),特別是對(duì)于大型項(xiàng)目和功能測試。你可以在同一文件中混合兩種Fixture機(jī)制,但unittest.TestCase子類的測試方法不能接收Fixture參數(shù)。

模塊級(jí)別setup/teardown

如果在單個(gè)模塊中有多個(gè)測試函數(shù)和測試類,則可以選擇實(shí)現(xiàn)以下fixture方法,這些方法通常會(huì)針對(duì)所有函數(shù)調(diào)用一次:

def setup_module(module):
    """ setup any state specific to the execution of the given module."""

def teardown_module(module):
    """ teardown any state that was previously setup with a setup_module
 method.
 """

從pytest-3.0開始,module參數(shù)是可選的。

班級(jí)setup/拆解

類似地,在調(diào)用類的所有測試方法之前和之后,在類級(jí)別調(diào)用以下方法:

@classmethod
def setup_class(cls):
    """ setup any state specific to the execution of the given class (which
 usually contains tests).
 """

@classmethod
def teardown_class(cls):
    """ teardown any state that was previously setup with a call to
 setup_class.
 """

方法和功能級(jí)別setup/teardown

同樣,圍繞每個(gè)方法調(diào)用調(diào)用以下方法:

def setup_method(self, method):
    """ setup any state tied to the execution of the given method in a
 class.  setup_method is invoked for every test method of a class.
 """

def teardown_method(self, method):
    """ teardown any state that was previously setup with a setup_method
 call.
 """

從pytest-3.0開始,method參數(shù)是可選的。

如果你希望直接在模塊級(jí)別定義測試函數(shù),還可以使用以下函數(shù)來實(shí)現(xiàn)fixture:

def setup_function(function):
    """ setup any state tied to the execution of the given function.
 Invoked for every test function in the module.
 """

def teardown_function(function):
    """ teardown any state that was previously setup with a setup_function
 call.
 """

從pytest-3.0開始,function參數(shù)是可選的。

備注:

  • 每個(gè)測試過程可以多次調(diào)用setup / teardown對(duì)。

  • 如果存在相應(yīng)的setup功能并且跳過了失敗/,則不會(huì)調(diào)用teardown功能。

  • 在pytest-4.2之前,xunit樣式的函數(shù)不遵守fixture的范圍規(guī)則,因此例如setup_method可以在會(huì)話范圍的autouse fixture之前調(diào)用a。

    現(xiàn)在,xunit風(fēng)格的功能與Fixture機(jī)制集成在一起,并遵守調(diào)用中涉及的燈具的適當(dāng)范圍規(guī)則。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容