一、安裝nose
先用easy_install 安裝 nose,easy_install是很好的python工具,可以方便安裝很多的python程序。可以去http://pypi.python.org/pypi/setuptools了解一下easy_install。如果懶得去看的,可以直接從這里下載安裝文件進(jìn)行安裝就可以了,注意,這個鏈接是windows 32位的安裝包。
安裝完easy_install后,在相應(yīng)版本的Scripts目錄下(例如C:\Python26\Scripts)會有一個easy_install.exe程序,通過這個就可以安裝了。在命令行下轉(zhuǎn)到Python的Scripts目錄下,執(zhí)行以下的命令進(jìn)行安裝:
C:\Python26\Scripts\easy_install nose
上面的 C:\Python26\Scripts 需要根據(jù)您的Python的安裝路徑進(jìn)行修改。
安完畢后,在C:\Python26\Scripts下會有一個nosetests.exe文件,通過這個exe程序就可以在命令行下運(yùn)行測試了。最好是把C:\Python26\Scripts加入環(huán)境變量,這樣在其它目錄中可以直接引用nosetests.exe。
二、運(yùn)行測試
在命令行下,直接運(yùn)行nosetests(注意要把nosetests.exe所在的目錄加入到環(huán)境變量Path里面),它就會自動查找當(dāng)前目錄下包含"Test"字符串的目錄和文件進(jìn)行測試。例如如下的目錄結(jié)構(gòu):
如果我們在C:\PythonTestRoot下運(yùn)行nosetests,那么nose會自動找到TestUnit1文件夾,然后在找到 Test2.py和TestSomething.py兩個文件進(jìn)行測試,我的兩個文件中各有兩個方法,一共有四個測試:
這樣我們可以把所有測試放在一起,然后讓測試自己去運(yùn)行,我們最后看結(jié)果就可以了。我們可以指定具體如何輸出結(jié)果,也可以指定如何搜索文件和文件夾,還可以把測試結(jié)果輸入到指定的文件,在nosetests后面加上一些命令行參數(shù)就可以,參數(shù)如何設(shè)置在最后面介紹。
三、編寫測試
上面說的運(yùn)行測試其實(shí)非常簡單,寫好測試放到一個文件夾里面,然后在這個文件夾里面執(zhí)行nosetests就可以了。編寫測試也非常簡單:
a)簡單的測試
Python代碼
======================= #### file: test.py #### ======================= def Testfunc(): a = 1 b = 2 assert a == b
把上面的文件保存到一個目錄下,然后在該目錄下在命令行里執(zhí)行nosetests就可以運(yùn)行測試了。
b)模塊的setUp和tearDown
Python代碼
def setUp(): print "function setup" def tearDown(): print "function teardown" def Testfunc1(): print "Testfunc1" assert True def Testfunc2(): print "Testfunc2" assert True
nose在文件中如果找到函數(shù)setup, setup_module, setUp 或者setUpModule等,那么會在該模塊的所有測試執(zhí)行之前執(zhí)行該函數(shù)。如果找到函數(shù) teardown,tearDown, teardown_module或者 tearDownModule 等,那么會在該模塊所有的測試執(zhí)行完之后執(zhí)行該函數(shù)。
對于上面的代碼,nose實(shí)際的執(zhí)行過程是這樣的:
setUp()->Testfunc1()->Testfunc2()->tearDown()
c)測試函數(shù)的setUp和tearDown
可能會想給每個函數(shù)單獨(dú)指定類似的setUp和tearDown函數(shù),可以如下處理:
Python代碼
def setUp(): print "function setup" def tearDown(): print "function teardown" def func1Start(): print "func1 start" def func1End(): print "func1 end" def func2Start(): print "func2 start" def func2End(): print "func2 end" def Testfunc1(): print "Testfunc1" assert True def Testfunc2(): print "Testfunc2" assert True Testfunc1.setup = func1Start Testfunc1.tearDown = func1End Testfunc2.setup = func2Start Testfunc2.tearDown = func2End
注意最后面的四行,分別指定了Testfunc1和Testfun2的setup和teardown函數(shù)。
nose對上面代碼的具體執(zhí)行順序如下:
setUp()->func1Start()->Testfunc1()->func1End()->func2Start()->Testfunc2()->func2End()->tearDown()
上面的代碼也可以換一種寫法如下,注意記得 import with_setup:
Python代碼
from nose.tools import with_setup def setUp(): print "function setup" def tearDown(): print "function teardown" def func1Start(): print "func1 start" def func1End(): print "func1 end" def func2Start(): print "func2 start" def func2End(): print "func2 end" @with_setup(func1Start, func1End) def Testfunc1(): print "Testfunc1" assert True @with_setup(func2Start, func2End) def Testfunc2(): print "Testfunc2" assert True
d)測試類的的setUp和tearDown
看如下的代碼:
Python代碼
class TestClass(): arr1 = 2 arr2 = 2 def setUp(self): self.arr1 = 1 self.arr2 = 3 print "MyTestClass setup" def tearDown(self): print "MyTestClass teardown" def Testfunc1(self): assert self.arr1 == self.arr2 def Testfunc2(self): assert self.arr1 == 2
這里nose會對每個類的測試方法單獨(dú)創(chuàng)建類的實(shí)例,并且有單獨(dú)的setUp和tearDown。nose對上面測試的順序如下:
setUp()->Testfunc1()->TearDown()->setUp()->Testfunc2()->TearDown()
e)package的setUp和tearDown
package的setUp和tearDown方法需要放在__init__.py這個文件中,整個package只執(zhí)行一次setUp和一次tearDown。
四、nosetest常用的命令行參數(shù)
這里只重點(diǎn)介紹幾個常用的,其它的參數(shù)可以通過nosetests -h進(jìn)行查看。
a) -w ,指定一個目錄運(yùn)行測試。目錄可以是相對路徑或絕對路徑。
例如: nosetest -w c:\pythonTests\Test1,只運(yùn)行目錄c:\pythonTests\Test1下的測試。可以指定多個目錄,例如: nosetest -w c:\pythonTests\Test1 -w c:\pythonTests\Test2。
b)-s,不捕獲輸出,會讓你的程序里面的一些命令行上的輸出顯示出來。例如print所輸出的內(nèi)容。
c)-v,查看nose的運(yùn)行信息和調(diào)試信息。例如會給出當(dāng)前正在運(yùn)行哪個測試。