unittest 跳過(guò)測(cè)試和預(yù)期故障

2022-08-02 14:59 更新

3.1 版中的新功能。

Unittest支持跳過(guò)單個(gè)測(cè)試方法甚至整個(gè)測(cè)試類(lèi)。此外,它還支持將測(cè)試標(biāo)記為“預(yù)期失敗”,即已中斷并將失敗的測(cè)試,但不應(yīng)計(jì)為 TestResult 上的失敗。

跳過(guò)測(cè)試只需使用 skip() 裝飾器或其條件變體之一,在 setUp() 或測(cè)試方法中調(diào)用 TestCase.skipTest(), 或直接引發(fā) SkipTest。

基本跳過(guò)如下所示:

class MyTestCase(unittest.TestCase):

    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")

    @unittest.skipIf(mylib.__version__ < (1, 3),
                     "not supported in this library version")
    def test_format(self):
        # Tests that work for only a certain version of the library.
        pass

    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_windows_support(self):
        # windows specific testing code
        pass

    def test_maybe_skipped(self):
        if not external_resource_available():
            self.skipTest("external resource not available")
        # test code that depends on the external resource
        pass

這是在詳細(xì)模式下運(yùn)行上述示例的輸出:

test_format (__main__.MyTestCase) ... skipped 'not supported in this library version'
test_nothing (__main__.MyTestCase) ... skipped 'demonstrating skipping'
test_maybe_skipped (__main__.MyTestCase) ... skipped 'external resource not available'
test_windows_support (__main__.MyTestCase) ... skipped 'requires Windows'

----------------------------------------------------------------------
Ran 4 tests in 0.005s

OK (skipped=4)

可以像方法一樣跳過(guò)類(lèi):

@unittest.skip("showing class skipping")
class MySkippedTestCase(unittest.TestCase):
    def test_not_run(self):
        pass

TestCase.setUp() 也可以跳過(guò)測(cè)試。當(dāng)需要設(shè)置的資源不可用時(shí),這很有用。

預(yù)期的故障使用預(yù)期的Failure()裝飾器。

class ExpectedFailureTestCase(unittest.TestCase):
    @unittest.expectedFailure
    def test_fail(self):
        self.assertEqual(1, 0, "broken")

通過(guò)制作一個(gè)在測(cè)試中調(diào)用 skip() 的裝飾器,當(dāng)它希望它被跳過(guò)時(shí),很容易滾動(dòng)你自己的跳過(guò)裝飾器。除非傳遞的對(duì)象具有特定屬性,否則此裝飾器會(huì)跳過(guò)測(cè)試:

def skipUnlessHasattr(obj, attr):
    if hasattr(obj, attr):
        return lambda func: func
    return unittest.skip("{!r} doesn't have {!r}".format(obj, attr))

以下修飾器和異常實(shí)現(xiàn)測(cè)試跳過(guò)和預(yù)期故障:

?@unittest.skip(reason)?

無(wú)條件跳過(guò)裝飾測(cè)試。reason應(yīng)描述跳過(guò)測(cè)試的原因。

?@unittest.skipIf(conditionreason)?

如果條件為真,則跳過(guò)修飾測(cè)試。

?@unittest.skipUnless(condition, reason)?

跳過(guò)裝飾測(cè)試,除非條件為真。

?@unittest.expectedFailure?

將測(cè)試標(biāo)記為預(yù)期的失敗或錯(cuò)誤。如果測(cè)試失敗或測(cè)試函數(shù)本身(而不是其中一個(gè)測(cè)試夾具方法)中的錯(cuò)誤,則將被視為成功。如果測(cè)試通過(guò),則將被視為失敗。

      exception ?unittest.SkipTest(reason)?

引發(fā)此異常是為了跳過(guò)測(cè)試。

通常,您可以使用 TestCase.skipTest() 或其中一個(gè)跳過(guò)的裝飾器,而不是直接引發(fā)它。

跳過(guò)的測(cè)試不會(huì)有 setUp() 或 tearDown() 圍繞它們運(yùn)行。跳過(guò)的類(lèi)將不會(huì)運(yùn)行 setUpClass() 或 tearDownClass()。跳過(guò)的模塊將不會(huì)有?setUpModule()?或?tearDownModule()?運(yùn)行。


以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)