閱讀《Practical Programming -An Introduction to Computer Science Using Python》一書時(shí),在第4章提到了使用Nose對(duì)Python代碼進(jìn)行測試,因而小試牛刀了一下。
---------------------------------------------------------------------------------------------------------
安裝
正常情況下,安裝步驟如下:
- 首先,下載:setuptools-->安裝
- 其次,打開cmd-->進(jìn)入命令提示行,此時(shí)保持電腦可以上外網(wǎng),在命令提示行中輸入 C:\Python27\Scripts\easy_install nose 回車,此時(shí)會(huì)從網(wǎng)上自動(dòng)下載東西,不用管它,Nose會(huì)自動(dòng)安裝完成。
注:這里的C:\Python27,是指python所在的文件夾,如果不同可以更改!
但是,現(xiàn)在我這里有兩個(gè)問題:
- 我安裝的是python3.3,但是setuptools沒有支持3.3的官方版本
- 待我安裝了python2.7和setuptools-py2.7后,按照上述方法在線安裝nose時(shí),又一直卡在那“reading xxx”,半天沒反應(yīng)(難道是我網(wǎng)速原因?)
沒辦法,只有去找別的安裝nose的方法,于是找到了這篇博文,介紹的安裝的方法不是通過setuptools在線安裝,而是如下:
- 先下載nose包,于是我下了一個(gè)支持python3.3的nose1.2.1
- 把下載的在python安裝路徑\Lib\site-packages下加壓,必須把加壓目錄下的‘nose'文件夾放在 \Lib\site-packages路徑下
- 點(diǎn)擊setup
按此步驟試了,不過setup后卻提示安裝error了,運(yùn)行如下代碼測試時(shí)果然有問題:
- import nose
- from temperature import to_celsius
-
- def test_roundoff ():
- '''''Test that roundoff works'''
- assert to_celsius(100) == 38, 'Returning an unrounded result' #not 37.77...
-
- if __name__ == "__main__":
- nose.runmodule()
運(yùn)行后提示:
- Traceback (most recent call last):
- File "test_temperature.py", line 29, in <module>
- AttributeError: 'module' object has no attribute 'runmodule'
網(wǎng)上找了半天還是沒找到啥問題,也是是nose就沒安裝上。沒辦法,只好從頭再來,到了晚上用上面說的正常情況下的安裝步驟進(jìn)行安裝,這次竟然成功了~ 不過是安裝python2.7版本的nose。
---------------------------------------------------------------------------------------------------------
使用
本次使用主要是用書上的例子進(jìn)行的測試。測試文件的名字以“test”開始,Nose運(yùn)行時(shí),它會(huì)自動(dòng)尋找以“test”開始的文件。每個(gè)Nose測試模塊都應(yīng)該包含以下內(nèi)容:
- 用于引入Nose及待測模塊的語句
- 實(shí)際用于測試我們模塊的函數(shù)
- 用于觸發(fā)那些測試函數(shù)的函數(shù)的調(diào)用
測試函數(shù)的名稱也必須以“test”開頭。 待測模塊temperature.py代碼如下:
- def to_celsius (t):
- return round ( (t-32.0)*5.0/9.0 )
-
- def above_freezing (t):
- return t>0
測試模塊test_temperature.py代碼如下:
- import nose
- from temperature import to_celsius
- from temperature import above_freezing
-
- def test_above_freezing ():
- '''''Test above_freezing '''
- assert above_freezing(89.4), 'A temperature above freezing'
- assert not above_freezing(-42), 'A temperature below freezing'
- assert not above_freezing(0), 'A temperature at freezing'
-
- def test_boiling ():
- '''''Test boiling point'''
- assert to_celsius(212) == 100
-
- def test_roundoff ():
- '''''Test that roundoff works'''
- assert to_celsius(100) == 38, 'Returning an unrounded result' #not 37.77...
-
- if __name__ == "__main__":
- nose.runmodule()
上面這段代碼執(zhí)行之后,每個(gè)測試都會(huì)得出以下三種結(jié)果之一:
- 通過(pass)。實(shí)際值跟期望值相符。
- 失敗(fail)。實(shí)際值跟期望值不相符。
- 出錯(cuò)(error)。測試本身出了問題;也就是說,測試代碼中含有bug。在這種情況下,測試將無法告訴我們?nèi)魏斡嘘P(guān)被測系統(tǒng)的信息。
上面的函數(shù)test_above_freezing中有3條assert語句,但Nose還是會(huì)認(rèn)為這只是一個(gè)測試,這是因?yàn)椋瑢?duì)Nose而言,每個(gè)函數(shù)就是一個(gè)測試,一個(gè)函數(shù)想要測試多少東西那是函數(shù)自己的事情。這樣有個(gè)缺點(diǎn)就是當(dāng)其中的某個(gè)斷言失敗時(shí),python就會(huì)立即停止執(zhí)行它所在的函數(shù)。也就是說,如果test_above_freezing中的第一個(gè)測試就失敗了,我們將無法得到其他測試的任何信息。因此通常的做法是編寫許多小的測試函數(shù),分別只測試少量的東西,而不是在其中放上一大堆各種各樣的斷言。
---------------------------------------------------------------------------------------------------------
參考
1. 《Practical Programming -An Introduction to Computer Science Using Python》
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。