unittest.skip跳過測試和unittest.expectedFailure預期失敗

 unittest.skip跳過測試方法

python unittest測試框架從python2.7開始支持設置跳過指定的測試方法或是跳過滿足某種條件的測試用例。

@unittest.skip(reason): skip(reason)裝飾器:無條件跳過裝飾的測試,並說明跳過測試的原因。

@unittest.skipIf(reason): skipIf(condition,reason)裝飾器:條件爲真時,跳過裝飾的測試,並說明跳過測試的原因。

@unittest.skipUnless(reason): skipUnless(condition,reason)裝飾器:條件爲假時,跳過裝飾的測試,並說明跳過測試的原因。

@unittest.expectedFailure: 標記該測試預期爲失敗 ,如果該測試方法運行失敗,則該測試不算做失敗

#coding:UTF-8
import unittest

class Test_ce(unittest.TestCase):
  a=16
  b=10
   
  @unittest.skip('無條件跳過')
  def test_ce1(self):
    self.assertEqual((self.a-self.b), 16)
    #判斷是否相等
     
  @unittest.skipIf(True==1, '條件爲真則跳過')
  def test_ce_2(self):
    self.assertFalse(self.a==self.b)
    #判斷是否爲False
     
  @unittest.skipUnless(1==1, '條件爲假則跳過')
  def test_ce_3(self):
    self.assertTrue(self.a>16)
    #判斷是否爲True
 
  @unittest.expectedFailure #標記該測試預期爲失敗 ,如果該測試方法運行失敗,則該測試不算做失敗
  def test_ce_4(self):
    self.assertFalse(self.a==16)
    #斷言結果是否爲False,爲假則測試用例通過
     
  @unittest.expectedFailure 
  def test_ce_5(self): 
    self.assertFalse(self.a==15)
     
if __name__ == '__main__':
  unittest.main()

'''
s:全稱是skipped(跳過)

s:條件爲真,所以也是skipped(跳過)

F:條件爲真,所以忽略裝飾器,執行斷言代碼,顯然是failures(失敗)

x:斷言結果顯然是失敗的,但是這是在我們意料之中,所以是expected failures(預期的失敗)

u:斷言結果顯然是pass,但是我們預計可能不通過,所以是unexpected successes(意想不到的成功)

即執行結果所示  FAILED (failures=1, skipped=2, expected failures=1, unexpected successes=1)

'''

 

 執行結果:

D:\python20190819>"C:/Program Files/Python37/python.exe" d:/python20190819/unittest跳過測試和預期失敗.py
ssFxu
======================================================================
FAIL: test_ce_3 (__main__.Test_ce)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:/python20190819/unittest跳過測試和預期失敗.py", line 20, in test_ce_3
    self.assertTrue(self.a>16)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 5 tests in 0.001s

FAILED (failures=1, skipped=2, expected failures=1, unexpected successes=1)

D:\python20190819>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章