Python單元測試中有哪些裝飾器?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

Python單元測試unittest中提供了一下四種裝飾器實(shí)現(xiàn)測試跳過和預(yù)期故障。
請查考Python手冊中:
#以下裝飾器實(shí)施測試跳過和預(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()
名稱欄目:Python單元測試中有哪些裝飾器-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://chinadenli.net/article2/ceosoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、微信小程序、網(wǎng)站收錄、ChatGPT、定制網(wǎng)站、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容