Python單元測試_使用裝飾器實現(xiàn)測試跳過和預(yù)期故障的方法
Python單元測試unittest中提供了一下四種裝飾器實現(xiàn)測試跳過和預(yù)期故障。(使用Python 2.7.13)
請查考Python手冊中:
https://docs.python.org/dev/library/unittest.html
The following decorators implement test skipping and expected failures:
#以下裝飾器實施測試跳過和預(yù)期故障:
@unittest.skip(原因)
Unconditionally skip the decorated test. reason should describe why the test is being skipped.
#無條件跳過裝飾測試。 原因應(yīng)該說明為什么要跳過測試。
@unittest.skipIf(條件,原因)
Skip the decorated test if condition is true.
#如果條件為真,跳過裝飾測試。
@unittest.skipUnless(條件,原因)
Skip the decorated test unless condition is true.
# 跳過裝飾的測試,除非條件是真的。
@unittest.expectedFailure
Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure.
#將測試標(biāo)記為預(yù)期的失敗。 如果測試在運(yùn)行時失敗,則測試不會被視為失敗。
(以上采用谷歌翻譯,可能會有差異)
好了,寫段代碼看下,test.py ,使用的Eclipse
#coding:UTF-8 import unittest from test.test_pprint import uni 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) @unittest.expectedFailure def test_ce_5(self): self.assertFalse(self.a==15) if __name__ == '__main__': unittest.main()
好的,運(yùn)行一下
ssFxu ====================================================================== FAIL: test_ce_3 (__main__.Test_ce) ---------------------------------------------------------------------- Traceback (most recent call last): File "D:\Escplise\workspace\Pytest\src\test001\CE.py", line 20, in test_ce_3 self.assertTrue(self.a>16) AssertionError: False is not true ---------------------------------------------------------------------- Ran 5 tests in 0.000s FAILED (failures=1, skipped=2, expected failures=1, unexpected successes=1)
好的,我們對第1行代碼進(jìn)行分析:
s:全稱是skipped(跳過)
s:條件為真,所以也是skipped(跳過)
F:條件為真,所以忽略裝飾器,執(zhí)行斷言代碼,顯然是failures(失敗)
x:斷言結(jié)果顯然是失敗的,但是這是在我們意料之中,所以是expected failures(預(yù)期的失?。?/p>
u:斷言結(jié)果顯然是pass,但是我們預(yù)計可能不通過,所以是unexpected successes(意想不到的成功)
即第13行代碼 所示 FAILED (failures=1, skipped=2, expected failures=1, unexpected successes=1)
以上這篇Python單元測試_使用裝飾器實現(xiàn)測試跳過和預(yù)期故障的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- python單元測試unittest實例詳解
- Python單元測試框架unittest使用方法講解
- 在Python中進(jìn)行自動化單元測試的教程
- 在Python的Flask框架中實現(xiàn)單元測試的教程
- Python之PyUnit單元測試實例
- 對Python的Django框架中的項目進(jìn)行單元測試的方法
- Python中unittest模塊做UT(單元測試)使用實例
- 在Python編程過程中用單元測試法調(diào)試代碼的介紹
- 詳解Python nose單元測試框架的安裝與使用
- 深入理解Python單元測試unittest的使用示例
- Python Unittest自動化單元測試框架詳解
- Python單元測試實例詳解
相關(guān)文章
關(guān)于Filter中獲取請求體body后再次讀取的問題
這篇文章主要介紹了關(guān)于Filter中獲取請求體body后再次讀取的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Spring中@Configuration注解修改的類生成代理原因解析
大家好,本篇文章主要講的是Spring中@Configuration注解修改的類生成代理原因解析,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02