Python單元測試中有哪些裝飾器-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)公司長期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為高陵企業(yè)提供專業(yè)的網(wǎng)站設(shè)計制作、網(wǎng)站制作,高陵網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

Python單元測試unittest中提供了一下四種裝飾器實現(xiàn)測試跳過和預(yù)期故障。

請查考Python手冊中:

#以下裝飾器實施測試跳過和預(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.

#將測試標記為預(yù)期的失敗。 如果測試在運行時失敗,則測試不會被視為失敗。

(以上采用谷歌翻譯,可能會有差異)

好了,寫段代碼看下,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)
標題路徑:http://www.muchs.cn/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)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化