Pytest官方教程-15-unittest.TestCase支持

目錄:

unittest.TestCase支持

pytest支持unittest開箱即用的基于Python 的測試。它旨在利用現有unittest的測試套件將pytest用作測試運行器,并允許逐步調整測試套件以充分利用pytest的功能。

要使用運行現有unittest樣式的測試套件pytest,請鍵入:

pytest tests

pytest將自動收集unittest.TestCase子類及其test方法test_*.py*_test.py文件。

幾乎所有unittest功能都受支持:

  • @unittest.skip 風格裝飾;
  • setUp/tearDown;
  • setUpClass/tearDownClass;
  • setUpModule/tearDownModule;

到目前為止,pytest不支持以下功能:

開箱即用的好處

通過使用pytest運行測試套件,你可以使用多種功能,在大多數情況下無需修改現有代碼:

unittest.TestCase子類中的pytest特性

以下pytest功能適用于unittest.TestCase子類:

下面pytest功能工作,也許永遠也因不同的設計理念:

第三方插件可能運行也可能不運行,具體取決于插件和測試套件。

unittest.TestCase使用標記將pytest燈具混合到子類中

運行unittest pytest允許你使用其 夾具機制進行unittest.TestCase樣式測試。假設你至少瀏覽了pytest fixture功能,讓我們跳轉到一個集成pytest db_class fixture,設置類緩存數據庫對象,然后從unittest樣式測試中引用它的示例:

# content of conftest.py

# we define a fixture function below and it will be "used" by
# referencing its name from tests

import pytest

@pytest.fixture(scope="class")
def db_class(request):
    class DummyDB(object):
        pass
    # set a class attribute on the invoking test context
    request.cls.db = DummyDB()

這定義了一個fixture函數db_class- 如果使用的話 - 為每個測試類調用一次,并將class-level db屬性設置為一個DummyDB實例。fixture函數通過接收一個特殊request對象來實現這一點,該對象允許訪問請求測試上下文,例如cls屬性,表示使用該fixture的類。該架構將夾具寫入與實際測試代碼分離,并允許通過最小參考(夾具名稱)重新使用夾具。那么讓unittest.TestCase我們使用fixture定義編寫一個實際的類:

# content of test_unittest_db.py

import unittest
import pytest

@pytest.mark.usefixtures("db_class")
class MyTest(unittest.TestCase):
    def test_method1(self):
        assert hasattr(self, "db")
        assert 0, self.db   # fail for demo purposes

    def test_method2(self):
        assert 0, self.db   # fail for demo purposes

@pytest.mark.usefixtures("db_class")類的裝飾可確保pytest固定函數db_class被調用每一次班。由于故意失敗的斷言語句,我們可以看看self.db回溯中的值:

$ pytest test_unittest_db.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 2 items

test_unittest_db.py FF                                               [100%]

================================= FAILURES =================================
___________________________ MyTest.test_method1 ____________________________

self = <test_unittest_db.MyTest testMethod=test_method1>

 def test_method1(self):
 assert hasattr(self, "db")
>       assert 0, self.db   # fail for demo purposes
E       AssertionError: <conftest.db_class.<locals>.DummyDB object at 0xdeadbeef>
E       assert 0

test_unittest_db.py:9: AssertionError
___________________________ MyTest.test_method2 ____________________________

self = <test_unittest_db.MyTest testMethod=test_method2>

 def test_method2(self):
>       assert 0, self.db   # fail for demo purposes
E       AssertionError: <conftest.db_class.<locals>.DummyDB object at 0xdeadbeef>
E       assert 0

test_unittest_db.py:12: AssertionError
========================= 2 failed in 0.12 seconds =========================

這個默認的pytest回溯顯示兩個測試方法共享同一個self.db實例,這是我們在編寫上面的類范圍的fixture函數時的意圖。

使用autouse燈具和訪問其他燈具

雖然通常更好地明確聲明對給定測試需要使用的燈具,但有時你可能想要在給定的上下文中自動使用燈具。畢竟,傳統的unittest-setup風格要求使用這種隱含的夾具編寫,而且很有可能,你習慣它或者喜歡它。

你可以使用標記夾具功能@pytest.fixture(autouse=True) 并在要使用它的上下文中定義夾具功能。讓我們看一個initdir夾具,它使一個TestCase類的所有測試方法都 在一個預先初始化的臨時目錄中執行samplefile.ini。我們的initdirfixture本身使用pytest builtin tmpdir fixture來委托創建一個per-test臨時目錄:

# content of test_unittest_cleandir.py
import pytest
import unittest

class MyTest(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def initdir(self, tmpdir):
        tmpdir.chdir() # change to pytest-provided temporary directory
        tmpdir.join("samplefile.ini").write("# testdata")

    def test_method(self):
        with open("samplefile.ini") as f:
            s = f.read()
        assert "testdata" in s

由于該autouse標志,initdirfixture函數將用于定義它的類的所有方法。這是@pytest.mark.usefixtures("initdir")在類中使用標記的快捷方式,如上例所示。

運行此測試模塊......:

$ pytest -q test_unittest_cleandir.py
.                                                                    [100%]
1 passed in 0.12 seconds

...給我們一個通過測試,因為initdir夾具功能在之前執行test_method

注意

unittest.TestCase 方法不能直接接收fixture參數作為實現可能會導致運行通用unittest.TestCase測試套件的能力。

以上usefixturesautouse示例應該有助于將pytest燈具混合到unittest套件中。

你也可以逐步從子類化轉移unittest.TestCase普通斷言 ,然后開始逐步從完整的pytest功能集中受益。

注意

unittest.TestCase子類運行測試--pdb將禁用針對發生異常的情況的tearDown和cleanup方法。這允許對所有在其tearDown機器中具有重要邏輯的應用程序進行適當的事后調試。但是,支持此功能會產生以下副作用:如果人們覆蓋unittest.TestCase __call__或者run需要以debug相同的方式覆蓋(對于標準unittest也是如此)。

注意

由于兩個框架之間的架構差異,在unittest測試call階段而不是在pytest標準setupteardown階段中執行基于測試的設置和拆卸。在某些情況下,這一點非常重要,特別是在推理錯誤時。例如,如果unittest基于a 的套件在設置期間出現錯誤,pytest則在其setup階段期間將報告沒有錯誤, 并且將在此期間引發錯誤call

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容