Python單元測試中有哪些裝飾器?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創(chuàng)新互聯是專業(yè)的珠海網站建設公司,珠海接單;提供成都網站建設、做網站,網頁設計,網站設計,建網站,PHP網站建設等專業(yè)做網站服務;采用PHP框架,可快速的進行珠海網站開發(fā)網頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網站,專業(yè)的做網站團隊,希望更多企業(yè)前來合作!
Python單元測試unittest中提供了一下四種裝飾器實現測試跳過和預期故障。
請查考Python手冊中:
#以下裝飾器實施測試跳過和預期故障:
@unittest.skip(原因)
Unconditionally skip the decorated test. reason should describe why the test is being skipped.
#無條件跳過裝飾測試。 原因應該說明為什么要跳過測試。
@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.
#將測試標記為預期的失敗。 如果測試在運行時失敗,則測試不會被視為失敗。
(以上采用谷歌翻譯,可能會有差異)
好了,寫段代碼看下,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()
好的,運行一下
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行代碼進行分析:
s:全稱是skipped(跳過)
s:條件為真,所以也是skipped(跳過)
F:條件為真,所以忽略裝飾器,執(zhí)行斷言代碼,顯然是failures(失?。?/p>
x:斷言結果顯然是失敗的,但是這是在我們意料之中,所以是expected failures(預期的失?。?/p>
u:斷言結果顯然是pass,但是我們預計可能不通過,所以是unexpected successes(意想不到的成功)
即第13行代碼 所示 FAILED (failures=1, skipped=2, expected failures=1, unexpected successes=1)
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯行業(yè)資訊頻道,感謝您對創(chuàng)新互聯的支持。
分享名稱:Python單元測試中有哪些裝飾器
當前路徑:http://sd-ha.com/article44/pepghe.html
成都網站建設公司_創(chuàng)新互聯,為您提供小程序開發(fā)、企業(yè)網站制作、手機網站建設、品牌網站建設、網站收錄、網站導航
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯