自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

深度剖析Python測(cè)試框架內(nèi)容

開發(fā) 后端
Python測(cè)試框架是一種代表簡(jiǎn)單主義思想的語言,由于你只需要把你的Python程序拷貝到另外一臺(tái)計(jì)算機(jī)上,它就可以工作了,這也使得你的Python程序更加易于移植。

下面進(jìn)行有效的說明一下Python測(cè)試框架,Python是一種解釋性的語言,但是這種說法是不正確的,其實(shí)他是一種可以讓沒有學(xué)習(xí)過編程或者并非計(jì)算機(jī)專業(yè)的編程學(xué)習(xí)的人,易于接受的語言。

Python測(cè)試框架中具有標(biāo)準(zhǔn)的單元測(cè)試框架(從 Python 2.3 開始。在 Python 2.2 中這只是可選模塊),與 Java jUnit 框架十分類似。測(cè)試用例的結(jié)構(gòu)與 jUnit 采用相同的方式。每一個(gè)待測(cè)試的類和模塊通常都具有自己的測(cè)試類。測(cè)試類中包含測(cè)試裝置(fixture)。

 

  1. import unittest  
  2. from pprint import pprint  
  3. import feedparser  
  4. class FeedparserTest(unittest.TestCase):  
  5.     """  
  6.     A test class for the feedparser module.  
  7.     """  
  8.       
  9.     def setUp(self):  
  10.         """  
  11.         set up data used in the tests.  
  12.         setUp is called before each test function execution.  
  13.         """  
  14.         self.developerWorksUrl = "testData/developerworks.rss"         
  15.     def testParse09Rss(self):  
  16.         """  
  17.         Test a successful run of the parse function for a  
  18.         0.91 RSS feed.  
  19.         """  
  20.         print "FeedparserTest.testParse09RSS()"  
  21.           
  22.         result = feedparser.parse(self.developerWorksUrl)  
  23.         pprint(result)  
  24.         self.assertEqual(0, result['bozo'])  
  25.           
  26.         self.assert_(result is not None)  
  27.         channel = result['channel']  
  28.         self.assert_(channel is not None)  
  29.         chanDesc = channel['description']  
  30.         self.assertEqual(u'The latest content from IBM developerWorks',  
  31.             chanDesc)  
  32.           
  33.         items = result['items']  
  34.         self.assert_(items is not None)  
  35.         self.assert_(len(items)> 3)  
  36.         firstItem = items[0]  
  37.         title = firstItem['title']  
  38.         self.assertEqual(u'Build installation packages with   
  39.             solution installation and deployment technologies',  
  40.             title)  
  41.     
  42.     def tearDown(self):  
  43.         """  
  44.         tear down any data used in tests  
  45.         tearDown is called after each test function execution.  
  46.         """  
  47.         pass  
  48.                   
  49. if __name__ == '__main__':  
  50.     unittest.main()  

它們?cè)?setUp 函數(shù)中初始化。每一個(gè)測(cè)試都編寫為測(cè)試類中的一個(gè)獨(dú)立的測(cè)試函數(shù)。unittest 框架會(huì)在測(cè)試函數(shù)之間循環(huán)往復(fù),先調(diào)用 setUp 、再測(cè)試函數(shù)、然后清除( tearDown )測(cè)試函數(shù)。上述清單是實(shí)現(xiàn) feedparser 模塊基本測(cè)試功能的測(cè)試類。完整的測(cè)試類見 feedParserTest 項(xiàng)目下的 src/feedparserTest/FeedparserTest.py。

setUp 函數(shù)負(fù)責(zé)準(zhǔn)備整個(gè)測(cè)試過程中需要使用的測(cè)試裝置,在本例中只有測(cè)試用的 RSS 文件的目錄,測(cè)試函數(shù)將對(duì)其進(jìn)行解析。 testParse09Rss 是真正的測(cè)試函數(shù)。這個(gè)函數(shù)調(diào)用 feedparser.parse 函數(shù),傳遞測(cè)試用的 RSS 文件,輸出解析結(jié)果。

并通過 TestCase 類的 assert 函數(shù)執(zhí)行基本的檢查統(tǒng)作。如果任何 assert 的求值結(jié)果不是真,或是在執(zhí)行過程中拋出任何異常。unittest 就會(huì)報(bào)告一次測(cè)試失敗或錯(cuò)誤。***的兩行負(fù)責(zé)在這個(gè)測(cè)試類內(nèi)部運(yùn)行測(cè)試,方法是直接運(yùn)行該模塊即可。

要獨(dú)立運(yùn)行該測(cè)試類,可以按前面所說的相同方式運(yùn)行 FeedparserTest.py 模塊。在 Eclipse Navigator 視圖中選擇 FeedparserTest.py。然后通過 Python測(cè)試框架> Run 運(yùn)行。此時(shí)顯示啟動(dòng)配置窗口。除 Base 目錄之外,其他都保持缺省值即可。Base 目錄必須是 feedParserTest 項(xiàng)目的目錄。

這樣才能在當(dāng)前目錄下找到 RSS 文件(testData/developerworks.rss)。修改 base 目錄的設(shè)置,然后點(diǎn)擊“Run”。輸出信息顯示在 Console 上。 您也許希望我們編寫的所有單元測(cè)試都能夠作為構(gòu)建的一部分自動(dòng)執(zhí)行。將下面清單 5 所示的構(gòu)建片斷加入構(gòu)建腳本便可實(shí)現(xiàn)。

***行是目標(biāo)聲明,這與其他的腳本相同。第 2 至第 6 行調(diào)用 py-test 任務(wù)。這部分代碼將在“src”目錄下查找所有以“Test.py”結(jié)尾的所有文件,并運(yùn)行所有測(cè)試。 PYTHONPATH 設(shè)置為“src”,測(cè)試執(zhí)行的當(dāng)前工作目錄就是當(dāng)前目錄(‘.’)。

【編輯推薦】

  1. 有關(guān)Python系統(tǒng)文件進(jìn)行介紹指導(dǎo)
  2. 如何正確的使用Python函數(shù)
  3. 對(duì)Python 構(gòu)建工具進(jìn)行詳細(xì)介紹分析
  4. PythonAndroid淺析Python優(yōu)勢(shì)所在
  5. 如何使用Python模塊解析配置文件?
責(zé)任編輯:chenqingxiang 來源: CSDN
相關(guān)推薦

2010-02-23 10:50:11

Python 測(cè)試框架

2010-03-01 17:40:29

Python面向?qū)ο笳Z

2009-12-07 18:43:29

WCF框架

2010-02-05 15:33:29

Android JDK

2009-12-07 17:21:50

WFC框架

2010-03-01 14:50:06

Python 工具

2010-03-01 18:33:30

2010-02-02 15:25:35

Python語法

2010-02-03 16:56:24

Python包

2010-02-01 13:34:59

Python 腳本

2010-01-27 09:31:39

C++Test測(cè)試

2010-02-07 15:42:46

Android單元測(cè)試

2010-02-26 13:21:42

WCF通道形狀

2010-02-22 13:53:22

Python 中文亂碼

2010-02-23 10:05:52

Python歷史

2010-02-02 13:22:06

Python面向?qū)ο?/a>

2010-02-26 10:38:29

Python語言

2010-02-03 09:35:20

Python函數(shù)編程

2010-02-03 11:26:28

2010-02-24 16:33:28

Python功能
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)