前言
當測試用例寫完后,有些模塊有改動時候,會影響到部分用例的執行,這個時候我們希望暫時跳過這些用例。
或者前面某個功能運行失敗了,后面的幾個用例是依賴於這個功能的用例,如果第一步就失敗了,后面的用例也就沒必要去執行了,直接跳過就行,節省用例執行時間。
一、skip裝飾器
skip裝飾器一共有四個
@
unittest.
skip
(reason)
-
Unconditionally skip the decorated test. reason should describe why the test is being skipped.
翻譯:無條件跳過用例,reason是說明原因
-
@
unittest.
skipIf
(condition, reason) -
Skip the decorated test if condition is true.
翻譯:condition為true的時候跳過
-
@
unittest.
skipUnless
(condition, reason) -
Skip the decorated test unless condition is true.
翻譯:condition為False的時候跳過
-
@
unittest.
expectedFailure
-
Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure.
翻譯:斷言的時候跳過()
二、skip案例
運行結果:
測試1
測試4
.ssx
----------------------------------------------------------------------
Ran 4 tests in 0.003s
OK (skipped=2, expected failures=1)
三、跳過整個測試類
四、參考代碼:
# coding:utf-8
import unittest
class Test(unittest.TestCase):
@unittest.skip(u"無條件跳過此用例")
def test_1(self):
print "測試1"
@unittest.skipIf(True, u"為True的時候跳過")
def test_2(self):
print "測試2"
@unittest.skipUnless(False, u"為False的時候跳過")
def test_3(self):
print "測試3"
@unittest.expectedFailure
def test_4(self):
print "測試4"
self.assertEqual(2, 4, msg=u"判斷相等")
if __name__ == "__main__":
unittest.main()
** 作者:上海-悠悠 QQ交流群:588402570**